【问题标题】:Java paintComponent as a JComponentJava paintComponent 作为一个 JComponent
【发布时间】:2014-06-26 02:42:41
【问题描述】:

不知道这是否是一个非常具体的标题,但我已经问过这个问题但已经死了。 我正在尝试执行paintComponent(),这样我就可以绘制矩形、三角形等,而该类是一个JComponent。

到目前为止,这是我的代码:

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

如上所示,除了可以选择颜色之外,一切都运行良好。 我希望能够用它们各自的位置和长度来定义矩形和三角形,但也想用它添加颜色。

但我得到一个错误。

错误提示:

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

还有:

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

请帮忙!试图弄清楚这一点让我压力很大,因为它阻碍了我做很多事情。

【问题讨论】:

    标签: java swing list shape paintcomponent


    【解决方案1】:

    答案很简单...Shape 不是ShapeWrapper 的类型,因此不能将其添加到标有List&lt;ShapeWrapper&gt;List

    你应该做什么而不是

    shapesDraw.add(new Rectangle(xPos, yPos, width, height));
    

    更像是……

    shapesDraw.add(new ShapeWrapper(Color.BLACK, new Rectangle(xPos, yPos, width, height)));
    

    ...Triangle 方法也是如此。您需要将生成的 Polygon 包装在 ShapeWrapper 中,然后再尝试将其添加到 List

    【讨论】:

    • 你先生...吻我...它的工作,完美,不能再感谢你了。
    • 除非你买饮料……还有很多饮料……;)很高兴它起作用了;)
    • 哈哈哈,是的。原因是我可以简化我的编码,所以如果我将绘画放在 JComponent 中,它会容易得多,所以我可以直接将它添加到 JPanel 中。再次感谢你。编辑:我想你上周回答了我关于同一堂课的另一个问题:D
    • 是的,这个概念似乎很熟悉;)
    猜你喜欢
    • 1970-01-01
    • 2013-12-21
    • 2015-06-08
    • 2016-02-09
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多