在 java 中绘制两个不同位置和颜色的圆圈

Drawing two circles with different positions and colors in java

我是 Java 的新手,我想用它尝试一些图形化的东西。我想生成两个具有两种不同颜色和不同位置的圆圈。我的代码:

绘画Class:

package de.test.pkg;
import javax.swing.*;

public class paint {

    public static void main(String[] args) throws Exception{

        JFrame frame = new JFrame("Titel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Circle d = new Circle();
        Circle r = new CircleRed();
        frame.add(d);
        frame.add(r);
        frame.setSize(600,200);
        frame.setVisible(true);

    }

}

圆形class

package de.test.pkg;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;


public class Circle extends JPanel {

    private double iconRadius = 100;
    private Color defaultColor = new Color(89,104,99); 
    private int positionX = 0;
    private int positionY = 0;

    private Ellipse2D iconBody = new Ellipse2D.Double(getPositionX(),getPositionY(),iconRadius,iconRadius);

    public Icon(){

    }

    public Color getDefaultColor() {
        return defaultColor;
    }

    public void setDefaultColor(Color defaultColor) {
        this.defaultColor = defaultColor;
    }

    public int getPositionX() {
        return positionX;
    }

    public void setPositionX(int positionX) {
        this.positionX = positionX;
    }

    public int getPositionY() {
        return positionY;
    }

    public void setPositionY(int positionY) {
        this.positionY = positionY;
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        super.paintComponent(g2d);
        this.setBackground(new Color(255,255,255));

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setPaint(getDefaultColor());
        g2d.draw(iconBody);
        g2d.fill(iconBody);
    }

}

红圈class

package de.design.pkg;
import java.awt.Color;

public class CircleRed extends Circle {

private Color defaultColor = Color.RED;
private int positionX = 120;
private int positionY = 0;

public CircleRed() {
}

public Color getDefaultColor() {
    return defaultColor;
}

public void setDefaultColor(Color defaultColor) {
    this.defaultColor = defaultColor;
} 

public int getPositionX() {
    return positionX;
}

public void setPositionX(int positionX) {
    this.positionX = positionX;
}

public int getPositionY() {
    return positionY;
}

public void setPositionY(int positionY) {
    this.positionY = positionY;
    }

}

结果是圆的位置相同。

我的问题是:

  1. 为什么他们的位置相同?
  2. 这是一个很好的方法还是有更好的解决方案?我想使用 class 所以请不要在 Main 中做所有的绘画事情给我答案。
  3. 有没有更好的持仓方式。也许在一个数组中?但是,如果我想 return array[position],setter 和 getter 应该是什么样子?
  4. 如果我想在Main函数中设置Position。我该怎么做?

你做的只是创建两个彩色圆圈,这太过分了。您可以只使用 java.awt

中的 paint 方法
public void paint(Graphics g){ 

       g.setColor(Color.YELLOW);
       g.fillOval(20,20,160,160);

       g.setColor(Color.RED);
       g.fillOval(60,60,80,80);
    } 

fillOval 采用以下参数(int x、int y、int width、int height) 您可以使用 g.setColor(Color.NAME) 更改圆圈的颜色。只需在绘制调用之前调用此方法即可。

  1. 它们处于同一位置,因为 Circle 中的 paintComponent() 方法被用于两者 - 并且它使用了 Circle 中定义的位置变量。当您让 CircleRed 扩展 Circle 时,您不需要再次定义位置变量——您从 Circle 继承它们。相反,调用 CircleRed 上的 setPosition() 方法。 (你也不需要定义这些,它们也是继承的。)

  2. 有多种更好的解决方案,我认为最大的改进是改进继承的使用。

  3. 这是保持位置的好方法。如果还有其他问题,您还可以使用 Point 对象。 (已经在 Java)

  4. 要从主函数设置位置,例如调用

    圆c = new Circle(); c.setPositionX(120); c.setPositionY(40);

你走错路了。您可以通过覆盖 JFrame class 的 paint 方法简单地绘制两个圆。

这是一个示例演示程序。

import java.awt.*;

/**
 *
 * @author Pankaj
 */
public class TestFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public TestFrame() {
        initComponents();
        setTitle("Graphics Demo");
        setSize(200,200);
    }

    /**
     * You need to override this method.
     */
    @Override
    public void paint(Graphics g) {
        int X1 = 10; //X coordinate of first circle
        int X2 = 60; //X coordinate of second circle
        int Y1 = 100; //Y coordinate of first circle
        int Y2 = 100; //Y coordinate of second circle
        int width = 50; //Width of the circle
        int height = 50; //Height of the circle 

        Color color1 = Color.RED; //Color of first circle
        Color color2 = Color.BLUE; //Color of second circle

        g.setColor(color1);
        g.fillOval(X1, Y1, width, height);

        g.setColor(color2);
        g.fillOval(X2, Y2, width, height);
    }

    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        pack();
    }                       

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestFrame().setVisible(true);
            }
        });
    }                 
}

这是输出

The Result is that the Circles have the same positions.

(1) Why do they have the same positions?

不是真的。结果是只显示CircleRed。您的问题不在于绘画,而在于使用合适的布局管理器将组件添加到容器中。行数

Circle d = new Circle();
Circle r = new CircleRed();
frame.add(d);
frame.add(r);

添加 r 而不是 d。这是因为 JFrame 默认使用 BorderLayout 并且您正在将中心组件 d 替换为 r 之后的行。只是为了说明这一点,添加行

frame.setLayout(new GridLayout(1, 2));

(2) Is this a good way to do that or are there better solutions? I want to use a class so please don't gave me answers with do all that paint thing in the Main.

这取决于你打算做什么。我斗胆猜测一下,如果你想实践继承,你最好先创建一个abstract class一个通用的共享代码的圈子,然后再subclass具体的实现和具体的代码各种类型。否则,您可以只创建一个可自定义的圆圈 class 并使用不同的参数实例化该圆圈。

在任何情况下这都不是一个好的实用方法,因为圆圈(JPanels)的位置将由布局管理器决定。绘画只决定了绘画形状在面板中的位置。最好只在一个大面板上绘制形状,而不是使用多个面板。

站点上有一些关于移动组件的非常好的答案。

(3) Is there a better way to hold the position. Maybe in an array? But how should the setter and getter look like if i want to return array[position]?

您的设计中实际上有 2 个位置。一个用于框架中的面板,另一个用于面板中的形状。

对于后者,我会在 class 本身中使用 Pointint x, y 字段。 getter 和 setter 是标准的,setter 将控制位置(不过你需要调用 repaint())。

首先,它由布局管理器决定,您不(不应该)以 pixel-prefect 方式控制它。您只需使用“准则”指示布局管理器,它就会为您进行计算。

(4) If I want to set the Position from the Main function. How can i do this?

同样,取决于你说的是哪个职位。见上文。