I want to modify the program to connect the series of dots that make up one stroke of a letter with a straight line so that words can be drawn but i don't really have a idea of how to do it so i want some references. I tried to draw them using drawLine but it's not what i really wanted.
This is my code:
package tool;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class MyPaintApp extends JFrame {
int x, y;
Vector<Point> list = new Vector<>();
class MyPanel extends JPanel {
public MyPanel() {
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
x = event.getX();
y = event.getY();
list.add(new Point(x, y));
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Point p : list)
g.fillOval(p.x, p.y, 4, 4);
}
}
public MyPaintApp() {
setSize(600, 450);
setLocation(400, 0);
setTitle("My Paint");
add(new MyPanel());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class MyPaintApp extends JFrame {
int x, y;
Vector<Point> list = new Vector<>();
Vector<Vector<Point>> list1 = new Vector<>();
class MyPanel extends JPanel {
public MyPanel() {
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
x = event.getX();
y = event.getY();
list.add(new Point(x, y));
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < list.size() - 1; i++) {
Point p1 = list.get(i);
Point p2 = list.get(i + 1);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
}
public MyPaintApp() {
setSize(600, 450);
setLocation(400, 0);
setTitle("My Paint");
add(new MyPanel());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
drawLine
to attempt to draw lines, but I only got a bunch of dots in the shape of a line instead of actual lines. How do I connect the dots?" I would encourage you to edit your question to say that instead.