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 thepaint()
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);
}
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.
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.
精彩评论