Reputation:
My question has been alluded to in Java Challenge on Permitting the User to Draw A Line, however, I am still having difficulty as no line appears on my application when the mouse is clicked and dragged.
Answering this question definitely would help most beginner programmers better understand the graphics class and drawing, an often intricate process, especially for beginners.
According to the text I am using (as I am learning Java on my own), this was the example of how to draw a line using Java:
/*
* LineTest
* Demonstrates drawing lines
*/
import java.awt.*;
public class LineTest extends Canvas {
public LineTest() {
super();
setSize(300, 200);
setBackground(Color.white);
}
public static void main(String args[]) {
LineTest lt = new LineTest();
GUIFrame frame = new GUIFrame("Line Test");
frame.add(lt);
frame.pack();
frame.setVisible(true);
}
public void paint(Graphics g) {
g.drawLine(10, 10, 50, 100);
g.setColor(Color.blue);
g.drawLine(60, 110, 275, 50);
g.setColor(Color.red);
g.drawLine(50, 50, 300, 200);
}
}
The specification is:
Create an application that allows you to draw lines by clicking the initial
point and draggingthe mouse to the second point. The application should be
repainted so that you can see the line changing size and position as you
are dragging the mouse. When the mouse button is eleased, the line is drawn.
As you will recognize, running this program does not create any drawing by the user. I believe this error is encountered due to the lack of a mouseReleased method.
Any help is greatly appreciated. Thank you in advance for all your time and cooperation regarding this matter.
My code to answer the question is:
import java.awt.*;
import java.awt.event.*;
public class LineDrawer2 extends Canvas {
int x1, y1, x2, y2;
public LineDrawer2() {
super();
setSize(300,200);
setBackground(Color.white);
}
public void mousePressed(MouseEvent me) {
int x1 = me.getX();
int y1 = me.getY();
x2 = x1;
y2 = y1;
repaint();
}
public void mouseDragged(MouseEvent me) {
int x2 = me.getX();
int y2 = me.getY();
repaint();
}
public void mouseReleased(MouseEvent me) {
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.blue);
g.drawLine(x1, y1, x2, y2);
}
public static void main(String args[]) {
LineDrawer2 ld2 = new LineDrawer2();
GUIFrame frame = new GUIFrame("Line Drawer");
frame.add(ld2);
frame.pack();
frame.setVisible(true);
}
public void mouseMoved(MouseEvent me) {
}
public void mouseClicked(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
}
P.S.: I understand from the last reply that this is an old format, however, if possible please let me know using the old format, I will be sure to learn the new as well. I sincerely appreciate it.
Upvotes: 0
Views: 118
Reputation: 691755
You're initializing local variables instead of initializing the fields in your event handling methods. Instead of
int x2 = me.getX();
int y2 = me.getY();
it should be
this.x2 = me.getX();
this.y2 = me.getY();
or simply
x2 = me.getX();
y2 = me.getY();
EDIT:
Another problem is that, even though your class has methods mousePressed(), mouseDragged(), etc., it doesn't implement MouseListener and MouseMotionListener. And finally, it doesn't add any such listener to itself. So the code should be modified like the following:
public class LineDrawer2 extends Canvas implements MouseListener, MouseMotionListener {
...
public LineDrawer2() {
...
addMouseListener(this);
addMouseMotionListener(this);
}
My advice: each time you add a method to a class (like mousePressed()
), and that this method is supposed to override a method from a class or an interface, annotate it with @Override
. That way, the compiler will generate a compilation error if the method doesn't actually override any method:
@Override
public void mousePressed(MouseEvent e) {
}
Upvotes: 1