I'm currently trying to write a Java application which would allow freehand drawing and later, the moving and deleting of each squiggle drawn.
I'm guessing my best bet would be to have each click and drag create a separate entity but I've no idea how to implement this. So far I only have a small JFrame which will display a "brush" with the help of the Oracle tutorials but not even the line this brush draws.
class MyPanel extends JPanel {
private int ovalX = 50;
private int ovalY = 50;
public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.black));
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
moveSquare(e.getX(),e.getY());
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
moveSquare(e.getX(),e.getY());
}
});
}
private void moveSquare(int x, int y) {
int OFFSET = 1;
if ((ovalX!=x) || (ovalY!=y)) {
ovalX=x;
ovalY=y;
repaint();
}
}
public Dimension getPreferredSize() {
return new Dimension(250,200);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillOval((ovalX - 5),(ovalY - 5),10,10);
}
}
I'm not sure how I am to continue. Should I first consume some general tutorials? And if so, on what subjects?