-1

I am having a grid of buttons and I want to draw a line between any two clicked buttons and that line should stay when i press next set of buttons.To do so I made following paint and init method in applet and also override the update method because we know that first of all repaint calls the update(Graphics g) method. default implementation of upadate is that it first clears the background and then calls the paint method which finally does the drawing . and hence due to the default implementation of the update method previous drawing gets erased. if we override the update method as given then it calls paint(g) directly and hence does not clear the earlier drawing.

But still the previous line disappear.

Cœur
  • 37,241
  • 25
  • 195
  • 267
prtyush
  • 157
  • 1
  • 7
  • 1
    Don't override update(..) and paint(...). That is an old approach when using AWT. When using Swing custom painting is done by overriding the `paintComponent()` method. – camickr Dec 30 '14 at 20:29
  • @camickr It's not possible to override paintComponent as JApplet is not a subclass of JComponent as mentioned below in comments – prtyush Dec 30 '14 at 20:33
  • @prtyush Then don't use a JApplet as you main container, use a JComponent add add it to the applet, that way you get double buffering for free – MadProgrammer Dec 30 '14 at 20:41
  • Yes, I didn't finish my thought. You should NOT do custom painting on a top level container like JApplet (JFrame, JDialog). Instead you do the custom painting on a JPanel and add the panel to the applet (frame, dialog). You WOULD override the paintComponent() method of the panel. Read the Swing tutorial on [Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) – camickr Dec 30 '14 at 20:43

2 Answers2

4

Instead of trying to stop it from repainting, you could store a data structure of the lines that should be drawn, and each time it paints, have it paint the appropriate lines. So, clicking two buttons would add data to the data structure indicating with pair of buttons a line should be drawn between.

So, in your actionPerformed listener, you would store the xy coordinates in the data structure, instead of setting those instance variables.

The data structure could be a simple ArrayList, containing a simple Line class that you create. The Line class would just contain two sets of coordinates:

class Line {
    private int x1;
    private int x2;
    private int y1;
    private int y2;

    public Line(x1, y1, x2, y2) {
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }
}

Then in your paint method, you can loop through your arraylist of Line objects, and draw each one.

forgivenson
  • 4,394
  • 2
  • 19
  • 28
  • 1+ for storing the object in a List. See [Custom Painting Approaches](http://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/) for a working example that uses this approach. – camickr Dec 30 '14 at 20:30
  • @forgivenson Can you write code ?Am not getting what you are trying to say ? – prtyush Dec 30 '14 at 20:30
  • @prtyush, `Can you write code ?` Don't expect us to write the code for you. Did you read the link given above and test out the example code? Take the time to understand the concept and then modify it for your particular requirement. – camickr Dec 30 '14 at 20:33
  • @prtyush See how you out your buttons into an ArrayList? It's the same concept – MadProgrammer Dec 30 '14 at 20:44
0

There are two ways of doing this. To go your way (e.g. override a paint() method), try overriding paintComponent() rather than paint()(more).

A better way is to use a JLayeredPane, and have a transparent JPanel on top of the panel with the buttons. The drawing will be done on the top pane. I think this approach is more inline with the Swing philosophy, and would work better (less graphic glitches due to image caching, etc).

Michael Bar-Sinai
  • 2,729
  • 20
  • 27
  • See the link I added. paint() takes care of many things (border, child components...) and you want to override as less as possible. However, note that swing does a lot of caching, and buttons may repaint on hover (depending on the current LaF) so you're really better off using JLayeredPane (again, see link) – Michael Bar-Sinai Dec 30 '14 at 20:28
  • 1
    It's not possible to override `paintComponent` as `JApplet` is not a subclass of `JComponent`. – TNT Dec 30 '14 at 20:29
  • Right. Thanks. Keeping this answer as the JLayerPane would still work (I think). – Michael Bar-Sinai Dec 30 '14 at 20:34
  • A JLayeredPane is also an approach, but the key to all of this is to store the objects you want to paint in an ArrayList so you can track all the lines as they are added (removed). Using this approach you would probably add a JPanel to the layered pane to do your custom painting so yes you would override the paintComponent() method. You should not be overriding a painting method of a top level container like JFrame, JApplet etc.. – camickr Dec 30 '14 at 20:34
  • 1
    @tnt Then don't use JApplet as the main container, use a JComponent or JPanel and add it to the applet, you decouple the code, improve reusability and get double buffering for free – MadProgrammer Dec 30 '14 at 20:43