gpt4 book ai didi

java - 单击 2 次鼠标绘制一条线

转载 作者:行者123 更新时间:2023-12-01 07:35:49 26 4
gpt4 key购买 nike

我试图通过一系列鼠标点击来画一条线。

例如,当我从屏幕上的一个点单击到下一个点时,它会将线条连接起来,然后我可以继续单击到下一个点,它会继续绘制连续的绘图线。

示例如下:http://oneslime.net/java/Tutorial_2练习2

我相信我的逻辑有一些错误(没有画线,只是画一个点),但我似乎找不到它!

有人可以帮我吗?

这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class RoadCreator extends JPanel {

private MouseHandler mouseHandler = new MouseHandler();

private Point previousPoint = new Point();
private Point nextPoint = new Point();
private boolean drawing;

public RoadCreator() {
this.setPreferredSize(new Dimension(640, 480));
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(8,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
g.drawLine(previousPoint.x, previousPoint.y, nextPoint.x, nextPoint.y);
}

private class MouseHandler extends MouseAdapter {

@Override
public void mousePressed(MouseEvent e) {
drawing = true;
nextPoint = e.getPoint();
repaint();
previousPoint = e.getPoint();

}
}

public void display() {
JFrame f = new JFrame("Road Creator");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
}

}

任何帮助将不胜感激,谢谢!

最佳答案

使用 GeneralPathPath2D 代替。

The GeneralPath class represents a geometric path constructed from straight lines, and quadratic and cubic (Bézier) curves. It can contain multiple subpaths.

enter image description here

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.GeneralPath;

public class RoadCreator extends JPanel {

private MouseHandler mouseHandler = new MouseHandler();

GeneralPath path = null;
private boolean drawing = false;

public RoadCreator() {
this.setPreferredSize(new Dimension(320, 200));
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(8,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
if (path!=null) {
g2d.draw(path);
}
}

private class MouseHandler extends MouseAdapter {

@Override
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if (!drawing) {
path = new GeneralPath();
path.moveTo(p.x, p.y);
drawing = true;
} else {
path.lineTo(p.x, p.y);
}

repaint();
}
}

public void display() {
JFrame f = new JFrame("Road Creator");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
}

public static void main(String[] args) {
RoadCreator rc = new RoadCreator();
rc.display();
}
}

关于java - 单击 2 次鼠标绘制一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12257933/

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