java - 为什么我不能用这段代码画一个椭圆?

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

 }
}

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

最佳答案

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

尝试添加这样的方法:

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

然后像这样从drawpaintComponent 调用它:

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

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

还有一个编码风格问题:为 selected 使用字符串字面量令人困惑(尽管它确实有效。)我会定义一个枚举。

关于java - 为什么我不能用这段代码画一个椭圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2514328/

相关文章:

android - Android TextView 上的最大行长度

java - Android:使用贝塞尔的数字签名

java - JDK8 - 无法解析类型 java.util.Map$Entry

java - ArrayList 最后一个索引

java - Android:如何从 MTP 读取 Java 应用程序中的文件

Java Swing 运行后重启定时器

java - 我如何调整 jscrollpane 的大小

java - 无换行符和/或垂直方向的 FlowLayout

java - 调用paint()方法时图像闪烁

java - 如何修复java中的 double 问题