Search in sources :

Example 1 with Graphics

use of java.awt.Graphics in project OpenNotebook by jaltekruse.

the class DocViewerPanel method makeDocPanel.

private JPanel makeDocPanel() {
    return new JPanel() {

        private static final long serialVersionUID = 1L;

        @Override
        public void paint(Graphics g) {
            // TODO - Stopwatch and logging
            //				System.out.println("painting doc:" + (new java.util.Date().getTime() - notebook.timeAtStart));
            //set the graphics object to render text and shapes with smooth lines
            Graphics2D g2d = (Graphics2D) g;
            if (!OpenNotebook.isMinimalGraphicsMode()) {
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            }
            //pixels needed to render a page, and the minimum border space around it
            int pageXSize = (int) (getDoc().getWidth() * zoomLevel);
            int pageYSize = (int) (getDoc().getHeight() * zoomLevel);
            Rectangle viewPortRect = new Rectangle((int) docScrollPane.getViewport().getViewPosition().getX(), (int) docScrollPane.getViewport().getViewPosition().getY(), docScrollPane.getViewport().getWidth(), docScrollPane.getViewport().getHeight());
            //fill background with gray
            g.setColor(Color.GRAY.brighter());
            ((Graphics2D) g).fill(viewPortRect);
            Rectangle currPageRect;
            Point pageOrigin = null;
            //used to store origin of the part of the page that can be seen
            //and the sections overall width and height, both are in the user space at 72 dpi
            int xShowing, yShowing, xSizeShowing, ySizeShowing;
            for (int i = 0; i < doc.getNumPages(); i++) {
                //need to modify rectangle to properly calculate the portion of the page currently displayed
                //which will be given in the user space starting with the origin of the printable area at 0,0
                //and at 72 dpi
                pageOrigin = getPageOrigin(i);
                currPageRect = new Rectangle((int) pageOrigin.getX(), (int) pageOrigin.getY(), pageXSize, pageYSize);
                if (viewPortRect.intersects(currPageRect)) {
                    //render page only if it is in the current section of the document showing
                    if (viewPortRect.getX() < currPageRect.getX()) {
                        xShowing = 0;
                    } else {
                        xShowing = (int) ((viewPortRect.getX() - currPageRect.getX()) / zoomLevel);
                    }
                    if (viewPortRect.getY() < currPageRect.getY()) {
                        yShowing = 0;
                    } else {
                        yShowing = (int) ((viewPortRect.getY() - currPageRect.getY()) / zoomLevel);
                    }
                    pageGUI.drawPageWithDecorations(g, doc.getPage(i), new Point((int) pageOrigin.getX(), (int) pageOrigin.getY()), new Rectangle(xShowing, yShowing, 10, 0), zoomLevel);
                }
            }
            if (selectionRect != null) {
                pageGUI.polygonGUI.drawMathObject(selectionRect, g2d, getPageOrigin(selectionRect.getParentPage()), zoomLevel);
            }
            g.dispose();
        }
    };
}
Also used : Graphics(java.awt.Graphics) JPanel(javax.swing.JPanel) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D)

Example 2 with Graphics

use of java.awt.Graphics in project jna by java-native-access.

the class AlphaMaskDemo method updateX11.

