tags:

views:

54

answers:

3

I made an Applet with some Panels on it. I draw something on a panel with specific methods I created, they use a graphics object to draw.
To draw I use commands like:

gr = this.getGraphics;  
gr.drawString... etc  

Then I call these methods from the applet class.
My problem is the following: After I minimize or resize the browser window the panel does not
show anything, I suppose it's because I didn't implement anything in the paint() method of the panel.
Is there an way to fix that problem without changing all my methods?

Some of my methods are like that:

//paint a node with coords x,y and nodeNumber in the center of the node
public void paintNode(int x,int y,Integer numberOfNode){

    gr = this.getGraphics();
    gr.setColor(ShowGUI.getPanelColor());
    gr.fillOval(x,y,40,40);
    gr.setColor(Color.BLACK);
    gr.drawOval(x,y,40,40);
    gr.drawString(numberOfNode.toString(),x+17,y+25);

}

//marks red the processing edge
public void markEdge(int x1,int y1,int x2,int y2,Integer numberOfNode1,Integer numberOfNode2,int weight){

    gr.setColor(Color.red);
    this.paintEdge(x1,y1,x2,y2,numberOfNode1,numberOfNode2,weight);
    this.paintNode(x1, y1, numberOfNode1);
    this.paintNode(x2, y2, numberOfNode2);

}
A: 

You need to call update()/repaint() methods on Panel when window is minimized and maximized.

You will need to override start() method of applet and add repaint() to it. Defination for start():

public void start(): This is called after the "init" event. It is also called at times when the user is not using your applet and starts to use it again such as when a minimized browser containning your applet is maximized.

Here is how you code should look:

public void start(){
    super.start();
    this.repaint();
}

Hope this helps.

YoK
how can I do that?Do you mean i have to overwrite the repaint() method in my Panel class?
john
just call it. If you will need to add windows litener add this call to methods invoked for minimize maximize event.
YoK
sorry,i dint really understand,Im relative new to java.Where in my Panel class do i have to call it?and how?My paint() is empty,anything drawn by the Panel is done in the methods i described...
john
@john Paint the component in the `paint` method.
Tom Hawtin - tackline
its not only one component,and many new things are added to the panel depending on the users action,so I implemented it like this:if a user wants do draw a circle,i call from the applet the panel method paintACircle(Ex,Ey) where Ex = E.x and Ey = E.y(the coords where the user clicked). There are many methods,i think I cant just put them into my paint()...
john
Thanks for your interest,but i think your code is for the applet class,not for my panel Class where actually all my drawing happens.I ve got a simple question,can i paint a graphics object gr where all my stuff is drawn, just in my paint() method?something like: paint(Graphics g){paint(gr)//like when paint is called,just redraw everything that is //drawn in the gr object}
john
As I see your Panel is included in Applet. So in your Applet you can add this start method and call repaint of your Applet as well as "panel.repaint();" panel here is instance of you Panel.
YoK
Yes your are right.But just because my Panels paint() is empty, after the repaint(),(after the browser window is maximized) the Applet shows everything except the graph(i draw graphs in the Panel)...Thats why i ask for my Graphics gr object...
john
You could actually store all variables you pass to your methods as instance variables, And call your methods from paint method. I think this will solve your problem.
YoK
That would solve the problem with the drawing in the paint() i agree.But i think the following situation:I handle all the drawing methods in the paint() with boolean vars.The drawing would be ok,but my Applet works like that:A kruskal algorithm is running and decides depending on his steps to erase an edge(a line),mark red an edge or just leave the edge as it is. After resizing my browser the paint() method actually doesnt "remember" what edge has do be marked,deleted or leaved. I think the approach of DevonCMiller would fit better.
john
My question:can i actually draw shapes on a image and then draw that "drawn" image when paint() is called?Is that possible?
john
AS I stated earlier , You could store all realted variables (Actually passed to your Paint methods) at instance level, And paint() calls all of your paintxxx methods with these variables. DevonCMiller's solution is also similar to this.
YoK
Thank you very much for your interest and patience!It worked with your advice.
john
@john u r welcome. can you please accept my answer now :).
YoK
haha,yes you were right of course,obviously i didnt understand your point of view at the beggining:)
john
@john By Accepting my answer I meant, You to click tick mark next to my answer for your question. :) I would gain some points with it.
YoK
A: 

When the applet is resized, the image is cleared and the paint method is called to repaint it. At the moment, the default paint method has no knowledge of the changes you are making to the display from paintNode.

The correct way to do this is to keep a list of the objects to be painted, including any relevant information like location, color, etc. When the user adds/removes/changes something, the list is changed and repaint() is called. The paint code then needs to walk the list and paint the shapes, text, etc to the display.

Devon_C_Miller
A: 

ok...what could that list possibly be?could i draw everything to an image too,and then repaint somehow the image?everything i draw is drawn on a Graphics Object gr,cant I repaint somehow that object gr in the paint method?

john