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

标签 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(??????????);

这是应该在数组列表“lineList”中绘制所有这些线的 Canvas 方法。到 Canvas

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 设置正确以及按钮设置),我如何传递这些信息才能真正将线条绘制到 Canvas 上。谢谢你!

最佳答案

不,您不想在任何地方传递 Graphics 对象,事实上您也不会从 MouseListener 或 MouseMotionListener 中进行绘制。相反,您可以从这些类中更改字段,调用 repaint(),然后在 PaintComponent 方法中使用字段结果。

事实上,在您的代码中,如果 CanvasPanel 可以访问 lineList,您所需要做的就是在将新行添加到 lineList 集合后对其调用 repaint() 。就是这样。

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

例如,

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);
    }
}

关于java - 如何在类和方法之间传递 Graphics 对象来绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35710659/

相关文章:

java - 如何重构/聚合 Java 中 ArrayList 中的数值?

java - 在java程序中修改图形?

java - 如何在android中比较int[]数组和int?

java - Runtime.getRuntime.exec ("who am i") 没有给出预期的结果

java - Maven、GWT 和 Eclipse 项目

java - 修改 DefaultListModel 中的元素不更新模型

更改数据后,Java JTable repaint() 方法不起作用

java - 在另一个函数中声明的 Canvas 上绘图

java - 我想挑选一个从方法扫描的值并获得该方法的 3 个实例的总和

java - 比较 ArrayList 与 boolean 值