【问题标题】:Java GUI - Calling draw method from abstract classJava GUI - 从抽象类调用draw方法
【发布时间】: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


【解决方案1】:

我浏览了您的WholePanel 课程,发现以下问题

  1. 您创建了ComboListener 类并且从未使用过它,它应该像这样使用comboShapes.addActionListener(new ComboListener());
  2. 您创建了PointListener 类并且从未使用过它,它应该像这样使用canvas.addMouseListener(new PointListener());

  3. 您使用本地 JComboBox 实例而不是类级别的 JComboBox 实例,因此 ComboListener 类中的 actionPerformed()method 将永远无法工作

  4. ButtonListener 类中的 actionPerformed 方法,没有任何意义,所以你需要重新做逻辑

因此我对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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    相关资源
    最近更新 更多