Example usage for java.awt Graphics fillRect

List of usage examples for java.awt Graphics fillRect

Introduction

In this page you can find the example usage for java.awt Graphics fillRect.

Prototype

public abstract void fillRect(int x, int y, int width, int height);

Source Link

Document

Fills the specified rectangle.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        return;//  ww  w.j  a  v a2 s .  co  m
    }
    SystemTray tray = SystemTray.getSystemTray();

    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);

    PopupMenu popup = new PopupMenu();
    MenuItem miExit = new MenuItem("Exit");
    ActionListener al;
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
    miExit.addActionListener(al);
    popup.add(miExit);

    TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2");
    ti.setPopupMenu(popup);

    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());
        }
    };
    ti.setActionCommand("My Icon");
    ti.addActionListener(al);

    MouseListener ml = new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            System.out.println("Tray icon: Mouse clicked");
        }

        public void mouseEntered(MouseEvent e) {
            System.out.println("Tray icon: Mouse entered");
        }

        public void mouseExited(MouseEvent e) {
            System.out.println("Tray icon: Mouse exited");
        }

        public void mousePressed(MouseEvent e) {
            System.out.println("Tray icon: Mouse pressed");
        }

        public void mouseReleased(MouseEvent e) {
            System.out.println("Tray icon: Mouse released");
        }
    };
    ti.addMouseListener(ml);
    MouseMotionListener mml = new MouseMotionListener() {
        public void mouseDragged(MouseEvent e) {
            System.out.println("Tray icon: Mouse dragged");
        }

        public void mouseMoved(MouseEvent e) {
            System.out.println("Tray icon: Mouse moved");
        }
    };
    ti.addMouseMotionListener(mml);

    MouseMotionListener[] mouseMotionListeners = ti.getMouseMotionListeners();
    for (MouseMotionListener ac : mouseMotionListeners) {
        ti.removeMouseMotionListener(ac);
    }

    tray.add(ti);
}

From source file:SystemTrayDemo2.java

public static void main(String[] args) {
    if (!SystemTray.isSupported()) {
        return;/*from  w ww . ja  va2  s.com*/
    }
    SystemTray tray = SystemTray.getSystemTray();

    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);
    g.setColor(Color.yellow);
    int ovalSize = (size.width < size.height) ? size.width : size.height;
    ovalSize /= 2;
    g.fillOval(size.width / 4, size.height / 4, ovalSize, ovalSize);

    try {
        PopupMenu popup = new PopupMenu();
        MenuItem miExit = new MenuItem("Exit");
        ActionListener al;
        al = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Goodbye");
                System.exit(0);
            }
        };
        miExit.addActionListener(al);
        popup.add(miExit);

        TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2", popup);

        al = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(e.getActionCommand());
            }
        };
        ti.setActionCommand("My Icon");
        ti.addActionListener(al);

        MouseListener ml;
        ml = new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                System.out.println("Tray icon: Mouse clicked");
            }

            public void mouseEntered(MouseEvent e) {
                System.out.println("Tray icon: Mouse entered");
            }

            public void mouseExited(MouseEvent e) {
                System.out.println("Tray icon: Mouse exited");
            }

            public void mousePressed(MouseEvent e) {
                System.out.println("Tray icon: Mouse pressed");
            }

            public void mouseReleased(MouseEvent e) {
                System.out.println("Tray icon: Mouse released");
            }
        };
        ti.addMouseListener(ml);

        MouseMotionListener mml;
        mml = new MouseMotionListener() {
            public void mouseDragged(MouseEvent e) {
                System.out.println("Tray icon: Mouse dragged");
            }

            public void mouseMoved(MouseEvent e) {
                System.out.println("Tray icon: Mouse moved");
            }
        };
        ti.addMouseMotionListener(mml);

        tray.add(ti);
    } catch (AWTException e) {
        System.out.println(e.getMessage());
        return;
    }
}

From source file:Main.java

