【问题标题】:Midpoint Ellipse Algorithm in JavaJava中的中点椭圆算法
【发布时间】:2014-10-21 23:30:10
【问题描述】:

我正在尝试编写一个程序以允许用户通过单击来绘制椭圆。用户先左键选择半径,再右击选择水平半径,再右击选择垂直半径。点击后什么都没有绘制。我不明白错误在哪里。

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) {}
}

【问题讨论】:

  • 你考虑过调试它吗?
  • 步骤调试器的创建是有原因的......

标签: java graphics ellipse


【解决方案1】:

三样东西马上跳出来……

  1. 在进行任何自定义绘画之前,您应该调用super.paintComponentpaintComponent 不需要成为public),请参阅Performing Custom PaintingPainting in AWT and Swing 了解更多详情
  2. 永远不要维护对图形上下文的引用,您想要绘制某些东西,向重绘管理器发出请求,然后等到调用您的绘制方法之一。绘画由重绘管理器控制,重绘可能随时发生,大部分时间在您不知情或没有互动的情况下。
  3. 您永远不会调用 repaint 来请求重绘管理器重绘您的组件...在您的 mousePressed 方法结束时,调用 repaint()...

【讨论】:

  • 好吧,您的 x 和 y 半径值已(据我所知)已正确更新,我唯一能想到的是您渲染它的方式有问题。 ..尝试向它提供您自己的已知值并在其上运行调试器...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多