Thursday, January 2, 2014

Draw something on JFrame


package javaswingdrawing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaSwingDrawing extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGUI();
        });
    }

    @Override
    public void paint(Graphics g) {
        int w = getWidth();
        int h = getHeight();
        g.setColor(Color.red);
        g.fillOval(w/4, h/4, w/2, h/2);
        g.setColor(Color.blue);
        g.fillRect(w/2, h/2, w/4, h/4);
        //super.paint(g);
    }

    private static void createAndShowGUI() {
        JavaSwingDrawing myFrame = new JavaSwingDrawing();
        myFrame.setTitle("java-buddy.blogspot.com");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setPreferredSize(new Dimension(400, 300));

        myFrame.pack();
        myFrame.setVisible(true);
    }
}


No comments:

Post a Comment