Class Painter can be used to create small programs that make up the line with the mouse (when the mouse-drag).
Here's how it looks:
Here's how it looks:

import java.awt .*;
import java.awt.event .*;
import javax.swing .*;
public class Painter extends JFrame {
private int pointCount = 0;
private Point points [] = new Point [1000];
public Painter () {
super ("simple drawing program");
getContentPane (). add (new JLabel ("Drag the mouse to draw"), BorderLayout.SOUTH);
addMouseMotionListener (
MouseMotionAdapter new () {
public void mouseDragged (MouseEvent e) {
if (pointCount
points [pointCount] = e.getPoint ();
+ + PointCount;
repaint ();
}
}
} / / End of anonymous class
); / / End method addMotionListener
setSize (300.150);
setLocationRelativeTo (null);
setVisible (true);
}
public void paint (Graphics g) {
super.paint (g);
for (int i = 0; i
g.setColor (Color.red);
g.fillOval (points [i]. x, points [i]. y, 4.4);
}
}
public static void main (String args []) {
Painter test = new Painter ();
test.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
} }
Hopefully helpful
import java.awt.event .*;
import javax.swing .*;
public class Painter extends JFrame {
private int pointCount = 0;
private Point points [] = new Point [1000];
public Painter () {
super ("simple drawing program");
getContentPane (). add (new JLabel ("Drag the mouse to draw"), BorderLayout.SOUTH);
addMouseMotionListener (
MouseMotionAdapter new () {
public void mouseDragged (MouseEvent e) {
if (pointCount
+ + PointCount;
repaint ();
}
}
} / / End of anonymous class
); / / End method addMotionListener
setSize (300.150);
setLocationRelativeTo (null);
setVisible (true);
}
public void paint (Graphics g) {
super.paint (g);
for (int i = 0; i
g.fillOval (points [i]. x, points [i]. y, 4.4);
}
}
public static void main (String args []) {
Painter test = new Painter ();
test.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
} }
Hopefully helpful
0 comments on Painter Class: Drawing with the Mouse in Java :
Post a Comment and Don't Spam!
Dont Spam please