私はJFrame(jf)を持っていて、そのウィンドウで3つのJPanelを取得しました。そのJPanelsにグラフィックがあります。キーボードの右矢印を押したときに、このグラフィックを再描画したいと思います。JFrameにkeyListenerを追加しました。addKeyListener(this)->JFrameはKeyListenerを実装します。
if (arg0.getKeyCode() == KeyEvent.VK_RIGHT){
start = System.currentTimeMillis();
l.czas.addMinuty(1);
aktualizuj();
}
すべてのパネルでリペイントと呼びますが、このリペイントと右矢印を押すまでの時間が長くなります。同時に右キーを何度も押すと、プログラムがフリーズし、いくつかの手順(およびその間のグラフィックス)がスキップされます。HITTINGANARROWからCallingまでの時間を測定しました。たとえば文字列は時間を表します。時間がないところは、キーを何度も押した瞬間です。私を助けてください。
Paint component called 1 times
Before superPaintComponent 1734
After superPaintComponent 1734
Before drawing Time 1781
Paint component called 2 times
Before superPaintComponent 2953
After superPaintComponent 2953
Before drawing Time 2953
Paint component called 3 times
Before superPaintComponent 0
After superPaintComponent 0
Before drawing Time 0
Paint component called 4 times
Before superPaintComponent 0
After superPaintComponent 0
Before drawing Time 0
Paint component called 5 times
Before superPaintComponent 0
After superPaintComponent 0
Before drawing Time 16
rakowiecka.PNG
Paint component called 6 times
Before superPaintComponent 0
After superPaintComponent 0
Before drawing Time 0
Paint component called 7 times
Before superPaintComponent 0
After superPaintComponent 0
Before drawing Time 0
Paint component called 8 times
Before superPaintComponent 0
After superPaintComponent 0
Before drawing Time 16
placuniilubelskiej.PNG
Paint component called 9 times
Before superPaintComponent 15
After superPaintComponent 15
Before drawing Time 15
Paint component called 10 times
Before superPaintComponent 0
After superPaintComponent 0
Before drawing Time 0
Paint component called 11 times
Before superPaintComponent 0
After superPaintComponent 0
Before drawing Time 0
placzbawiciela.PNG
Paint component called 12 times
Before superPaintComponent 0
After superPaintComponent 0
Before drawing Time 0
**IT GOES ON...................**
public void aktualizuj(int nrPrzystanku){
this.nrPrzystanku = nrPrzystanku;
repaint();
}
@Override
public void paintComponent(Graphics g) {
NR++;
System.out.println("Paint component called\t" + NR + "\ttimes");
System.out.println("Before superPaintComponent\t\t" + (System.currentTimeMillis() - GlowneOkno.start));
super.paintComponent(g);
System.out.println("After superPaintComponent\t\t" + (System.currentTimeMillis() - GlowneOkno.start));
int Height = (int) d.getHeight(), Width = (int) d.getWidth();
// System.out.println("PAINT W InfoLiniaPanel");
Font f = new Font(Font.SANS_SERIF,Font.BOLD, (4 *Height/10));
g.setFont(f);
// System.out.println(Height);
//rysowanie numeru linii
g.drawString(linia.nr + "", Width/10, Height/3);
//rysowanie czasu
Font f1 = new Font(Font.SANS_SERIF,Font.BOLD, 2 * Height/18);
g.setFont(f1);
System.out.println("Before drawing Time\t\t" + (System.currentTimeMillis() - GlowneOkno.start));
g.drawString(linia.czas +" ", Width/10 + g.getFontMetrics(f).stringWidth(linia.nr+"") + Width/10, Height/3);
//kierunek
// System.out.println(linia.kurs.get(linia.kurs.size() -1 ).kierunek);
g.setFont( new Font(Font.SANS_SERIF,Font.BOLD,1 *Height/20));
g.drawString(linia.kurs.get(linia.kurs.size() -1 ).kierunek + "",
Width/10 + g.getFontMetrics(f).stringWidth(linia.nr+"") + Width/10,
Height/3 - g.getFontMetrics(f1).getAscent() /*g.getFontMetrics(f1).getHeight()*/ );
// System.out.println(g.getFontMetrics(f1).getHeight());
//nastepny przystanek
// System.out.println(linia.kurs.get(nrPrzystanku)); //TRZEBA ZMIENIC NA JAKIS INDEKS ZEBY SZLA SYMULKACJA!!!!!!
//NAJLEPIEJ DAC POLE OBECNY PRZYSTANEK W KLASIE LINIA
g.drawString("Następny przystanek:" , Width/10, Height/3 + g.getFontMetrics().getHeight() + Height/10);
g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,1 *Height/18));
if( nrPrzystanku +1 < Linia.trasa.length)
g.drawString(linia.kurs.get(nrPrzystanku + 1).nazwa+"", Width/10, Height/3 + g.getFontMetrics().getHeight()*12/6 + Height/10);
else
g.drawString("Ostatni postój", Width/10, Height/3 + g.getFontMetrics().getHeight()*12/6 + Height/10);
}