I'm still learning the basics of Java, so sorry if this question is basic. What I've tried to do is to store the points and lines into ArrayLists
and then have the paintComponent()
method go through them and draw them each time its called. Since I'm using the same ArrayList
for my points, I clear it every time the mouse is pressed, so the new line won't connect with the previous one.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;
public class SwingPaintDemo2 {
public static void main(String[] args) {
JFrame f = new JFrame("Swing Paint Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300,300);
f.add(new MyPanel());
f.setBackground(Color.WHITE);
f.setVisible(true);
}
}
class MyPanel extends JPanel {
private int x;
private int y;
private int x2;
private int y2;
private ArrayList<Point> points = new ArrayList<Point>();
private ArrayList<Curves> curvedLines = new ArrayList<Curves>();
public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.black));
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
points.add(new Point(e.getX(), e.getY()));
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
points.clear();
points.add(new Point(e.getX(), e.getY()));
}
@Override
public void mouseReleased(MouseEvent e) {
ArrayList<Point> newPoints = new ArrayList<Point>();
for (int i = 0; i < points.size(); i++) {
newPoints.add(points.get(i));
}
curvedLines.add(new Curves(newPoints));
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
for (int c = 0; c < points.size() - 1; c++) {
x = (int) points.get(c).getX();
y = (int) points.get(c).getY();
x2 = (int) points.get(c + 1).getX();
y2 = (int) points.get(c + 1).getY();
g.drawLine(x, y, x2, y2);
}
for (int t = 0; t < curvedLines.size(); t++) {
ArrayList<Point> iterator = curvedLines.get(t).points;
for (int c = 0; c < iterator.size() - 1; c++) {
x = (int) iterator.get(c).getX();
y = (int) iterator.get(c).getY();
x2 = (int) iterator.get(c + 1).getX();
y2 = (int) iterator.get(c + 1).getY();
g.drawLine(x, y, x2, y2);
}
}
}
class Curves {
private ArrayList<Point> points;
public Curves(ArrayList<Point> points) {
this.points = new ArrayList<Point>();
this.points = points;
}
}
}
The problem is that while I drag the mouse, no line is drawn, but its points are still being stored since the line appears when I drag the mouse again. What is causing this apparent delay?
In my original comment you were asked to compare the working code given in the last question to the code you posted here to see what the difference is.
The difference is that you added the if/else statement in the paintComponent() method.
I added the if/else statement because the paintComponenet wouldn't draw anything
That doesn't make any sense. When you add an if/else statement it means that only one type of painting can be done, either paint the curves or paint the points but you can never do both.
You always want to do both:
So get rid of the if/else statement.
Also, in the future when you post code post a proper SSCCE se we can execute the code. The code posted here doesn't have a main() method or a JFrame so it can't be compiled and tested.