java – 为什么我的线不画?
作者:互联网
所以我已经定义了一个mouseEventlistener和mousemotionListener来定义点.
protected Point elementPosition = null;
public Point endPoint = null;
public Axis tempAxis;
public Graphics g;
class MouseButtonHandler extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
if(e.getModifiers()==InputEvent.BUTTON1_MASK)
{
elementPosition =new Point(e.getX(), e.getY()) ;
if(addType==YLABEL)
{
YDialog ydia = new YDialog(anApp);
ydia.setVisible(true);
value =(double) ydia.getValue();
ydia.dispose();
}
}
}
public void mouseReleased(MouseEvent e)
{
}
}
class MouseMoveHandler extends MouseMotionAdapter
{
public void MouseMoved(MouseEvent e)
{
Point currentCursor = e.getPoint();
}
public void mouseDragged(MouseEvent e)
{
endPoint = new Point(e.getX(), e.getY());
tempAxis = new Axis(elementPosition, endPoint);
tempAxis.draw(g);
}
}
轴类定义的位置.
import java.awt.*;
import java.awt.event.*;
public class Axis extends Object
{
public Point position;
public Point endPoint;
public Axis(Point position, Point endPoint)
{
this.position = position;
this.endPoint = endPoint;
}
public void draw(Graphics g)
{
g.setColor(Color.red);
g.drawLine(position.x, position.y, endPoint.x, endPoint.y);
}
}
这些都在视图类中实现.弹出窗口,按菜单显示菜单,但在mouseDragged时不绘制轴.具体来说它表示存在问题
tempAxis.draw(克);.有谁知道为什么会发生这个错误.顺便说一句,我还是Java新手.
解决方法:
Why is my line not drawing?
因为这不是定制绘画的工作原理.
至少有两个主要问题.首先,您要在每个拖动事件上创建一个新的Axis,这是不必要且低效的.
您应该在mousePressed上创建一个新的Axis,传递起始点并在mouseDragged事件中更新此实例.如果你需要保持以前的绘制线,你需要将它们添加到某种类型的List中,这样它们就可以被重新绘制(记住,绘画具有破坏性).
第二个问题是绘画是在组件绘制方法的上下文中执行的.假设你正在使用AWT,你应该有一些从Component扩展的自定义类,Canvas非常受欢迎.
您将覆盖此组件的paint方法并在此处执行绘制.这就是你需要List的原因
例如…
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class DrawLine {
public static void main(String[] args) {
new DrawLine();
}
public DrawLine() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
Frame frame = new Frame("Testing");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends Canvas {
private List<Axis> lines;
public TestPane() {
lines = new ArrayList<>(25);
MouseAdapter handler = new MouseAdapter() {
private Axis current;
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Clicled");
current = new Axis(e.getPoint());
lines.add(current);
}
@Override
public void mouseDragged(MouseEvent e) {
System.out.println("Dragged");
if (current != null) {
current.setEndPoint(e.getPoint());
repaint();
}
}
@Override
public void mouseReleased(MouseEvent e) {
current = null;
}
};
addMouseListener(handler);
addMouseMotionListener(handler);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (Axis axis : lines) {
System.out.println("line");
axis.draw(g);
}
}
}
public class Axis extends Object {
public Point position;
public Point endPoint;
public Axis(Point position) {
this.position = position;
this.endPoint = position;
}
public void setEndPoint(Point endPoint) {
this.endPoint = endPoint;
}
public void draw(Graphics g) {
g.setColor(Color.red);
g.drawLine(position.x, position.y, endPoint.x, endPoint.y);
}
}
}
有关绘画过程的更多详细信息,请查看Painting in AWT and Swing.
现在,除非你有充分的理由这样做,否则我鼓励你在AWT库上使用Swing API,它在15年前被Swing取代.有更多的人了解Swing如何工作然后谁记得(或有经验)AWT.为此,你应该先看看Performing Custom Painting
标签:java,graphics,awt,draw,mousemove 来源: https://codeday.me/bug/20190930/1834286.html