所以我已经定义了一个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新手。
答案 0 :(得分:1)
为什么我的画线不画画?
因为这不是自定义绘画的工作原理。
至少有两个主要问题。首先,您要在每个拖动事件上创建一个新的Axis
,这是不必要且效率低下的。
您应该在Axis
上创建新的mousePressed
,传递起始点并在mouseDragged
事件中更新此实例。如果你需要保留以前的绘制线,你需要将它们添加到某种List
中,这样它们就可以被重新绘制(记住,绘画具有破坏性)。
第二个问题是绘画是在组件paint
方法的上下文中执行的。假设您使用的是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