Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

i wat to draw in image ?

843806Jun 15 2008 — edited Jun 17 2008
in this code i make ovalButton and i want to draw oval in bufferedImage
please see my code and say to me why oal not appear

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class OvalDemo extends JFrame {
   
    DrawOval drawoval=new DrawOval();
    private JPanel panel = new JPanel();
    private JButton ovalButton = new JButton();

    public OvalDemo(){
        
        ovalButton.setText("oval");
        panel.add(ovalButton);
        panel.setPreferredSize(new Dimension(100,50));
        getContentPane().add(panel,BorderLayout.NORTH);
        getContentPane().add(drawoval,BorderLayout.CENTER);
        ovalButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                drawoval.setSHAPE(ShapeEnum.OVAL);
            }
        });
    }
   
    public static void main(String[]args){
        JFrame frame=new OvalDemo();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setSize(500, 300);
    }
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class DrawOval extends JPanel {

    private BufferedImage image;
    private ImageIcon icon;
    private Graphics g;
    private Point p1,p2;
    private  ShapeEnum SHAPE;

    public DrawOval() {
        image = new BufferedImage(1000,1000, BufferedImage.TYPE_INT_RGB);
        g = image.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.setColor(Color.BLACK);
        MymouseAdabter mouse=new MymouseAdabter();
        addMouseListener(mouse);
        
    }

    @Override
public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }

    private class MymouseAdabter extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e){
            p1=e.getPoint();
        }
        public void mouseRelesead(MouseEvent e){
            p2=e.getPoint();
            drawShape();
            repaint();
        }
    }
    
     public ShapeEnum getSHAPE() {
        return SHAPE;
    }
     
    public void setSHAPE(ShapeEnum shapeTodraw) {
        this.SHAPE = shapeTodraw;
    }

   
    public void drawShape(){
        g.setColor(Color.BLACK);
        switch(SHAPE){
            case OVAL:
                int x=Math.min(p1.x,p2.x);
                int y=Math.min(p1.y,p2.y);
                int width=Math.abs(p1.x-p2.x);
                int height=Math.abs(p2.y-p2.y);
                g.drawOval(x, y, width, height);
                break;
          }
    }
    

}
public enum ShapeEnum {
    OVAL
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 15 2008
Added on Jun 15 2008
9 comments
129 views