private void updateX11(boolean a, boolean i) {
    X11 x11 = X11.INSTANCE;
    X11.Window win = X11.Window.None;
    Display dpy = x11.XOpenDisplay(null);
    try {
        if (!alphaWindow.isDisplayable()) {
            alphaWindow.pack();
            if (System.getProperty("java.version").matches("^1\\.4\\..*"))
                alphaWindow.setVisible(true);
            win = new X11.Window((int) Native.getWindowID(alphaWindow));
            Window parent = alphaWindow.getOwner();
            Point where = parent.getLocationOnScreen();
            where.translate(parent.getWidth(), 0);
            alphaWindow.removeAll();
            alphaWindow.setLocation(where);
            alphaWindow.setBackground(new Color(0, 0, 0, 0));
        } else {
            win = new X11.Window((int) Native.getWindowID(alphaWindow));
        }
        if (i) {
            int w = image.getWidth(null);
            int h = image.getHeight(null);
            alphaWindow.setSize(w, h);
            if (buffer == null || buffer.size() != w * h * 4) {
                buffer = new com.sun.jna.Memory(w * h * 4);
                pixels = new int[w * h];
            }
            BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics g = buf.getGraphics();
            g.drawImage(image, 0, 0, w, h, null);
            long start = System.currentTimeMillis();
            long blitTime, putImageTime, write;
            GC gc = x11.XCreateGC(dpy, win, new NativeLong(0), null);
            long gcTime = System.currentTimeMillis();
            try {
                Raster raster = buf.getData();
                int[] pixel = new int[4];
                for (int y = 0; y < h; y++) {
                    for (int x = 0; x < w; x++) {
                        raster.getPixel(x, y, pixel);
                        int alpha = (pixel[3] & 0xFF) << 24;
                        int red = (pixel[2] & 0xFF);
                        int green = (pixel[1] & 0xFF) << 8;
                        int blue = (pixel[0] & 0xFF) << 16;
                        pixels[y * w + x] = alpha | red | green | blue;
                    }
                }
                blitTime = System.currentTimeMillis();
                X11.XWindowAttributes xwa = new X11.XWindowAttributes();
                x11.XGetWindowAttributes(dpy, win, xwa);
                X11.XImage image = x11.XCreateImage(dpy, xwa.visual, 32, X11.ZPixmap, 0, buffer, w, h, 32, w * 4);
                buffer.write(0, pixels, 0, pixels.length);
                write = System.currentTimeMillis();
                x11.XPutImage(dpy, win, gc, image, 0, 0, 0, 0, w, h);
                x11.XFree(image.getPointer());
                putImageTime = System.currentTimeMillis();
            } finally {
                if (gc != null)
                    x11.XFreeGC(dpy, gc);
            }
            long end = System.currentTimeMillis();
        //System.out.println("gc: " + (gcTime-start) + "ms");
        //System.out.println("blit: " + (blitTime-gcTime) + "ms");
        //System.out.println("write: " + (write-blitTime) + "ms");
        //System.out.println("put image: " + (putImageTime-write) + "ms");
        //System.out.println("total: " + (end-start) + "ms");
        }
    } finally {
        if (dpy != null)
            x11.XCloseDisplay(dpy);
    }
    if (a)
        WindowUtils.setWindowAlpha(alphaWindow, alpha);
    if (!alphaWindow.isVisible()) {
        alphaWindow.setVisible(true);
        // hack for initial refresh (X11)
        update(true, true);
    }
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) Color(java.awt.Color) NativeLong(com.sun.jna.NativeLong) Raster(java.awt.image.Raster) Point(java.awt.Point) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics(java.awt.Graphics) X11(com.sun.jna.platform.unix.X11) GC(com.sun.jna.platform.unix.X11.GC) Display(com.sun.jna.platform.unix.X11.Display)

Example 3 with Graphics

use of java.awt.Graphics in project jna by java-native-access.

the class WindowUtilsTest method xtestReveal.

public void xtestReveal() throws Exception {
    final int SIZE = 200;
    System.setProperty("sun.java2d.noddraw", "true");
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    Window w;
    Container content;
    if (true) {
        JFrame frame = new JFrame(getName(), gconfig);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        content = frame.getContentPane();
        w = frame;
    } else {
        Frame frame = JOptionPane.getRootFrame();
        JWindow window = new JWindow(frame, gconfig);
        content = window.getContentPane();
        w = window;
    }
    final Window f = w;
    WindowUtils.setWindowTransparent(f, true);
    content.add(new JButton("Quit") {

        private static final long serialVersionUID = 1L;

        {
            addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
        }
    }, BorderLayout.SOUTH);
    content.add(new JComponent() {

        private static final long serialVersionUID = 1L;

        public Dimension getPreferredSize() {
            return new Dimension(SIZE, SIZE);
        }

        protected void paintComponent(Graphics graphics) {
            Graphics2D g = (Graphics2D) graphics.create();
            g.setComposite(AlphaComposite.Clear);
            g.fillRect(0, 0, SIZE, SIZE);
            g.dispose();
            g = (Graphics2D) graphics.create();
            Color[] colors = { new Color(0, 0, 0), new Color(0, 0, 0, 128), new Color(128, 128, 128), new Color(128, 128, 128, 128), new Color(255, 255, 255), new Color(255, 255, 255, 128) };
            for (int i = 0; i < colors.length; i++) {
                g.setColor(colors[i]);
                g.fillRect((SIZE * i) / colors.length, 0, (SIZE + colors.length - 1) / colors.length, SIZE);
            }
            g.setColor(Color.red);
            g.drawRect(0, 0, SIZE - 1, SIZE - 1);
            g.dispose();
            SwingUtilities.getWindowAncestor(this).toFront();
        }
    });
    f.pack();
    f.addMouseListener(handler);
    f.addMouseMotionListener(handler);
    f.setLocation(100, 100);
    f.setVisible(true);
    while (f.isVisible()) {
        Thread.sleep(1000);
    //f.repaint();
    }
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) JFrame(javax.swing.JFrame) Frame(java.awt.Frame) ActionEvent(java.awt.event.ActionEvent) JWindow(javax.swing.JWindow) Color(java.awt.Color) JButton(javax.swing.JButton) JComponent(javax.swing.JComponent) Dimension(java.awt.Dimension) Point(java.awt.Point) GraphicsConfiguration(java.awt.GraphicsConfiguration) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) Container(java.awt.Container) ActionListener(java.awt.event.ActionListener) JFrame(javax.swing.JFrame)

