为什么我不能使用此代码绘制椭圆?

时间:2010-03-25 09:33:05

标签: java swing paint

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();

 }
}

我需要创建一个绘图应用程序。当我通过从左向右拖动鼠标绘制椭圆时,它什么也没显示。为什么?我应该在这里使用任何其他功能吗?

1 个答案:

答案 0 :(得分:7)

当您将鼠标向下并向右拖动时,您的程序绘制一个椭圆。它向上拖动和/或向左拖动不起作用,因为Graphics.drawOval不适用于负宽度或高度。

尝试添加如下方法:

private Shape createEllipse() {
    Ellipse2D e = new Ellipse2D.Double();
    e.setFrameFromDiagonal(start, end);
    return e;
}

然后从drawpaintComponent这样调用它:

if(selected=="elipse") {
    g2.draw(createEllipse());
}

此外,您可能不需要在draw()结束时拨打actionPerformed。如果在直线和椭圆模式之间切换,它将绘制一个椭圆,其坐标与最近的线相同,反之亦然。

一个编码风格问题:对selected使用字符串文字会让人感到困惑(虽然它确实有用。)我会定义一个枚举。