0

Making a paint-like application that works by saving mouse points in an arraylist. My idea is to have a "points" arraylist with all the previously drawn stuff, and a "temp" arraylist to get and modify the current brush stroke the user just entered. This is necessary as the user can change the color and size, so my idea is to modify the current brush stroke based off what buttons have been pressed, then add that brush stroke to the rest of the picture. I searched around StackOverflow and found some code but cant get it to work how I want to (assuming I found the right code).

@Override
public void paintComponent(Graphics g1) {
    super.paintComponent(g1);
    final Graphics2D g = (Graphics2D)g1.create();
    try {
        g.setColor(brushColor);
        for (Point point : tempArrayList){
            g.fillOval(point.x, point.y, brushSize, brushSize);
        }

    } finally {
        g.dispose();
    }

The problem is that I need to clear the tempArrayList for the next brush stroke, which I can do when they change the color/size, but then it erases what was previously there. I am starting to think that I don't even need the "points" arraylist as descibed above because I was hoping that the g1 graphic would just save what the g graphic created.

I guess I just need to figure out how to add the g graphic to g1

frosty
  • 307
  • 4
  • 15
  • *"How to prevent JPanel from repainting everything?"* Paint to a `BufferedImage` and display it in a label. – Andrew Thompson Oct 26 '14 at 23:10
  • @AndrewThompson Could you show me how to do this? Or link to another SE question similar? – frosty Oct 26 '14 at 23:36
  • Here is a [recent example](http://stackoverflow.com/a/26519275/418556) of doing an animation within a buffered image. The [`WorldView`](http://stackoverflow.com/a/18825844/418556) source is another example. Both are MCVEs. – Andrew Thompson Oct 26 '14 at 23:48

2 Answers2

0

Painting is controlled by the Swing API, there is a expectation that whenever paintComponent is called, you will repaint the entire state of the component (as part of your component might have been damaged due to some system event), so the short answer is, no, you can't...however....

You Could...

Paint to a BufferedImage instead and paint the BufferedImage when paintComponent is called

You Could...

Establish a series of "paintable" objects which contain information about what is to be painted and how it is to the painted, including the brush stroke, stroke color and fill color.

These would then be added to some kind of List and "painted" when the paintComponent method is called

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yeah, Currently my paintComponent has about 12 for loops. Basically I have 12 array lists (4 colors and 3 sizes, so 1 size for each color) and the repaint goes through each arraylist and recreates it every time I call repaint. I realize this is horrible code so I was looking for a way to clean it up, not using buffered image. Could you show me how to do it with buffered image in your answer above, if I wanted to go that route? – frosty Oct 26 '14 at 23:18
0

Check out Custom Painting Approaches for exampled of painting from:

  1. A List of objects
  2. A BufferedImage.

The examples show how to draw Rectangles of different colours.

camickr
  • 321,443
  • 19
  • 166
  • 288