-3

EDIT:

I DO the same thing here but problem does not occur.

    import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

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


public class MainFrame extends JFrame implements KeyListener {
static long start;
MyPanel right, left;
    public MainFrame() {

    }

    public void go(){
        setUndecorated(true);
        setLocation(0, 0);
        Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setSize(screenSize);
        addKeyListener(this);
        setLayout(new GridLayout(1,0));

        Border b = BorderFactory.createLineBorder(Color.black, 4);
        Border b1 = BorderFactory.createLineBorder(Color.blue, 4);
        right = new MyPanel();
        left = new MyPanel();
        right.setBorder(b); left.setBorder(b1);

        add(left);add(right);


        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


    public void update() {
        left.update();
        right.update();
}

    @Override
    public void keyPressed(KeyEvent arg0) {
        if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE)
            System.exit(1);
        else if (arg0.getKeyCode() == KeyEvent.VK_RIGHT){
            start = System.currentTimeMillis();
            update();
        }

    }



    @Override
    public void keyReleased(KeyEvent arg0) {}

    @Override
    public void keyTyped(KeyEvent arg0) {}

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().go();
            }
        });

    }

}
import java.awt.Graphics;

import javax.swing.JPanel;


public class MyPanel extends JPanel {
    MyPanel(){
        super();
    }
    public void update(){
        repaint();
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        System.out.println(System.currentTimeMillis() -  MainFrame.start);
    }


}

First of all I want to say that I've prepared little example which suppose to show my problem but the problem didn't occured in this example code it is only in the original program. I know it is horrible to read but it will help a lot, because problem is in exact particular place and you don't need to understand rest of the code or even polish only a few lines.

  1. GlowneOkno is main class with a main method which starts it all.
  2. Glowne okno is KeyListener and Extends JFrame it contains 3 extended JPanels: MapaPanel, InfoLiniaPanel, KursPanel.
  3. When you press "right arrow" on keyboard it suppose to run akutalizuj(it means update) method which calls akutalizuj method(update method) in those 3 extended JPanels. In those methods there is repaint() called.
  4. I used static long start in class GlowneOkno to see when the method GlowneOkno:Aktualizuj is called and then I println(currentTime - GlowneOkno.start) in paint method() from InfoLiniaKurs(this is that one where the time is displayed -> you will see). And this interval is enermous it is so slow, I don't know why, if I block in method akutalizuj Mapa Panel

I know this is big thing I am asking, but this is fith day I can't find reason and I am unable to form question and put there everything you should know without giving all project in Eclipse and I have like 5 more hours.

Program works in 1680 x 1050

Copyrights Robert Kilar 2012 ; ) But if you wanna use it somehow just ask me. I will be glad to help.

The main main is in GlowneOkno class.

ESC to close the app.

enter image description here

