Search in sources :

Example 11 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 12 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 13 with Graphics

use of java.awt.Graphics in project yyl_example by Relucent.

the class DoubleBufferCanvas method update.

// ==============================Methods==========================================
/**
	 * 更新
	 * @param g 指定的 Graphics 上下文
	 */
@Override
public void update(Graphics g) {
    Graphics bufferGraphics = getBufferGraphics();
    if (bufferGraphics == null) {
        paint(g);
    } else {
        paint(bufferGraphics);
        bufferGraphics.dispose();
        g.drawImage(canvasBufferImage, 0, 0, this);
    }
}
Also used : Graphics(java.awt.Graphics)

Example 14 with Graphics

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

the class AbstractBlackBoxTestCase method rotateImage.

protected static BufferedImage rotateImage(BufferedImage original, float degrees) {
    if (degrees == 0.0f) {
        return original;
    }
    switch(original.getType()) {
        case BufferedImage.TYPE_BYTE_INDEXED:
        case BufferedImage.TYPE_BYTE_BINARY:
            BufferedImage argb = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics g = argb.createGraphics();
            g.drawImage(original, 0, 0, null);
            g.dispose();
            original = argb;
            break;
    }
    double radians = Math.toRadians(degrees);
    // Transform simply to find out the new bounding box (don't actually run the image through it)
    AffineTransform at = new AffineTransform();
    at.rotate(radians, original.getWidth() / 2.0, original.getHeight() / 2.0);
    BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
    RectangularShape r = op.getBounds2D(original);
    int width = (int) Math.ceil(r.getWidth());
    int height = (int) Math.ceil(r.getHeight());
    // Real transform, now that we know the size of the new image and how to translate after we rotate
    // to keep it centered
    at = new AffineTransform();
    at.rotate(radians, width / 2.0, height / 2.0);
    at.translate((width - original.getWidth()) / 2.0, (height - original.getHeight()) / 2.0);
    op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
    return op.filter(original, new BufferedImage(width, height, original.getType()));
}
Also used : Graphics(java.awt.Graphics) BufferedImageOp(java.awt.image.BufferedImageOp) RectangularShape(java.awt.geom.RectangularShape) AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) AffineTransformOp(java.awt.image.AffineTransformOp)

Example 15 with Graphics

use of java.awt.Graphics in project jdk8u_jdk by JetBrains.

the class BMPCompressionTest method showDiff.

private static void showDiff(final BufferedImage in, final BufferedImage out) {
    final int width = in.getWidth();
    final int height = in.getHeight();
    JFrame f = new JFrame("");
    f.getContentPane().add(new JComponent() {

        public Dimension getPreferredSize() {
            return new Dimension(2 * width + 2, height);
        }

        public void paintComponent(Graphics g) {
            g.setColor(Color.black);
            g.drawImage(in, 0, 0, null);
            g.drawImage(out, width + 2, 0, null);
        }
    });
    f.pack();
    f.setVisible(true);
}
Also used : Graphics(java.awt.Graphics) JFrame(javax.swing.JFrame) JComponent(javax.swing.JComponent) Dimension(java.awt.Dimension)

Aggregations

Graphics (java.awt.Graphics)217 BufferedImage (java.awt.image.BufferedImage)104 Dimension (java.awt.Dimension)35 Point (java.awt.Point)32 Graphics2D (java.awt.Graphics2D)31 Color (java.awt.Color)28 JPanel (javax.swing.JPanel)22 Insets (java.awt.Insets)21 Rectangle (java.awt.Rectangle)21 JLabel (javax.swing.JLabel)19 BorderLayout (java.awt.BorderLayout)18 File (java.io.File)18 FontMetrics (java.awt.FontMetrics)16 Frame (java.awt.Frame)16 MolecularComponentPattern (org.vcell.model.rbm.MolecularComponentPattern)16 MolecularTypePattern (org.vcell.model.rbm.MolecularTypePattern)16 Component (java.awt.Component)15 Font (java.awt.Font)15 GridBagLayout (java.awt.GridBagLayout)15 JScrollPane (javax.swing.JScrollPane)15