【问题标题】:Java changing colour of shapeJava改变形状的颜色
【发布时间】:2014-08-11 00:32:24
【问题描述】:

我正在尝试用 Java 制作一个矩形,完成了。我也可以用纯色填充它,完成。但我想真正改变形状本身的纯色。我知道图形可以使用 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 中进行编码,除了一件事之外一切正常! 每次它说 shapeDraw/shapesFill.add() 它说:

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

如此接近!请回复。

【问题讨论】:

  • getWidth()/getHeight() 应该在 getPreferredSize() 中
  • 我只用过,所以不需要调用 setBounds();在 JPanel 中。它不会改变图形,所以应该没问题。

标签: java swing colors paint graphics2d


【解决方案1】:

你可以使用类似的东西:

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

编辑:根据您的例外情况,您尝试将另一个类(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();
}

【讨论】:

  • 谢谢你的输入,我明白你的意思,但我想要实现的是你可以有不同颜色的单独形状,即 2 个填充三角形,一个红色,一个蓝色,目前会发生的情况是两者都是一种颜色。
  • @Sk4llsRPG:ShapeWrapper shape1 构造为红色三角形。 ShapeWrapper shape2 构造为蓝色三角形。将两者都添加到 List.
  • 现在它告诉我: List 类型中的方法 add(Shape) 不适用于参数 (ShapeWrapper)
  • 您在编辑时似乎使用List&lt;Shape&gt; 而不是List&lt;ShapeWrapper&gt; shapesDraw
  • 不知道为什么我从不接受您的回答...再次感谢,由于您的帮助,该项目变得更好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 2011-11-02
相关资源
最近更新 更多