Thursday, November 1, 2012

Draw Shape example

Draw Shape example


package javaswing;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaTestSwing {
    
    static JFrameWin jFrameWindow;
    
    public static class MyComponent extends JComponent{

        @Override
        protected void paintComponent(Graphics g) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            Shape shape = new java.awt.geom.Ellipse2D.Float(
                    0, 
                    0, 
                    width, 
                    height);
            
            graphics2D.draw(shape);
        }
    }
    
    public static class JFrameWin extends JFrame{
        public JFrameWin(){
            this.setTitle("java-buddy.blogspot.com");
            this.setSize(300, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
            MyComponent myComponent = new MyComponent();
            this.add(myComponent);
        }
    }

    public static void main(String[] args){
        Runnable doSwingLater = new Runnable(){
            
            @Override
            public void run() {
                jFrameWindow = new JFrameWin();
                jFrameWindow.setVisible(true);
            }
        };
        
        SwingUtilities.invokeLater(doSwingLater);
        
    }

}


No comments:

Post a Comment