Why is my line not drawing?
Because this is not how custom painting works.
There are at least two main problems. The first is, you are creating a new Axis
on each drag event, this unnecessary and inefficient.
You should create a new Axis
on mousePressed
, passing the start points and update this instance within the mouseDragged
event. If you need to maintain previously draw lines, you will need to add these to a List
of some kind so they can be re-painted (remember, painting is destructive).
The second issue is the fact that painting is performed within the context of the components paint
method. Assuming that you are using AWT, you should have some kind of custom class that extends from Component
, Canvas
is very popular.
You would override the paint
method of this component and perform you painting here. This is why you need the List
For example...
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);
}
}
}
Take a look at Painting in AWT and Swing for more details about the painting process.
Now, unless you have a really good reason for doing so, I would encourage you to use the Swing API over the AWT library, which was replaced with Swing some 15 years ago. There are more people available who understand how Swing works then who remember (or have experience with) AWT. To that end, you should start by having a look at Performing Custom Painting
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…