Why cant i draw an elipse in with code?

Posted by bvivek88 on Stack Overflow See other posts from Stack Overflow or by bvivek88
Published on 2010-03-25T09:33:05Z Indexed on 2010/03/25 9:33 UTC
Read the original article Hit count: 344

Filed under:
|
|
 package test;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class test_bmp extends JPanel implements MouseListener,MouseMotionListener,ActionListener
{
 static BufferedImage image;
 Color color;
 Point start=new Point();
 Point end =new Point();
 JButton elipse=new JButton("Elipse");
 JButton rectangle=new JButton("Rectangle");
 JButton line=new JButton("Line");
 String selected;
 public test_bmp()
    {
  color = Color.black; 
  setBorder(BorderFactory.createLineBorder(Color.black));   
  addMouseListener(this);
  addMouseMotionListener(this);
    }
 public void paintComponent(Graphics g) 
 {
  //super.paintComponent(g);
  g.drawImage(image, 0, 0, this);
  Graphics2D g2 = (Graphics2D)g;
  g2.setPaint(Color.black);
  if(selected=="elipse")
        {
         g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y));
         System.out.println("Start : "+start.x+","+start.y);
         System.out.println("End   : "+end.x+","+end.y);
        }
        if(selected=="line")
         g2.drawLine(start.x,start.y,end.x,end.y);
 }
 //Draw on Buffered image
 public void draw()
    {
        Graphics2D g2 = image.createGraphics();
        g2.setPaint(color);
      System.out.println("draw");
        if(selected=="line")
         g2.drawLine(start.x, start.y, end.x, end.y);
        if(selected=="elipse")
        {
         g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y));
            System.out.println("Start : "+start.x+","+start.y);
         System.out.println("End   : "+end.x+","+end.y);
        }
        repaint();
        g2.dispose();
        }  
 public JPanel addButtons()
 {
  JPanel buttonpanel=new JPanel();
  buttonpanel.setBackground(color.lightGray);
  buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS));
  elipse.addActionListener(this);
  rectangle.addActionListener(this);
  line.addActionListener(this);
  buttonpanel.add(elipse);
  buttonpanel.add(Box.createRigidArea(new Dimension(15,15)));
  buttonpanel.add(rectangle);
  buttonpanel.add(Box.createRigidArea(new Dimension(15,15)));
  buttonpanel.add(line);
  return buttonpanel;
 }
 public static void main(String args[]) 
 {
   test_bmp application=new test_bmp();
   //Main window
   JFrame frame=new JFrame("Whiteboard");   
   frame.setLayout(new BorderLayout());
   frame.add(application.addButtons(),BorderLayout.WEST);
   frame.add(application);
   //size of the window
   frame.setSize(600,400);
   frame.setLocation(0,0);
   frame.setVisible(true);
   int w = frame.getWidth();
      int h = frame.getHeight();
      image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2 = image.createGraphics();
      g2.setPaint(Color.white);
      g2.fillRect(0,0,w,h);
      g2.dispose();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 @Override
 public void mouseClicked(MouseEvent arg0) {
  // TODO Auto-generated method stub
 }
 @Override
 public void mouseEntered(MouseEvent arg0) {
  // TODO Auto-generated method stub
 }
 @Override
 public void mouseExited(MouseEvent arg0) {
  // TODO Auto-generated method stub
 }
 @Override
 public void mousePressed(MouseEvent event) 
 {
  start = event.getPoint();
 }
 @Override
 public void mouseReleased(MouseEvent event) 
 {
  end = event.getPoint();
  draw();
 }
 @Override
 public void mouseDragged(MouseEvent e) 
 {
  end=e.getPoint();
  repaint();
 }
 @Override
 public void mouseMoved(MouseEvent arg0) {
  // TODO Auto-generated method stub

 }
 @Override
 public void actionPerformed(ActionEvent e) 
 {
  if(e.getSource()==elipse)
   selected="elipse";
  if(e.getSource()==line)
   selected="line";
  draw();

 }
}

I need to create a paint application, when i draw elipse by dragging mouse from left to right it displays nothing, why?? should i use any other function here?

© Stack Overflow or respective owner

Related posts about java

Related posts about swing