连接两个班[...]

时间:2014-06-01 12:53:39

标签: java swing repaint

我在连接两个课时遇到了问题:

这是在JPanel上进行基本操作的代码:

public class Drawing extends JPanel {

    Pencil pencil;
    Tool lines;
    Tool paintbrush;

    public Drawing() {
        setBackground(Color.WHITE);
        pencil = new Pencil();
        addMouseListener(pencil);
        addMouseMotionListener(pencil);
        addMouseListener(lines);
        addMouseMotionListener(lines);
        addMouseListener(paintbrush);
        addMouseMotionListener(paintbrush);
        this.repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        pencil.paintComponent(g);
        super.paintComponent(g);
    }
}

这是一个名为Pencil的特定工具,用于在JPanel上面绘制:

public class Pencil implements MouseListener, MouseMotionListener {

    private int x, y;
    private ArrayList<ArrayList<Point>> pointsList = new ArrayList<ArrayList<Point>>();
    private ArrayList<Color> Colors = new ArrayList<Color>();

    public Pencil() { 
        System.out.println("PENCIL SELECTED!"); 
    }

    public void paintComponent(Graphics g) {
        // super.paintComponent(g);
         ((Graphics2D) g).setStroke(new BasicStroke(2));
            for(int i=0; i<pointsList.size(); i++) {
                ArrayList<Point> Points = pointsList.get(i);
                Color pencilColor = Colors.get(i);
                g.setColor(pencilColor);
                    for(int j=0; j<Points.size()-1; j++) {
                        Point p1, p2 = null;
                        p1 = Points.get(j);
                        p2 = Points.get(j+1);
                        g.drawLine(p1.x, p1.y, p2.x, p2.y);
                    }
            }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        ArrayList<Point> Points = new ArrayList<Point>();
        Color pencilColor = GUI.getColour();
        Colors.add(pencilColor);
        pointsList.add(Points);
        x = e.getX();
        y = e.getY();
        pointsList.get(pointsList.size()-1).add(new Point(x, y));
        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        pointsList.get(pointsList.size()-1).add(new Point(x, y));
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        pointsList.get(pointsList.size()-1).add(new Point(x, y));
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {}
    @Override
    public void mouseClicked(MouseEvent e) {}
    @Override
    public void mouseEntered(MouseEvent e) {}
    @Override
    public void mouseExited(MouseEvent e) {}
}

编译器说,Pencil类没有repaint()方法。

我不知道,如何连接这两个类...... 提前感谢您的帮助! :)

1 个答案:

答案 0 :(得分:2)

正如错误所说,Pencil类没有repaint()方法,并且它不会从超类继承repaint()方法,就像Drawing类那样。

解决此问题的一种方法是将Drawing实例传递给Pencil实例。

像这样:

 public Drawing() {
        setBackground(Color.WHITE);
        pencil = new Pencil(this); // send the instance to `Pencil` by constructor.
        addMouseListener(pencil);
        addMouseMotionListener(pencil);
        addMouseListener(lines);
        addMouseMotionListener(lines);
        addMouseListener(paintbrush);
        addMouseMotionListener(paintbrush);
        this.repaint();
    }

Pencil

private Drawing drawingBoard;
public Pencil(Drawing drawingBoard) { 
    System.out.println("PENCIL SELECTED!"); 
    this.drawingBoard = drawingBoard;
}

现在,当您致电repaint()时,请将其称为:drawingBoard.repaint()