Home > Back-end >  Ask questions about the cost.
Ask questions about the cost.

Time:03-14

The procedure is as follows, there are two problems:
1) the public void paint (Graphics g) will be executed repeatedly, if press in the program to write, so if there are a lot of the Font object is generated, and then quickly abandoned?
2) there is a global object private Font theFont=new Font (null, the Font. PLAIN, 19), if the paint method used in this object, whether can avoid the waste of repeatedly make the Font object?

 import Java awt. *; 

Public class test extends Frame {
Private Image offScreenImage=null;
Private Font theFont=new Font (null, the Font. PLAIN, 19).

Public static void main (String [] args) {
The test TTT=new test ();
TTT. LaunchFrame ();
}

Public void launchFrame () {
Enclosing setLocation (400, 10);
Enclosing setSize (400, 400);
Enclosing setBackground (Color. LightGray);
Enclosing setVisible (true);
New Thread (new PaintThread ()). The start ();
}

@ Override
Public void paint (Graphics g) {
G.s etFont (new Font (null, the Font. PLAIN, 19));
}

@ Override
Public void update (Graphics g) {
If (offScreenImage==null) {
OffScreenImage=this. CreateImage (900, 800);
}
Graphics g_offScreen=offScreenImage. GetGraphics ();
Color=c g_offScreen. GetColor ();
G_offScreen. SetColor (Color. LightGray);
G_offScreen. FillRect (0, 0, 820, 800);
G_offScreen. SetColor (c);
Paint (g_offScreen);
G.d rawImage (offScreenImage, 0, 0, null);
}

Private class PaintThread implements Runnable {
@ Override
Public void the run () {
While (true) {
Repaint ();
Try {
Thread.sleep (15);
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
}
}
  • Related