public static void main(String[] args) {
    JTextArea textArea = new JTextArea(5, 30);
    textArea.setOpaque(false);//from ww  w  .j  av  a  2 s .co  m

    JViewport viewport = new JViewport() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = this.getWidth();
            int h = this.getHeight();
            g.setColor(Color.RED);
            g.fillRect(0, 0, w / 2, h / 2);
        }
    };

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewport(viewport);
    scrollPane.setViewportView(textArea);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane);
    frame.setLocationByPlatform(true);
    frame.pack();
    frame.setVisible(true);
}

From source file:FullScreen.java

public static void main(String args[]) {
    GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
    DisplayMode originalDisplayMode = graphicsDevice.getDisplayMode();

    try {//from www  . ja  va2  s .  com
        Frame frame = new Frame();
        frame.setUndecorated(true);
        frame.setIgnoreRepaint(true);
        graphicsDevice.setFullScreenWindow(frame);
        if (graphicsDevice.isDisplayChangeSupported()) {
            graphicsDevice.setDisplayMode(getBestDisplayMode(graphicsDevice));
        }
        frame.createBufferStrategy(2); // 2 buffers
        Rectangle bounds = frame.getBounds();
        BufferStrategy bufferStrategy = frame.getBufferStrategy();
        while (!done()) {
            Graphics g = null;
            try {
                g = bufferStrategy.getDrawGraphics();
                if ((counter <= 2)) { // 2 buffers
                    g.setColor(Color.CYAN);
                    g.fillRect(0, 0, bounds.width, bounds.height);
                }
                g.setColor(Color.RED);
                // redraw prior line, too, since 2 buffers
                if (counter != 1) {
                    g.drawLine(counter - 1, (counter - 1) * 5, bounds.width, bounds.height);
                }
                g.drawLine(counter, counter * 5, bounds.width, bounds.height);
                bufferStrategy.show();
            } finally {
                if (g != null) {
                    g.dispose();
                }
            }
            try {
                Thread.sleep(250);
            } catch (InterruptedException ignored) {
            }
        }
    } finally {
        graphicsDevice.setDisplayMode(originalDisplayMode);
        graphicsDevice.setFullScreenWindow(null);
    }
    System.exit(0);
}

