简体   繁体   English

JPanel绘制椭圆的问题

[英]JPanel problems with drawing ovals

I've created an application that draws a random number of ovals (with random sizes and colors) with JPanel and JFrame (Java 8 installed in Ubuntu 16.04).我创建了一个应用程序,它使用 JPanel 和 JFrame(安装在 Ubuntu 16.04 中的 Java 8)绘制随机数量的椭圆(具有随机大小和颜色)。 Everything seems to be right... but no oval is draw when I execute the code.一切似乎都是正确的......但是当我执行代码时没有绘制椭圆形。 I receave just a blank screen (JFrame appears, but only blank).我只收到一个空白屏幕(出现 JFrame,但只有空白)。

It have 3 classes: MyOval, DrawPanel and DrawTest.它有 3 个类:MyOval、DrawPanel 和 DrawTest。

MyOval Class我的椭圆类

package drawingovals;

import java.awt.Color;
import java.awt.Graphics;

public class MyOval {
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private Color myColor;
    private boolean isFilled;

    public MyOval(int x1, int y1, int x2, int y2, Color color, boolean isFilled) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.x2 = x2;
        myColor = color;
        this.isFilled = isFilled;
    }

    public int getUpperLeftX(){
        return x1;
    }

    public int getUpperLeftY(){
        return y1;
    }

    public int getWidth(){
        return x2 - x1;
    }

    public int getHeight() {
        return y2 - y1;
    }

    public String getColor() {
        return myColor.toString();
    }

    public boolean getFilling(){
        return isFilled;
    }

    public void setUpperLeftX(int value) {
        x1 = value;
    }

    public void setUpperLeftY(int value) {
        y1 = value;
    }

    public void setDownRightX(int value) {
        x2 = value;
    }

    public void setDownRightY(int value) {
        y2 = value;
    }

    public void drawNoFill(Graphics g) {
        g.setColor(myColor);
        g.drawOval(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight());
    }

    public void drawFill(Graphics g) {
        g.setColor(myColor);
        g.fillOval(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight());
    }
}

DrawPanel Class DrawPanel 类

package drawingovals;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;

public class DrawPanel extends JPanel{
    private Random randomNumbers = new Random();
    private MyOval[] ovals;

    public DrawPanel() {
        setBackground(Color.WHITE);

        ovals = new MyOval[5 + randomNumbers.nextInt(5)];

        for (int count = 0; count < ovals.length; count++) {
            int x1 = randomNumbers.nextInt(300);
            int y1 = randomNumbers.nextInt(300);
            int x2 = randomNumbers.nextInt(300);
            int y2 = randomNumbers.nextInt(300);

            Color color = new Color(randomNumbers.nextInt(256),
                    randomNumbers.nextInt(256), randomNumbers.nextInt(256));

            boolean fill = randomNumbers.nextBoolean();

            ovals[count] = new MyOval(x1, y1, x2, y2, color, fill);
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int count = 0; count < ovals.length - 1; count++) {
            ovals[count].drawFill(g);
        }
    }
}

DrawTest Class绘制测试类

package drawingovals;

import javax.swing.JFrame;

public class DrawTest {

    public static void main(String[] args) {
        DrawPanel panel = new DrawPanel();
        JFrame application = new JFrame();

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);
        application.setSize(400, 400);
        application.setVisible(true);
    }

}

You need to take care that your oval heights and widths contain only positive values -- your code does not do this.您需要注意椭圆的高度和宽度仅包含正值——您的代码不会这样做。 Consider using Math.max, Math.min, or Math.abs when obtaining your oval properties to help you achieve this.在获取椭圆属性以帮助您实现这一目标时,请考虑使用 Math.max、Math.min 或 Math.abs。

Also, this is wrong as you're duplicating setting x2 bug ignoring y2:此外,这是错误的,因为您正在复制忽略 y2 的设置 x2 错误:

    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;  // *** these are the same
    this.x2 = x2;  // *** these are the same

Something like this would work:像这样的事情会起作用:

public MyOval(int x1, int y1, int x2, int y2, Color color, boolean isFilled) {
    this.x1 = Math.min(x1, x2);
    this.y1 = Math.min(y1, y2);
    this.x2 = Math.max(x1, x2);
    this.y2 = Math.max(y1, y2);;
    myColor = color;
    this.isFilled = isFilled;
}

In the future, the key to solving these types of issues is to use a debugger to check the fields of your program, here the oval properties, as the program runs, and then see if they don't make sense, and then why.将来,解决这些类型问题的关键是使用调试器检查程序的字段,这里是椭圆属性,程序运行时,然后查看它们是否没有意义,然后是为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM