gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:40:20 25 4
gpt4 key购买 nike

我正在制作一个带有 mouseListener 和 mouseMotionListener 的 gui 程序。我有以下线路类

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

这是我的 mouseReleased,我在其中获得所需行的最终点。

  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”中绘制所有这些线。到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com