如何在类和方法之间传递Graphics对象来绘制线

时间:2016-02-29 22:07:30

标签: java swing arraylist

我正在使用mouseListener和mouseMotionListener创建一个gui程序。我有以下Line类

public class Line {

private int x1, x2, y1, y2;
private Color color;

public Line(int x1, int x2, int y1, int y2, Color color)
{
    this.x1 = x1;
    this.x2 = x2;
    this.y1 = y1;
    this.y2 = y2;
    this.color = color;
}

public void draw(Graphics page)
{
    page.drawLine(x1, y1, x2, y2);
    page.setColor(color);
}

}

这是我的鼠标释放,我得到了所需行的最终点。

  public void mouseReleased (MouseEvent event)
  { // ending points
     moving = false;
     Point p2 = event.getPoint();
     x2 = p2.x;
     y2 = p2.y;  
     line = new Line(x1,x2,y1,y2,currentColor);
     lineList.add(line);
     canvas.paintComponent(??????????);

这是canvas方法,它应该在数组列表" lineList"中绘制所有这些行。到画布

private class CanvasPanel extends JPanel
{
  //this method draws all shapes specified by a user
  public void paintComponent(Graphics page)
   {
    super.paintComponent(page);
    setBackground(Color.WHITE);
    for(int i = 0; i <lineList.size()-1;i++)
    {
        line.draw(page);
    }

但是我不知道如何将图形对象传递给canvas类,以便在JPanel上实际绘制我的线条。假设我有所有其他信息正确(初始线点,JPanel设置正确,按钮设置)我如何传递这些实际上使它绘制线到画布。谢谢!

1 个答案:

答案 0 :(得分:2)

不,您不希望在任何地方传递Graphics对象,实际上您不会在MouseListener或MouseMotionListener中进行绘制。而是从这些类中更改字段,调用repaint(),然后在paintComponent方法中使用字段结果。

实际上在你的代码中,如果CanvasPanel可以访问lineList,你需要做的就是在lineList集合中添加一个新行后调用repaint()。而已。

此外,不要在paintComponent中设置背景,而是在构造函数中设置背景。您还需要在Line的draw方法中交换方法调用。您需要在>绘制线之前设置颜色

如,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

@SuppressWarnings("serial")
public class Drawing extends JPanel {
    public static final Color BG = Color.WHITE;
    public static final Color LINE_COLOR = Color.RED;
    public static final Color CURRENT_LINE_COLOR = Color.LIGHT_GRAY;
    public static final int PREF_W = 800;
    public static final int PREF_H = PREF_W;
    private List<Line> lineList = new ArrayList<>();
    private Line currentLine = null;
    private CanvasPanel canvasPanel = new CanvasPanel();

    public Drawing() {
        MyMouse myMouse = new MyMouse();
        canvasPanel.addMouseListener(myMouse);
        canvasPanel.addMouseMotionListener(myMouse);

        setLayout(new BorderLayout());
        add(canvasPanel);
    }

    private class CanvasPanel extends JPanel {
        public CanvasPanel() {
            setBackground(BG);
        }

        public void paintComponent(Graphics page) {
            super.paintComponent(page);
            // setBackground(Color.WHITE); // !! no, not here

            for (Line line : lineList) {
                line.draw(page);
            }

            if (currentLine != null) {
                currentLine.draw(page);
            }
        }

        @Override
        public Dimension getPreferredSize() {
            if (isPreferredSizeSet()) {
                return super.getPreferredSize();
            }
            return new Dimension(PREF_W, PREF_H);
        }
    }

    private class MyMouse extends MouseAdapter {
        private int x1;
        private int y1;

        @Override
        public void mousePressed(MouseEvent e) {
            if (e.getButton() != MouseEvent.BUTTON1) {
                return;
            }
            x1 = e.getX();
            y1 = e.getY();
            currentLine = null;
            canvasPanel.repaint();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.getButton() != MouseEvent.BUTTON1) {
                return;
            }
            Line line = createLine(e, LINE_COLOR);
            lineList.add(line);
            currentLine = null;
            canvasPanel.repaint();
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            currentLine = createLine(e, CURRENT_LINE_COLOR);
            repaint();
        }

        private Line createLine(MouseEvent e, Color currentColor) {
            int x2 = e.getX();
            int y2 = e.getY();
            return new Line(x1, x2, y1, y2, currentColor);
        }


    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Drawing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new Drawing());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }

}

class Line {

    private int x1, x2, y1, y2;
    private Color color;

    public Line(int x1, int x2, int y1, int y2, Color color) {
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
        this.color = color;
    }

    public void draw(Graphics page) {

        // swap these calls!
        page.setColor(color); //!! This first!
        page.drawLine(x1, y1, x2, y2);  // **Then** this
        // !! page.setColor(color);
    }
}