Thursday, November 8, 2012

Draw Shape example: drawOval() and Ellipse2D.Double()

drawOval() and Ellipse2D.Double()


package javaswing;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
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();
            
            float x1 = width/4;
            float y1 = height/4;
            float x2 = width/4 + width/2;
            float y2 = height/4 + height/2;
            Color color1 = Color.RED;
            Color color2 = Color.BLUE;

            g.drawOval(0, 0, width/2, height/2);
            Ellipse2D.Double ellipse2D
                    = new Ellipse2D.Double(width/2, height/2, width/2, height/2);
            graphics2D.draw(ellipse2D);

        }
    }
    
    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