gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:08:22 25 4
gpt4 key购买 nike

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

油漆类:

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);

}

}

圆类

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

}

CircleRed 类

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. 这是一个好方法还是有更好的解决方案?我想使用一个类,所以请不要给我在 Main 中做所有绘画事情的答案。
  3. 有没有更好的方法来保住这个位置?也许在一个数组中?但是如果我想返回array[position],setter 和 getter 应该是什么样的? ?
  4. 如果我想从 Main 函数设置 Position。我该怎么做?

最佳答案

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));

enter image description here

(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.

这取决于您的目标。我大胆猜测,如果您想练习继承,最好为具有共享代码的通用循环创建一个抽象类,然后使用具体实现和特定代码对其进行子类化。各种类型。否则,您可以只创建一个可自定义的圆类并使用不同的参数实例化该类。

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

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

(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 个位置。一个用于框架中的面板,另一个用于面板中的形状。

对于后者,我会使用 Point或者只是类本身中的 int x, y 字段。 getter 和 setter 是标准的,setter 将控制位置(不过您需要调用 repaint())。

首先,它由布局管理器确定,您不(不应该)以像素完美的方式控制它。您只需用“指南”指示布局管理器,它就会为您进行计算。

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

同样,这取决于您所谈论的职位。见上文。

关于java - 在java中绘制两个不同位置和颜色的圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34656898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com