Example 4 with Graphics

use of java.awt.Graphics in project jna by java-native-access.

the class WindowUtilsTest method xtestWindowTransparency.

// Expect failure on windows and x11, since transparent pixels are not 
// properly captured by java.awt.Robot
public void xtestWindowTransparency() throws Exception {
    if (GraphicsEnvironment.isHeadless())
        return;
    System.setProperty("sun.java2d.noddraw", "true");
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    Frame root = JOptionPane.getRootFrame();
    final Window background = new Window(root);
    background.setBackground(Color.white);
    background.setLocation(X, Y);
    final JWindow transparent = new JWindow(root, gconfig);
    transparent.setLocation(X, Y);
    ((JComponent) transparent.getContentPane()).setOpaque(false);
    transparent.getContentPane().add(new JComponent() {

        private static final long serialVersionUID = 1L;

        public Dimension getPreferredSize() {
            return new Dimension(W, H);
        }

        protected void paintComponent(Graphics g) {
            g = g.create();
            g.setColor(Color.red);
            g.fillRect(getWidth() / 4, getHeight() / 4, getWidth() / 2, getHeight() / 2);
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
            g.dispose();
        }
    });
    transparent.addMouseListener(handler);
    transparent.addMouseMotionListener(handler);
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            background.pack();
            background.setSize(new Dimension(W, H));
            background.setVisible(true);
            transparent.pack();
            transparent.setSize(new Dimension(W, H));
            transparent.setVisible(true);
            transparent.toFront();
        }
    });
    WindowUtils.setWindowTransparent(transparent, true);
    //robot.delay(60000);
    Color sample = robot.getPixelColor(X + W / 2, Y + H / 2);
    assertEquals("Painted pixel should be opaque", Color.red, sample);
    sample = robot.getPixelColor(X + 10, Y + 10);
    assertEquals("Unpainted pixel should be transparent", Color.white, sample);
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) Graphics(java.awt.Graphics) JFrame(javax.swing.JFrame) Frame(java.awt.Frame) JWindow(javax.swing.JWindow) Color(java.awt.Color) JComponent(javax.swing.JComponent) Dimension(java.awt.Dimension) GraphicsConfiguration(java.awt.GraphicsConfiguration)

Example 5 with Graphics

use of java.awt.Graphics in project jfinal by jfinal.

the class CaptchaRender method drawGraphic.

private String drawGraphic(BufferedImage image) {
    // 获取图形上下文
    Graphics g = image.createGraphics();
    // 生成随机类
    Random random = new Random();
    // 设定背景色
    g.setColor(getRandColor(200, 250));
    g.fillRect(0, 0, WIDTH, HEIGHT);
    // 设定字体
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
    g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 155; i++) {
        int x = random.nextInt(WIDTH);
        int y = random.nextInt(HEIGHT);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
        g.drawLine(x, y, x + xl, y + yl);
    }
    // 取随机产生的认证码(4位数字)
    String sRand = "";
    for (int i = 0; i < 4; i++) {
        String rand = String.valueOf(strArr[random.nextInt(strArr.length)]);
        sRand += rand;
        // 将认证码显示到图象中
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
        g.drawString(rand, 16 * i + 11, 19);
    }
    // 图象生效
    g.dispose();
    return sRand;
}
Also used : Graphics(java.awt.Graphics) Random(java.util.Random) Color(java.awt.Color) Font(java.awt.Font)

Aggregations

Graphics (java.awt.Graphics)158 BufferedImage (java.awt.image.BufferedImage)91 Graphics2D (java.awt.Graphics2D)22 Dimension (java.awt.Dimension)20 Color (java.awt.Color)19 File (java.io.File)18 Frame (java.awt.Frame)14 JFrame (javax.swing.JFrame)13 Point (java.awt.Point)12 Rectangle (java.awt.Rectangle)12 Component (java.awt.Component)11 IOException (java.io.IOException)11 JComponent (javax.swing.JComponent)9 FontMetrics (java.awt.FontMetrics)8 GraphicsConfiguration (java.awt.GraphicsConfiguration)8 Insets (java.awt.Insets)8 Panel (java.awt.Panel)8 ImageIcon (javax.swing.ImageIcon)8 GraphicsDevice (java.awt.GraphicsDevice)6 Window (java.awt.Window)6