鼠标绘图应用程序:未显示任何内容

Mouse Drawing Application: Nothing shows up

提问人:stupidmoron 提问时间:4/19/2022 最后编辑:Andrew Thompsonstupidmoron 更新时间:6/29/2022 访问量:33

问:

这是一个逐字逐句的例子,直接出自 Paul 和 Harvey Deitel 所著的《Java How to Program》一书。

PaintPanel.java

// Using class MouseMotionAdapter
import java.awt.Point;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JPanel;

public class PaintPanel extends JPanel {
    private int pointCount = 0;
    // array of 10,000 java.awt.Point references
    private Point[] points = new Point[10000];
    // set up gui and register mouse event handler
    public PaintPanel() {
        addMouseMotionListener(new MouseMotionAdapter() {
            // store drag coordinates and repaint
        
            public void MouseDragged(MouseEvent e) {
                if (pointCount < points.length) {
                    points[pointCount] = e.getPoint();
                    pointCount++;
                    repaint();
                } // end if
            }
        }
        );
    }
    
    // draw ovals in a 4 x 4 bounding box at specified location on the window
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);  // clear drawing area
        g.setColor(Color.BLUE);
        // draw all points in the array
        for(int i = 0; i < pointCount; i++)
            g.fillOval(points[i].x, points[i].y, 4, 4);
    }
}

驱动程序Painter.java

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Painter {
    public static void main(String[] args) {
        JFrame app = new JFrame("A Simple Paint Program");
        PaintPanel pp = new PaintPanel();
        app.add(pp, BorderLayout.CENTER);
        app.add(new JLabel("Drag the rat to draw"), BorderLayout.SOUTH);
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setSize(400, 200);
        app.setVisible(true);
    }
}

当我运行它时,它会显示绘图面板,但当我尝试用鼠标在上面绘图时没有任何反应。所有代码都是从书中逐字复制的。什么给了?

Java Swing jPanel Paint组件

评论


答:

1赞 MadProgrammer 4/19/2022 #1

Java 区分大小写。它是 ,不是 。这就是为什么你应该总是在你“认为”你正在覆盖的方法上使用,这样你就可以获得编译时保护。mouseDraggedMouseDragged@Override

可运行示例...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new PaintPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class PaintPanel extends JPanel {
        private int pointCount = 0;
        // array of 10,000 java.awt.Point references
        private Point[] points = new Point[10000];
        // set up gui and register mouse event handler

        public PaintPanel() {
            addMouseMotionListener(new MouseMotionAdapter() {
                // store drag coordinates and repaint

                @Override
                public void mouseDragged(MouseEvent e) {
                    if (pointCount < points.length) {
                        points[pointCount] = e.getPoint();
                        pointCount++;
                        repaint();
                    } // end if
                }
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 200);
        }

        // draw ovals in a 4 x 4 bounding box at specified location on the window
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);  // clear drawing area
            g.setColor(Color.BLUE);
            // draw all points in the array
            for (int i = 0; i < pointCount; i++) {
                g.fillOval(points[i].x, points[i].y, 4, 4);
            }
        }
    }
}

评论

0赞 stupidmoron 4/19/2022
非常感谢您的帮助!难怪程序员在小组中工作。总是需要第二双新鲜的眼睛才能看到问题,因为我们总是对自己的嘘声视而不见!
0赞 MadProgrammer 4/19/2022
@stupidmoron 欢迎来到森林,留意树木 😉