【发布时间】:2017-10-11 21:36:35
【问题描述】:
我编译的程序运行时没有错误,但是当我在画布内单击时,没有生成/绘制任何形状。 compiled program 油漆组件类
private class CanvasPanel extends JPanel {
//this method draws all shapes
public void paintComponent(Graphics page)
{
super.paintComponent(page);
setBackground(Color.WHITE);
for (int i = 0; i < shapeList.size(); i++) {
((Shape) shapeList.get(i)).draw(page);
}
}
}
指针监听类。
public class PointListener implements MouseListener
{
public void mousePressed (MouseEvent event)
{
Point pt = event.getPoint();
//pointList.add(event.getPoint());
if (currentShape.equals("circle")) {
nShape = new Circle((int)pt.getX(), (int)pt.getY(), currentSize, currentSize, currentColor);
shapeList.add(nShape);
} else {
nShape = new Square((int)pt.getX(), (int)pt.getY(), currentSize, currentSize, currentColor);
shapeList.add(nShape);
}
repaint();
}
抽象形状类
public abstract class Shape {
protected int x;
protected int y;
protected int width;
protected int height;
protected Color color;
public Shape (int x1, int y1, int width, int height, Color color) {
this.x = x1;
this.y = y1;
this.width = width;
this.height = height;
}
public void draw (Graphics page) {
}
}
因此,理想情况下,用户使用他们选择的选项在画布上单击,单击会生成形状对象并存储在列表中。绘制组件循环遍历整个列表并调用形状类绘制方法,该方法从我的 Square/Circle 子类中提取并绘制它们选择的形状。不幸的是,什么都没有发生。有什么遗漏吗? 谢谢!
这是一个包含我所有项目的仓库。 https://github.com/Purpleshoes/assignment7
【问题讨论】:
-
"Is something missing?"-- 你的minimal reproducible example,我们可以编译和运行的代码,它说明了问题。请注意,我们不是要求您提供整个代码,而是一个已提取的子集,它小到可以在此处完整发布,并且可以为我们编译和运行。请看链接。 -
比如shapeList保存在哪里?有多少个 shapeList?一切是如何联系起来的?
-
PointListener的信息如何与CanvasPanel共享?
标签: java swing draw paintcomponent