0

I have something like that (the time is now displayed correctly). I have added KeyListener to the JFrame in which these JPanels are. When I press ->(right key) I want to time to increment by one and it does but it is really slow(like 3 seconds of waiting then even much longer). The algorithms in program are not the fault(checked without GUI, they're fast). Is there any way to faster the reaction ->pressing key-> changing time. The image is png so it should be fast. I call aktualizuj() every time I want to refresh.

PS: THE TIME IS ONT THE LEFT SIDE OF THE SCREEN: 14:28

enter image description here

import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;


public class InfoLiniaPanel extends JPanel {
Linia linia;
Dimension d;
int nrPrzystanku;
    public InfoLiniaPanel(Linia linia) {
        super();
        this.linia = linia;
        nrPrzystanku = 0;
        go();
    }

    public void go(){
    d = new Dimension(java.awt.Toolkit.getDefaultToolkit().getScreenSize().width/4, java.awt.Toolkit.getDefaultToolkit().getScreenSize().height/2);
    setPreferredSize(d);        
        Border b = BorderFactory.createLineBorder(Color.red, 4);
        setBorder(b);       
    }

    public void aktualizuj(int nrPrzystanku){
        this.nrPrzystanku = nrPrzystanku;
        revalidate();
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        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);
        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));
        g.drawString(linia.kurs.get(nrPrzystanku).nazwa+"", Width/10, Height/3 + g.getFontMetrics().getHeight()*12/6 + Height/10);


    }

    public static void main(String[] args) {
        new InfoLiniaPanel(new Linia(4));
        JFrame jf = new JFrame();
        jf.setSize(400,400);
        jf.add(new InfoLiniaPanel(new Linia(4)));
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

EDIT:

@Override
public void keyPressed(KeyEvent arg0) {
    if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE)
        System.exit(1);
    if (arg0.getKeyCode() == KeyEvent.VK_RIGHT){
        l.czas.addMinuty(1);
        aktualizuj();
    }

}


public void aktualizuj(){
    //System.out.println("Akutalizuj");

try{

    liniaPanel.aktualizuj(nrPrzystankuZKolei);
//  System.out.println(l.kurs.get(nrPrzystankuZKolei + 1).czas.equals(l.czas));
    if (nrPrzystankuZKolei < (l.kurs.size()-1) && l.kurs.get(nrPrzystankuZKolei + 1).czas.compareTo(l.czas) <= 0) {
        /*  System.out.println(l.czas + " "
                    + l.kurs.get(nrPrzystankuZKolei).czas + " "
                    + l.kurs.get(nrPrzystankuZKolei + 1).czas);
            System.out.println("Pora nanastepny przystanek");
            System.out.println(l.kurs.get(nrPrzystankuZKolei + 1));
            System.out.println(l.czas); */
            nrPrzystankuZKolei++;// przejscie do kolejnego przystanku
            liniaPanel.aktualizuj(nrPrzystankuZKolei);
            //mapa
            mapaPanel.sciezka = Linia.trasa[nrPrzystankuZKolei].substring(0,  Linia.trasa[nrPrzystankuZKolei].length() - 2)+".png";
            ((MapaPanel) mapaPanel).aktualizuj();
            //kurs
            ((KursPanel) kursPanel).aktualizuj(nrPrzystankuZKolei);
        }

}catch(Exception e){e.printStackTrace();}
}
aymeric
  • 3,877
  • 2
  • 28
  • 42
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • You're sample's a little lacking, for instance, missing `Linia`, now I appreciate that difficult in positing all the code, but when I fix your code for the missing classes, it runs fine :P – MadProgrammer Aug 18 '12 at 03:58
  • It is the reaction I press right key and then I have to wait until the upper left Panel will be updated... really long. – Yoda Aug 18 '12 at 04:04
  • Stupid question ;) in your key listener, you are calling repaint? Have you put debug statements in the paint code to see when it paints between pressing the key and when the paint occurs?? – MadProgrammer Aug 18 '12 at 04:07
  • liniaPanel.aktualizuj() is call for aktualizuj() in the JPanel we are talking about. It is good idea to see when the paint is called... I can't use debugger I will try to use nanosecs.. – Yoda Aug 18 '12 at 04:12
  • I might suggest the use of `inavldiate` over `revalidate` but I've been known to use both when I'm stuck with paint problem I can't resolve (you need to invalidate the container in order for revalidate to really have much of an effect) – MadProgrammer Aug 18 '12 at 04:18
  • It can jump like 10 minutes forward after freeze. – Yoda Aug 18 '12 at 04:24
  • There suppose to be 8 infos about time. It skipped after freeze. 1345213242671 1345213243890 rakowiecka.PNG placuniilubelskiej.PNG placzbawiciela.PNG metropolitechnika.PNG placpolitechniki.PNG 1671 nowowiejska.PNG koszykowa.PNG centralny.PNG 1015 – Yoda Aug 18 '12 at 04:26
  • Be sure to call `Graphics.setClip(..)` before painting. – Andrew Thompson Aug 18 '12 at 04:33
  • Tried, nothing changed. Maybe it is because of listener? Maybe it slows down? – Yoda Aug 18 '12 at 05:03
  • besides being unreadable (to the more western eye :-) - there's nothing obvious in the snippets you are showing. No, keyListeners (which you shouldn't use here, but that's another story) never slow down anything. If you are absolutely sure that the underlying data update (which would be my first guess to the culprit) is just fine, strip apart all the painting code and test the panels separately. Then go from there: worst case would be that all are fine but the interaction runs wild ... – kleopatra Aug 18 '12 at 07:40
  • 2
    Without an [sscce](http://sscce.org/) or profiled fragment, this question is _not_ constructive. – trashgod Aug 18 '12 at 08:42

0 Answers0