0

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Klaabu
  • 337
  • 1
  • 2
  • 9
  • *"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."* Don't forget the `Color`, `Stroke`, `RenderingHints` etc. used to generate the graphic. It is tougher than you might expect. When partway finished to creating serializable versions of many of those things for a [paint undo/redo functionality](http://stackoverflow.com/questions/12780063/command-pattern-for-undo-redo-in-paint-application), I decided it was easier to create a `BufferedImage` of the result, and store that instead. – Andrew Thompson Nov 27 '12 at 08:23
  • I was thinking ideally, yes, to store some sort of raster image of the squiggle rather than all it's points and attributes. I'll note to look into some BufferedImage tutorials, then. – Klaabu Nov 27 '12 at 08:52

1 Answers1

3

There's quite few on varying subjects that would be helpful. Some of the core/basic ones would include

Which I would suggest you would need an appreciation of

I'll throw Creating a GUI With JFC/Swing as it discusses such things as key bindings and mouse listeners.

I'd also become familiar with Working with Images as undoubtly your going to want to save and load images as well

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Ah, yes, that first one is what got me this far. Thanks for the help, I'll definitely be checking the others out as well. – Klaabu Nov 27 '12 at 08:48