gpt4 book ai didi

Java - Graphics2D - 鼠标移动后绘制一条线

转载 作者:行者123 更新时间:2023-12-01 13:34:59 25 4
gpt4 key购买 nike

我不知道如何用几句话来表达标题,所以我将解释一下我的情况:我已经设置了一个扩展 JPanel 的类,包括鼠标监听器的所有代码等......我的问题是,一旦用户拖动鼠标,它就不会绘制所需的线,即使坐标按预期工作(将显示我临时设置的控制台输出,只是为了验证坐标是否正确)命令确实有效)。

这是该类的代码:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class Container extends JPanel implements MouseMotionListener{
Graphics2D board;
public void paintComponent(Graphics comp)
{
board = (Graphics2D) comp;
board.setColor(new Color(255,255,255));
Rectangle2D.Double bg = new Rectangle2D.Double(0.0, 0.0, (double) getSize().width, (double) getSize().height);
board.fill(bg);

board.setColor(new Color(0,0,0));

//Line2D.Double test = new Line2D.Double(100,100,150,150);
//board.draw(test); works
}

public Container(String name, String text, String text2)
{
addMouseMotionListener(this);
}

public Container(String filepath)
{
addMouseMotionListener(this);
}

int oldy;
int oldx;
int newy;
int newx;
boolean old;
@Override
public void mouseDragged(MouseEvent e) {
newy = e.getY();
newx = e.getX();

if(old)
{
oldy = newy;
oldx = newx;
}
else
{
old = true;
}

board.setColor(new Color(0,0,0));
Line2D.Double line = new Line2D.Double(10,10,20,20);
board.draw(line);
board.draw(new Line2D.Double((int) oldx, (int) oldy, (int) newx,(int) newy));

System.out.print(oldx + ", " + oldy + ", " + newx + ", " + newy + ".\n");
}

@Override
public void mouseMoved(MouseEvent arg0) {

}

}

这是控制台输出(“oldx,oldy,newx,newy。”):

418, 426, 418, 426.
409, 422, 409, 422.
403, 419, 403, 419.
400, 416, 400, 416.
396, 413, 396, 413.
393, 408, 393, 408.
390, 399, 390, 399.
390, 390, 390, 390.
390, 379, 390, 379.
392, 365, 392, 365.
397, 351, 397, 351.
403, 336, 403, 336.
410, 318, 410, 318.
418, 302, 418, 302.
428, 286, 428, 286.
436, 272, 436, 272.
446, 260, 446, 260.
455, 251, 455, 251.
464, 243, 464, 243.
475, 238, 475, 238.
490, 233, 490, 233.
510, 232, 510, 232.
532, 232, 532, 232.
557, 235, 557, 235.
581, 241, 581, 241.
607, 251, 607, 251.
631, 262, 631, 262.
652, 273, 652, 273.
671, 283, 671, 283.
687, 291, 687, 291.
698, 299, 698, 299.
710, 306, 710, 306.
720, 311, 720, 311.
728, 316, 728, 316.
735, 320, 735, 320.
737, 321, 737, 321.
739, 322, 739, 322.
741, 322, 741, 322.
745, 320, 745, 320.
751, 316, 751, 316.
761, 311, 761, 311.
772, 305, 772, 305.
784, 298, 784, 298.
799, 289, 799, 289.
814, 283, 814, 283.
829, 277, 829, 277.
840, 272, 840, 272.
852, 268, 852, 268.
862, 264, 862, 264.
867, 262, 867, 262.
872, 261, 872, 261.
874, 259, 874, 259.
877, 257, 877, 257.
879, 255, 879, 255.
880, 254, 880, 254.
881, 253, 881, 253.
882, 253, 882, 253.
883, 252, 883, 252.
884, 251, 884, 251.
885, 251, 885, 251.
885, 252, 885, 252.
883, 256, 883, 256.
877, 267, 877, 267.
870, 280, 870, 280.
858, 300, 858, 300.
844, 322, 844, 322.
827, 349, 827, 349.
811, 379, 811, 379.
792, 407, 792, 407.
775, 432, 775, 432.
759, 455, 759, 455.
747, 469, 747, 469.
736, 482, 736, 482.

提前谢谢您!

最佳答案

参见Custom Painting Approaches自定义绘画的两种常见方法:

  1. 将要绘制的形状添加到列表中,并在每次重新绘制组件时重新绘制形状。
  2. 将形状绘制到 BufferedImage 上,并在重新绘制组件时绘制缓冲图像。

关于Java - Graphics2D - 鼠标移动后绘制一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21355082/

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