From source file:org.eclipse.swt.snippets.Snippet361.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 361");
    shell.setText("Translate and Rotate an AWT Image in an SWT GUI");
    shell.setLayout(new GridLayout(8, false));

    Button fileButton = new Button(shell, SWT.PUSH);
    fileButton.setText("&Open Image File");
    fileButton.addSelectionListener(widgetSelectedAdapter(e -> {
        String filename = new FileDialog(shell).open();
        if (filename != null) {
            image = Toolkit.getDefaultToolkit().getImage(filename);
            canvas.repaint();/*  w ww. j  a  va 2 s.c  om*/
        }
    }));

    new Label(shell, SWT.NONE).setText("Translate &X by:");
    final Combo translateXCombo = new Combo(shell, SWT.NONE);
    translateXCombo.setItems("0", "image width", "image height", "100", "200");
    translateXCombo.select(0);
    translateXCombo.addModifyListener(e -> {
        translateX = numericValue(translateXCombo);
        canvas.repaint();
    });

    new Label(shell, SWT.NONE).setText("Translate &Y by:");
    final Combo translateYCombo = new Combo(shell, SWT.NONE);
    translateYCombo.setItems("0", "image width", "image height", "100", "200");
    translateYCombo.select(0);
    translateYCombo.addModifyListener(e -> {
        translateY = numericValue(translateYCombo);
        canvas.repaint();
    });

    new Label(shell, SWT.NONE).setText("&Rotate by:");
    final Combo rotateCombo = new Combo(shell, SWT.NONE);
    rotateCombo.setItems("0", "Pi", "Pi/2", "Pi/4", "Pi/8");
    rotateCombo.select(0);
    rotateCombo.addModifyListener(e -> {
        rotate = numericValue(rotateCombo);
        canvas.repaint();
    });

    Button printButton = new Button(shell, SWT.PUSH);
    printButton.setText("&Print Image");
    printButton.addSelectionListener(widgetSelectedAdapter(e -> {
        performPrintAction(display, shell);
    }));

    composite = new Composite(shell, SWT.EMBEDDED | SWT.BORDER);
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 8, 1);
    data.widthHint = 640;
    data.heightHint = 480;
    composite.setLayoutData(data);
    Frame frame = SWT_AWT.new_Frame(composite);
    canvas = new Canvas() {
        @Override
        public void paint(Graphics g) {
            if (image != null) {
                g.setColor(Color.WHITE);
                g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());

                /* Use Java2D here to modify the image as desired. */
                Graphics2D g2d = (Graphics2D) g;
                AffineTransform t = new AffineTransform();
                t.translate(translateX, translateY);
                t.rotate(rotate);
                g2d.setTransform(t);
                /*------------*/

                g.drawImage(image, 0, 0, this);
            }
        }
    };
    frame.add(canvas);
    composite.getAccessible().addAccessibleListener(new AccessibleAdapter() {
        @Override
        public void getName(AccessibleEvent e) {
            e.result = "Image drawn in AWT Canvas";
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Main.java

public static void main(String[] args) {
    String s = "The quick brown fox jumps over the lazy dog!";
    BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    FontMetrics fm = g.getFontMetrics();
    Rectangle2D b = fm.getStringBounds(s, g);
    System.out.println(b);//from ww w .  java2  s. c  o m
    bi = new BufferedImage((int) b.getWidth(), (int) (b.getHeight() + fm.getDescent()),
            BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.drawString(s, 0, (int) b.getHeight());

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));

    // Technique 3 - JLabel
    JLabel l = new JLabel(s);
    l.setSize(l.getPreferredSize());
    bi = new BufferedImage(l.getWidth(), l.getHeight(), BufferedImage.TYPE_INT_RGB);
    g = bi.getGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, 400, 100);
    l.paint(g);

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
}

From source file:Main.java

/**
 * Fills a circle with the specified bounds. This is used instead of  
 * {@link Graphics#fillRect(int, int, int, int)} due to anti-aliasing 
 * issues.//from   w  w w. jav a 2  s. com
 * 
 * @param g the graphics instance to use for painting
 * @param x the x position for the circle
 * @param y the y position for the circle
 * @param width the width of the circle
 * @param height the height of the circle
 */
public static void fillCircle(Graphics g, int x, int y, int width, int height) {
    g.fillRect(x + 1, y + 1, width - 1, height - 1);
    g.drawLine(x + 1, y, x + width - 1, y);
    g.drawLine(x + width, y + 1, x + width, y + height - 1);
    g.drawLine(x + 1, y + height, x + width - 1, y + height);
    g.drawLine(x, y + 1, x, y + height - 1);
}

From source file:Main.java

public static ImageIcon createImage() {
    BufferedImage image = new BufferedImage(28, 28, BufferedImage.TYPE_INT_ARGB);
    Graphics g = image.getGraphics();
    g.setColor(Color.RED);/*ww  w. j a va  2 s .  co m*/
    g.fillRect(0, 0, 32, 32);
    g.dispose();
    return new ImageIcon(image);
}

From source file:Main.java

/**
 * Gets a "spacer" pixel for use./* w w w. j av  a2  s  .  com*/
 * @return
 */
public static BufferedImage create1x1Pixel() {
    BufferedImage b = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Graphics g = b.getGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, 2, 2);
    g.dispose();
    return b;
}

From source file:GraphicsUtil.java

public static void drawOptimizedLine(Graphics g, int x1, int y1, int x2, int y2) {
    if (g.getColor().getAlpha() < 255 && (x1 == x2 || y1 == y2))
        g.fillRect(x1 < x2 ? x1 : x2, y1 < y2 ? y1 : y2, Math.abs(x2 - x1) + 1, Math.abs(y2 - y1) + 1);
    else/*  www  .j  a  va 2s .  c  o m*/
        g.drawLine(x1, y1, x2, y2);
}