Yoda
  • 17,363
  • 67
  • 204
  • 344
  • Please repeat your attempt to isolate the problem first. If you didn't isolate it the first time, you should try again before trying to post the whole megillah here. – Hovercraft Full Of Eels Aug 21 '12 at 02:00
  • *"I've prepared little example"* Good, but if it is small, inline the code to the question. – Andrew Thompson Aug 21 '12 at 02:00
  • But it does not show the problem. Like I said problem occurs in the program. I had hope problem will ocure there but it did not. – Yoda Aug 21 '12 at 02:00
  • I know it doesn't show the problem -- that means that your attempt to isolate it wasn't successful and you've got more work to do. You're premature to ask it on SO at this stage. – Hovercraft Full Of Eels Aug 21 '12 at 02:01
  • *"Program works in 1680 x 1050"* Good, run it at that and stop bothering us. If OTOH, you mean the game is **supposed** to work at 1680x1050, but is broken at any size, adjust the size to 200x100. I typically don't need a screen-full of broken GUI to solve these types of problems. – Andrew Thompson Aug 21 '12 at 02:02
  • @AndrewThompson It works at 1680x1050. It will crash at other resolution. – Yoda Aug 21 '12 at 02:02
  • Note that neither I nor @HovercraftFullOfEels were notified of those comments. Start them with 'at'PersonName to notify someone. As an aside, to whom was your last comment directed? – Andrew Thompson Aug 21 '12 at 02:04
  • @Andrew: it's a cryptic comment to me. – Hovercraft Full Of Eels Aug 21 '12 at 02:04
  • @HovercraftFullOfEels I am trying to resist giving this qn. a down-vote, but the last edits are making it harder. :( – Andrew Thompson Aug 21 '12 at 02:06
  • @AndrewThompson I am sorry, I am trying as hard as I can. Do you mean that it does not look as in the picutre in 1680x1050 on your computer? The main main is in GlowneOkno class. – Yoda Aug 21 '12 at 02:09
  • 2
    1) Take a deep breath. Let it out slowly. 2) If a graphic view has too many layers it will take a long time to refresh as each layer is redrawn. Sometimes you can fix this to a degree by giving the right hints to the drawing system so that it knows it doesn't have to redraw stuff that either hasn't hand anything "on top" of it change or else is completely hidden/overwritten by layers "on top". – Hot Licks Aug 21 '12 at 02:11
  • @HotLicks I call the repaint() mathod in those 3 panels not in the main JFrame. Even if I block two of them and let repaint only this on the upper left side it is still slow. – Yoda Aug 21 '12 at 02:13
  • If the upper left side is slow painting it's likely because you're painting a lot of stuff you don't need to be painting. – Hot Licks Aug 21 '12 at 02:14
  • @Andrew: He's posting 12 full classes in a foreign language with supporting resources and without any simplification or reduction -- I don't think that this is an answerable question on this site in this format. I'm voting to close. – Hovercraft Full Of Eels Aug 21 '12 at 02:14
  • *"I am trying as hard as I can."* That is worrying, some people are not suited to programming, and a vital skill in programming is reducing a problem to the simplest form. I will not respond to the rest of the comment on the basis that I will not be following the link. The advice I offered on GUI size was about the SSCCE. Voting to close. – Andrew Thompson Aug 21 '12 at 02:16
  • @AndrewThompson Added simplified version but here it goes fast. It is in english. – Yoda Aug 21 '12 at 02:18
  • @HovercraftFullOfEels Please look at edit. Thank you. – Yoda Aug 21 '12 at 02:24
  • I wonder if you should be doing some of your drawing, the background images that don't change in a BufferedImage. Then you don't have to keep re-drawing everything. – Hovercraft Full Of Eels Aug 21 '12 at 02:34
  • @HovercraftFullOfEels I wonder how to save for example this big 4 in upper left screen and draw only time hh:mm? – Yoda Aug 21 '12 at 02:48
  • @HovercraftFullOfEels This using I wonder... was not mean it's just accidentally ; ) – Yoda Aug 21 '12 at 02:55
  • You need to take about 4 hours time and study the documentation for your drawing system so that you really understand how an image is built up, layer-by-layer. – Hot Licks Aug 21 '12 at 02:58
  • 1
    And you appear to be reading image files a *lot* which is very expensive time-wise. You could try to store some of the images to memory if possible and minimize image file reads as much as possible. – Hovercraft Full Of Eels Aug 21 '12 at 02:58
  • @AndrewThompson -- In other cases I'd agree with you that the person posting should be flipping burgers or some such. But assuming what he's displaying above is largely his own work and not simply hacked from somewhere else it exhibits some degree of ability. I think he's simply panicked. – Hot Licks Aug 21 '12 at 02:59
  • @HovercraftFullOfEels Thanks that's something I didn't thought about. Could you tell me what class or mechanism let me load images first to the memory, so I can short study an example? It is 5 am now and this is second night in last 4 days I am not sleeping(working). Thanks for indulgence . – Yoda Aug 21 '12 at 03:05
  • The scaling algorithm was too expensive. – Yoda Aug 21 '12 at 03:19

1 Answers1

4

Take a long hard look at each individual step required to do a repaint.

  1. InfoLinePane needs to render the text and time. While this itself is not a difficult process, you could consider offloading some of it to a buffered image instead, cause lets face it, only the time changes. When the other details change, you could invalidate the buffer and repaint it again.
  2. You update the InfoLinePane again...?
  3. You assign the map name to the mapaPanel without any consideration as to whether it needs to changed or not
  4. You load the map. You scale said map. You change the preferred size of the map panel to match the size of the scaled image
  5. You scale the map (again)
  6. You paint the station stops. I'm pretty sure the stops don't change that match, you should consider buffering this result. Unless the time has actually changed, it's probably no point in updating progress bar.

Image loading and scaling is expensive. You should consider caching these results if memory allows. Scaling is best achieved in the background. Use a low quality to scale to start with and offload the high quality scale to a separate thread

Consider what actually changes. Render everything that doesn't change (often) to buffer instead.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • That's something which will really help. Thank you. I will tell if the program speeded up a lot. – Yoda Aug 21 '12 at 03:13
  • Thank you really, really much that you even wanted to look at my code. I've changed scaling algorithm and now it works just fine! I understood that it is not the only mistake, but because of lack of time this has to be enough for now. It flies right now. – Yoda Aug 21 '12 at 03:16
  • @RobertKilar Sometimes we can't see the forest for the trees ;) - That's why they invented chain saws >) – MadProgrammer Aug 21 '12 at 03:20