gpt4 book ai didi

Java改变形状的颜色

转载 作者:行者123 更新时间:2023-11-29 05:22:02 25 4
gpt4 key购买 nike

我正在尝试用 Java 制作一个矩形,完成了。我也可以用纯色填充它,完成。但我实际上想改变形状本身的纯色。我知道对于 Graphics 你可以使用 g.setColor();但是我已经用一种特殊的方式设置了我的组件,如下所示:

public class Design extends JComponent {
private static final long serialVersionUID = 1L;

private List<Shape> shapesDraw = new ArrayList<Shape>();
private List<Shape> shapesFill = new ArrayList<Shape>();

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int screenWidth = gd.getDisplayMode().getWidth();
int screenHeight = gd.getDisplayMode().getHeight();

public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for(Shape s : shapesDraw){
g2d.draw(s);
}
for(Shape s : shapesFill){
g2d.fill(s);
}
}

public void drawRect(int xPos, int yPos, int width, int height) {
shapesDraw.add(new Rectangle(xPos, yPos, width, height));
repaint();
}

public void fillRect(int xPos, int yPos, int width, int height) {
shapesFill.add(new Rectangle(xPos, yPos, width, height));
repaint();
}

public void drawTriangle(int leftX, int topX, int rightX, int leftY, int topY, int rightY) {
shapesDraw.add(new Polygon(
new int[]{leftX, topX, rightX},
new int[]{leftY, topY, rightY},
3));
repaint();
}

public void fillTriangle(int leftX, int topX, int rightX, int leftY, int topY, int rightY) {
shapesFill.add(new Polygon(
new int[]{leftX, topX, rightX},
new int[]{leftY, topY, rightY},
3));
repaint();
}

public Dimension getPreferredSize() {
return new Dimension(screenWidth, screenHeight);
}

public int getWidth() {
return screenWidth;
}
public int getHeight() {
return screenHeight;
}

}

如您所见,它不只是绘制和填充,而是使用列表来绘制。有没有办法可以更改列表<形状>中的颜色?我最好希望每个绘图/填充形状内的颜色都可以更改。

感谢您的帮助。

根据回答更新:

我的类如下来自您的 ShapeWrapper 示例:

public class Design extends JComponent {
private static final long serialVersionUID = 1L;

private List<ShapeWrapper> shapesDraw = new ArrayList<ShapeWrapper>();
private List<ShapeWrapper> shapesFill = new ArrayList<ShapeWrapper>();

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int screenWidth = gd.getDisplayMode().getWidth();
int screenHeight = gd.getDisplayMode().getHeight();

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for(ShapeWrapper s : shapesDraw){
g2d.setColor(s.color);
g2d.draw(s.shape);
}
for(ShapeWrapper s : shapesFill){
g2d.setColor(s.color);
g2d.fill(s.shape);
}
}

public void drawRect(int xPos, int yPos, int width, int height) {
shapesDraw.add(new Rectangle(xPos, yPos, width, height));
repaint();
}

public void fillRect(int xPos, int yPos, int width, int height) {
shapesFill.add(new Rectangle(xPos, yPos, width, height));
repaint();
}

public void drawTriangle(int leftX, int topX, int rightX, int leftY, int topY, int rightY) {
shapesDraw.add(new Polygon(
new int[]{leftX, topX, rightX},
new int[]{leftY, topY, rightY},
3));
repaint();
}

public void fillTriangle(int leftX, int topX, int rightX, int leftY, int topY, int rightY) {
shapesFill.add(new Polygon(
new int[]{leftX, topX, rightX},
new int[]{leftY, topY, rightY},
3));
repaint();
}


public Dimension getPreferredSize() {
return new Dimension(getWidth(), getHeight());
}

public int getWidth() {
return screenWidth;
}
public int getHeight() {
return screenHeight;
}

}

class ShapeWrapper {

Color color;
Shape shape;

public ShapeWrapper(Color color , Shape shape){
this.color = color;
this.shape = shape;
}
}

现在我在 Eclipse 中编码,除了一件事之外一切正常!!每次它说 shapesDraw/shapesFill.add() 它说:

类型List中的方法add(ShapeWrapper)不适用于参数(Rectangle)

太近了!请回复。

最佳答案

你可以使用类似的东西:

private class ShapeWrapper {

private Color color;
private Shape shape;

public ShapeWrapper(Color color , Shape shape){
this.color = color;
this.shape = shape;
}
}

代替普通的Shape来存储Shape+Color

然后像下一个一样绘制它们:

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for(ShapeWrapper s: shapesDraw){
g2d.setColor(s.color);
g2d.draw(s.shape);
}
for(ShapeWrappers s : shapesFill){
g2d.setColor(s.color);
g2d.fill(s.shape);
}
}

编辑:根据您的异常(exception)情况,您尝试将另一个类(Shape)的对象添加到类型化列表(ShapeWrapper)中,像接下来那样修复您的方法:

public void drawRect(int xPos, int yPos, int width, int height) {
ShapeWrapper wr = new ShapeWrapper(Color.RED,new Rectangle(xPos, yPos, width, height));
shapesDraw.add(wr);
repaint();
}

关于Java改变形状的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24324066/

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