我有一个 JFrame(jf),在那个窗口中我得到了三个 JPanel。我在 JPanel 中有图形。当我按下键盘上的右箭头时,我想重新绘制这个图形。我在 JFrame 中添加了一个 keyListener。addKeyListener(this) -> JFrame 实现 KeyListener。
if (arg0.getKeyCode() == KeyEvent.VK_RIGHT){
start = System.currentTimeMillis();
l.czas.addMinuty(1);
aktualizuj();
}
在每个面板中,我都调用 repaint,但是将调用此 repaint 和按下右箭头之间的时间很长。当我同时按多次右键时,程序将冻结,然后跳过一些步骤(和之间的图形)。我测量了 HITTING AN ARROW 和 Calling 之间的时间,例如代表时间的字符串。时间为0的地方是我多次按键的时刻。请帮我。
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);
}