2014-10-21 42 views
-4

我正在尝试编写一个程序,允许用户使用点击绘制椭圆。用户首先左键单击以选择半径,然后右键单击以选择水平半径,然后再次右键单击以选择垂直半径。点击后没有画任何东西。我不明白错误在哪里。Java中的中点椭圆算法

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Ellipse extends JPanel implements MouseListener{ 

    Graphics P; 

    public Ellipse() 
    { 
     addMouseListener(this); 
    } 

    static int Radius = 0; 
    int CenterX, CenterY, RadiusX, RadiusY; 

    public void paintComponent(Graphics g) 
    { 
     P=g; 
     EllipseMidpoint(CenterX, CenterY, RadiusX, RadiusY); 
    } 

    public void EllipseMidpoint(int Cx, int Cy, int Rx, int Ry) 
    { 
     int Rx2 = Rx * Rx; 
     int Ry2 = Ry * Ry; 
     int twoRx2 = 2 * Rx2; 
     int twoRy2 = 2 * Ry2; 
     int x = 0; 
     int y = Ry; 
     int p; 
     int px= 0; 
     int py = twoRx2 * y; 
     PlotEllipsePoint(Cx, Cy, x, y); 

     //Region 1 
     p = (int)(Ry2 - (Rx2 * Ry) + (0.25 + Rx2)); 
     while (px < py) 
     { 
      x = x + 1; 
      px = twoRy2 + px; 
      if (p < 0) 
      { 
       p = Ry2 + px + p; 
      } 
      else 
      { 
       y = y - 1; 
       py = twoRx2 - py; 
       p = Ry2 + px - py + p; 
      } 
      PlotEllipsePoint(Cx, Cy, x, y); 
     } 

     //Region2 
     p = (int)(Ry2 * (x + 0.5) * (x + 0.5) + Rx2 * (y - 1) * (y - 1) - Rx2 * Ry2); 
     while (y > 0) 
     { 
      y = y - 1; 
      py = twoRx2 - py; 
      if (p > 0) 
      { 
       p = Rx2 - py + p; 
      } 
      else 
      { 
       x = x + 1; 
       px = twoRy2 + px; 
       p = Rx2 + px - py + p; 
      } 
      PlotEllipsePoint(Cx, Cy, x, y);  
     }   
    } 

    public void PlotEllipsePoint(int CX, int CY, int X, int Y) 
    { 
    drawPixel(CX + X, CY + Y); 
    drawPixel(CX - X, CY + Y); 
    drawPixel(CX + X, CY - Y); 
    drawPixel(CX - X, CY - Y); 
    } 

    public void drawPixel(int x, int y) 
    { 
     P.fillOval(x, y, 5, 5); 
    } 

    public void mousePressed(MouseEvent e) 
    { 
     if (e.getButton() == MouseEvent.BUTTON1) 
     { 
      CenterX = e.getX(); 
      CenterY = e.getY(); 
     } 
     else if (e.getButton() == MouseEvent.BUTTON3) 
     { 
      Radius = Radius + 1; 
      if (Radius == 1) 
      { 
       RadiusX = (int) Math.pow((Math.pow((e.getX() - CenterX), 2) + Math.pow((e.getY() - CenterY), 2)), 0.5); 
      } 
      else if (Radius == 2) 
      { 
       RadiusY = (int) Math.pow((Math.pow((e.getX() - CenterX), 2) + Math.pow((e.getY() - CenterY), 2)), 0.5); 
      } 
      PlotEllipsePoint(CenterX, CenterY, RadiusX, RadiusY); 
     } 
    } 

    public static void main(String[] args) 
     { 
      JFrame JF = new JFrame("Ellipse"); 
      JF.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
      JF.setSize(500,500); 
      Ellipse E = new Ellipse(); 
      JF.getContentPane().add(E); 
      JF.setVisible(true); 
     } 

    public void mouseClicked(MouseEvent e) {} 
    public void mouseReleased(MouseEvent e) {} 
    public void mouseEntered(MouseEvent e) {} 
    public void mouseExited(MouseEvent e) {} 
} 
+2

你有没有考虑要调试吗? – 2014-10-21 23:31:25

+0

步调试器是由于某种原因创建的... – 2014-10-21 23:33:18

回答

0

有三件事情立即跳出...

  1. 你应该做的任何自定义涂装前被调用super.paintComponent(也没有必要paintComponentpublic),见Performing Custom PaintingPainting in AWT and Swing更多详细信息
  2. 永远不要维护对图形上下文的引用,要绘制某些东西,请向重绘管理器发出请求,然后等待,直到调用其中一种绘画方法。绘画由重画管理员控制,重绘可能随时发生,大部分时间没有你的知识或互动。
  3. 你永远不会调用repaint问重绘管理器重新绘制你的组件......在你mousePressed方法的末尾,调用repaint() ...
+0

那么,你的x和y半径值已被正确更新(尽可能接近),我唯一能想到的就是你的方式有问题渲染它...尝试喂它你拥有已知的值,并运行一个调试器... – MadProgrammer 2014-10-22 00:21:28