[英]Java GUI - Calling draw method from abstract class
My compiled program runs with no errors but when I click inside the canvas no shapes are generated/painted. 我的编译程序运行没有错误,但是当我在画布内单击时,没有生成/绘制任何形状。 compiled program PaintComponent Class
编译程序 PaintComponent类
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);
}
}
}
Pointer listener class. 指针侦听器类。
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();
}
Abstract Shape class 抽象形状类
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) {
}
}
So ideally the user clicks on the canvas with their selected options, the click generates the shape object and stores in a list. 因此,理想情况下,用户单击带有选定选项的画布,该单击将生成形状对象并存储在列表中。 The paint component cycles through the whole list and calls the shape class draw method which pulls from my Square/Circle child classes and draws their selected shape.
绘制组件在整个列表中循环,并调用shape类的draw方法,该方法从我的Square / Circle子类中提取并绘制其选定的形状。 Unfortunately nothing is happening.
不幸的是,什么都没有发生。 Is something missing?
缺少什么吗? Thanks!
谢谢!
Here is a repo containing all of my project. 这是一个包含我所有项目的仓库。 https://github.com/Purpleshoes/assignment7
https://github.com/Purpleshoes/assignment7
I went through with your WholePanel
class and found below issues 我完成了您的
WholePanel
类,发现了以下问题
ComboListener
class and never used that , it should use like this comboShapes.addActionListener(new ComboListener());
ComboListener
类,并且从未使用过它,它应该像这样使用comboShapes.addActionListener(new ComboListener());
You created PointListener
class and never used that , it should use like this canvas.addMouseListener(new PointListener());
您创建了
PointListener
类,并且从未使用过它,它应该像canvas.addMouseListener(new PointListener());
You used local JComboBox
instances instead of class level JComboBox
instances , therefore actionPerformed()
method in ComboListener
class will never work 您使用本地
JComboBox
实例而不是类级别的JComboBox
实例,因此ComboListener
类中的actionPerformed()
方法将永远无法工作
actionPerformed
method in ButtonListener
class , doesn't make any sense , so u need to re-do the logic ButtonListener
类中的actionPerformed
方法没有任何意义,因此您需要重新执行逻辑 Therefore i made few changes to WholePanel
class , see below 因此,我对
WholePanel
类进行了一些更改,请参见下文
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
public class WholePanel extends JPanel {
private Color currentColor = Color.black;
private CanvasPanel canvas;
private JPanel topPanel;
private JButton undo;
private ArrayList shapeList;
int flag = 1; //Debug
private String currentShape = "circle";
private int currentSize = 10;
private ArrayList pointList;
Shape nShape;
JComboBox<String> comboShapes = new JComboBox<String>();
JComboBox<Integer> comboSize = new JComboBox<Integer>();
JComboBox<String> comboColors = new JComboBox<String>();
public WholePanel() {
currentColor = Color.black;
shapeList = new ArrayList();
pointList = new ArrayList();
topPanel = new JPanel();
canvas = new CanvasPanel();
canvas.addMouseListener(new PointListener());
JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel, canvas);
setLayout(new BorderLayout());
add(sp);
undo = new JButton("Undo");
comboShapes = new JComboBox<String>();
comboShapes.addItem("circle");
comboShapes.addItem("square");
comboShapes.addActionListener(new ComboListener());
comboSize = new JComboBox<Integer>();
comboSize.addItem(10);
comboSize.addItem(20);
comboSize.addItem(30);
comboSize.addItem(40);
comboSize.addItem(50);
comboSize.addActionListener(new ComboListener());
comboColors = new JComboBox<String>();
comboColors.addItem("Black");
comboColors.addItem("Red");
comboColors.addItem("Blue");
comboColors.addItem("Green");
comboColors.addItem("Orange");
comboColors.addActionListener(new ComboListener());
topPanel.add(comboShapes, BorderLayout.WEST);
topPanel.add(comboSize, BorderLayout.CENTER);
topPanel.add(comboColors, BorderLayout.EAST);
topPanel.add(undo, BorderLayout.EAST);
}
private class CanvasPanel extends JPanel {
//this method draws all shapes
public void paintComponent(Graphics page) {
super.paintComponent(page);
setBackground(Color.WHITE);
// page.setColor(nShape.color);
// page.drawOval(nShape.x, nShape.y, nShape.width, nShape.height);
// page.fillOval(nShape.x, nShape.y, nShape.width, nShape.height);
for (int i = 0; i < shapeList.size(); i++) {
//Point drawPoint = (Point) pointList.get(i);
((Shape) shapeList.get(i)).draw(page);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
setBackground(Color.WHITE);
//page.setColor(nShape.color);
//page.drawOval(nShape.x, nShape.y, nShape.width, nShape.height);
//page.fillOval(nShape.x, nShape.y, nShape.width, nShape.height);
for (int i = 0; i < shapeList.size(); i++) {
//Point drawPoint = (Point) pointList.get(i);
((Shape) shapeList.get(i)).draw(g);
}
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
undo = new JButton("Undo");
undo.addActionListener(new ButtonListener());
}
} // end of ButtonListener
// listener class to set the color chosen by a user using
// color combo box, set the size chosen using size combo box
// or set the shape (circle or square) using shape combo box
private class ComboListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
System.out.println("action action");
if (event.getSource() == comboShapes) {
currentShape = (String) comboShapes.getSelectedItem();
}
if (event.getSource() == comboSize) {
currentSize = (int) comboSize.getSelectedItem();
}
if (event.getSource() == comboColors) {
currentColor = Color.getColor((String) comboColors.getSelectedItem());
}
}
} // end of ComboListener
// listener class that listens to the mouse
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 void mouseReleased(MouseEvent event) {
}
public void mouseClicked(MouseEvent event) {
}
public void mouseEntered(MouseEvent event) {
}
public void mouseExited(MouseEvent event) {
}
} // end of PointListener
} // end of Whole Panel Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.