Posted snake. In Windows 7, everything works fine, in Ubuntu 14.04 it slows down. RAM on a laptop 8 Gb. Both in Windows and in Ubuntu I launch via the console. I suspect the problem is installing the JDK. Maybe someone knows what exactly the problem?

while(snake.isAlive()) { snake.move(dir, apple); gp.repaint(); try { Thread.sleep(70); // Ρ‚Π°ΠΊ рСгулируСтся ΡΠΊΠΎΡ€ΠΎΡΡ‚ΡŒ } catch(Exception ex) {} } class GamePanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(backColor); g.fillRect(0, 0, Width * Scale, Height * Scale); g.setColor(apple.color); while(!apple.rectSnakeFree(snake.coordX, snake.coordY)) { // ΠΏΠΎΠΊΠ° Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ свободного ΠΎΡ‚ Π·ΠΌΠ΅ΠΉΠΊΠΈ мСста Π½Π° Π»ΠΎΠΊΠ°Ρ†ΠΈΠΈ apple.randCoord(); // ΠΌΠ΅Π½ΡΡ‚ΡŒ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ } g.fillRect(apple.x * Scale, apple.y * Scale, Scale - 1, Scale - 1); // отрисовка яблока g.setColor(snake.color); // отрисовка Π·ΠΌΠ΅ΠΉΠΊΠΈ for(int i = 0; i < snake.len; i++) { g.fillRect(snake.coordX[i] * Scale, snake.coordY[i] * Scale, Scale - 1, Scale - 1); } } } 

This is how the animation is drawn.

If you need a code, here is a link to github .

Closed due to the fact that off-topic participants Nick Volynkin ♦ , Aries , LEQADA , aleksandr barakin , Abyx 20 Oct '15 at 13:41 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • β€œQuestions asking for help with debugging (β€œ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nick Volynkin, Aries, LEQADA, aleksandr barakin, Abyx
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • five
    There are performance problems - profile. Put a piece of code illustrating the problem here (but not all of the code, of course). Otherwise, your question will be answered only by those who are not too lazy to climb on the githab (and there are few) - VladD
  • Are the versions the same? On openjdk linux or installer downloaded from oracle? - don Rumata
  • @donRumata Download from oracle - Roman Kavyrshin

1 answer 1

On Linux, paintComponent is called stably, but drawing on the screen does go with brakes.

Use double buffering:

 class GamePanel extends JPanel { private final Graphics2D G; private final BufferedImage buffer; public GamePanel() { buffer = new BufferedImage(Width * Scale, Height * Scale, BufferedImage.TYPE_INT_RGB); G = buffer.createGraphics(); } @Override protected void paintComponent(Graphics g) { draw(G); g.drawImage(buffer, 0, 0, null); } private void draw(Graphics g) { g.setColor(backColor); g.fillRect(0, 0, Width * Scale, Height * Scale); // ... } } 

With her brakes will be less.

Also, the while(snake.isAlive()) is called in the UI stream and blocks it for 70 ms - it would not hurt to take out the loop and all the checks in a separate thread.

And finally, if you plan to handle the keys in JPanel, in addition to requestFocus() you need to set setFocusable(true) .

 gp.setFocusable(true); gp.requestFocus(); new Thread(new Runnable() { @Override public void run() { while(snake.isAlive()) { while(!apple.rectSnakeFree(snake.coordX, snake.coordY)) { apple.randCoord(); } snake.move(dir, apple); gp.repaint(); try { Thread.sleep(70); } catch(Exception ex) {} } } }).start();