tags:

views:

144

answers:

4

What is the event handler in Java (using net beans, Swing) that resembles the Paint in C#? The event which shall be fired when the form is restored, resized ... etc

public void paint(Graphics g2){
    g2 = pnlDrawing.getGraphics();
    g2.clearRect(0, 0, size, size);
    g2.setColor(Color.BLACK);
    g2.fillRect(size/2-1, 0, 2, size); // draw y axis
    g2.fillRect(0, size/2-1, size, 2); // draw x axis

    //set the font
    g2.setFont(new Font("Arial", 2, 12));

    // write the values on the X axis
    for(int i=0; i<=10; i++){
        if(i == 0)
            continue;
        int pos1 = (size/2-1)-i*30;
        int pos2 = (size/2-1)+i*30;
        g2.draw3DRect(pos1, size/2-3, 1, 5, true);
        g2.drawString(String.valueOf(-i),pos1-10,size/2-3+20);
        g2.draw3DRect(pos2, size/2-3, 1, 5, true);
        g2.drawString(String.valueOf(i),pos2-5,size/2-3+20);
    }

    for(int i=0; i<=10; i++){
        if(i == 0)
            continue;
        int pos1 = (size/2-1)-i*30;
        int pos2 = (size/2-1)+i*30;
        g2.draw3DRect(size/2-3, pos1, 5, 1, true);
        g2.drawString(String.valueOf(i),size/2-3+10,pos1+5);
        g2.draw3DRect(size/2-3, pos2, 5, 1, true);
        g2.drawString(String.valueOf(-i),size/2-3+10,pos2+5);
    }

       pnlDrawing.invalidate();
}
A: 

repaint() . it makes a request to call paint() eventually though, if I'm not mistaken. paint() is the real painter method but repaint() is fired in those restore, resize etc.

Halo
repaint is the public method from the Component class that allows you to request a repaint, it is not the callback, i.e. if the window is resized then a system repaint is triggered, but it won't be done by calling reapaint (I think)
DaveJohnston
you may be right yeah
Halo
+2  A: 

The method:

public void paint(Graphics g)

in the java.awt.Component class (which is the superclass for all Swing components) is the callback method for painting. So any repainting of the components that needs to be done will eventually call this method, so you can override it if you wish to perform your own painting.

== UPDATE ==

You need to subclass a component to get the paint callback, e.g.

public class MyPanel extends JPanel {
    public void paint(Graphics g) {
        // do your painting here
    }
}

Or you could use an anonymous inner class if you don't want to create a whole new class, e.g.

pnlDrawing = new JPanel() {
    public void paint(Graphics g) {
        // Your painting code
    }
}
DaveJohnston
thanks! but are you sure of the exact syntax? because i tried it this way and it didn't get fired!
Ahmad Farid
If you are using eclipse (and probably other IDEs) it will tell you if you are actually overriding a method from a superclass. Maybe if you post some of your code we can take a look and see if there is something else going wrong.
DaveJohnston
I am using Net Beans. I added a part of the code, I am not sure what exactly will be helpful. Thanks.
Ahmad Farid
-1 In general the paintComponent() method NOT the paint() method should be overridden when you are doing custom painting. Read the Swing tutorial on Custom Painting for more information.
camickr
+3  A: 

You should override paintComponent method because paint is also responsible for drawing child components, border and doing some other stuff.

Ha
+1  A: 

The event which shall be fired when the form is restored, resized ... etc

I'm not sure I understand your question. Swing will automatically make sure the painting is done for you and there is no "paint event" that you can listen for. If you want to understand more about painting then you can read up on Painting in AWT and Swing.

If you want to know what event is fired when a form is:

a) resized - use a ComponentListener
b) restored - use a WindowListener

In the above cases read the appropriate section from the Swing Tuturial. You will also find a section on Custom Painting which explains why paint should be done in the paintComponent() method.

camickr