Note that there are some explanatory texts on larger screens.

plurals
  1. POGUI running really slow JAVA
    primarykey
    data
    text
    <p>So i'm making a little game again for a school project with 2 of my friends. We have this same idea as before, but now its all graphical (JPanel, JLabel etc. etc.). We have the basic GUI, but its running really slow. When i hit the arrow key, it responds in like 1-2 seconds, and if i hit the fast in a row, it might take up to 15+ seconds. Here's the code:</p> <p>Main</p> <pre><code>package game; import java.awt.GridLayout; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JPanel; public class Gui extends JFrame{ private static final long serialVersionUID = 1L; int n = 0; int m = 0; static MapMake mapMake = new MapMake(); static MapRefresh mapRefresh = new MapRefresh(); static JPanel panel = new JPanel(); static Fighting fighting = null; public static void main(String[] args) throws IOException { Gui frame = new Gui(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Dungeons and Dragons(and some food)"); frame.setSize(1280, 720); mapMake.setDiscovered(0, 0, 1); panel.setLayout(new GridLayout(10, 10, 2, 2)); mapRefresh.MapRefresh(); frame.add(panel); frame.addKeyListener(new Listener()); frame.setVisible(true); } } </code></pre> <p>MapMake</p> <pre><code>package game; public class MapMake{ static int[][] discovered = new int[10][10]; static int[][] content = new int[10][10]; static int heroX = 0; static int heroY = 0; public void MapMake(){ discovered[0][0] = 1; for (int row = 0; row &lt; 10; row++) { for (int col = 0; col &lt; 10; col++) { discovered[row][col] = 0; } } discovered[0][0] = 1; int dragon1 = 3; int dragon2 = 5; int dragon3 = 7; int dragon4 = 9; int dragon5 = 1; int food = 25; // id 6 while (dragon1 != 0) { for (int i = 0; i &lt; 3; i++) { for (int j = 0; j &lt; 3; j++) { int x = (int) (Math.random() * 2); if (x != 1 &amp;&amp; dragon1 != 0 &amp;&amp; content[i][j] == 0 &amp;&amp; (i &gt; 0 &amp;&amp; i &lt; 3 || j &gt; 0 &amp;&amp; j &lt; 3)) { dragon1--; content[i][j] = 1; } } } } while (dragon2 != 0 &amp;&amp; food != 0) { for (int i = 0; i &lt; 5; i++) { for (int j = 0; j &lt; 5; j++) { int x = (int) (Math.random() * 2); if (x != 1 &amp;&amp; dragon2 != 0 &amp;&amp; content[i][j] == 0 &amp;&amp; (i &gt; 2 &amp;&amp; i &lt; 5 || j &gt; 3 &amp;&amp; j &lt; 5)) { dragon2--; content[i][j] = 2; } } } } while (dragon3 != 0) { for (int i = 0; i &lt; 7; i++) { for (int j = 0; j &lt; 7; j++) { int x = (int) (Math.random() * 2); if (x != 1 &amp;&amp; dragon3 != 0 &amp;&amp; content[i][j] == 0 &amp;&amp; (i &gt; 4 &amp;&amp; i &lt; 7 || j &gt; 4 &amp;&amp; j &lt; 7)) { dragon3--; content[i][j] = 3; } } } } while (dragon4 != 0) { for (int i = 0; i &lt; 9; i++) { for (int j = 0; j &lt; 9; j++) { int x = (int) (Math.random() * 2); if (x != 1 &amp;&amp; dragon4 != 0 &amp;&amp; content[i][j] == 0 &amp;&amp; (i &gt; 6 &amp;&amp; i &lt; 9 || j &gt; 6 &amp;&amp; j &lt; 9)) { dragon4--; content[i][j] = 4; } } } } while (dragon5 != 0) { for (int i = 0; i &lt; 10; i++) { for (int j = 0; j &lt; 10; j++) { int x = (int) (Math.random() * 2); if (x != 1 &amp;&amp; dragon5 != 0 &amp;&amp; content[i][j] == 0 &amp;&amp; (i &gt; 8 &amp;&amp; i &lt; 10 || j &gt; 8 &amp;&amp; j &lt; 10)) { dragon5--; content[i][j] = 5; } } } } while (food != 0) { for (int i = 0; i &lt; 10; i++) { for (int j = 0; j &lt; 10; j++) { int x = (int) (Math.random() * 2); if (x != 1 &amp;&amp; food != 0 &amp;&amp; content[i][j] == 0) { food--; content[i][j] = 6; } } } } discovered[0][0] = 1; } public int getDiscovered(int i, int j) { return discovered[i][j]; } public int getContent(int i, int j) { return content[i][j]; } public int getHeroX() { return heroX; } public int getHeroY() { return heroY; } public void setDiscovered(int i, int j, int k) { MapMake.discovered[i][j] = k; } public void setContent(int i, int j, int k) { MapMake.content[i][j] = k; } public void setHeroX(int heroX) { this.heroX = heroX; } public void setHeroY(int heroY) { this.heroY = heroY; } } </code></pre> <p>MapRefresh</p> <pre><code>package game; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; public class MapRefresh extends Gui{ static MapMake mapMake = Gui.mapMake; @SuppressWarnings("unused") public void MapRefresh() { BufferedImage hall = null; BufferedImage valge = null; JPanel panel = Gui.panel; for (int i = 0; i &lt; 10; i++) { for (int j = 0; j &lt; 10; j++) { // panel.add(new JButton("" + content[i][j])); if (mapMake.getDiscovered(i, j) == 1) { panel.add(new JLabel("hero on siin peal")); } else if (mapMake.getDiscovered(i, j) == 0) { try { panel.add(new JLabel(new ImageIcon(hall = ImageIO.read(new File("C:/hall.jpg"))))); } catch (IOException e) { } catch (NullPointerException e){ } } else { try { panel.add(new JLabel(new ImageIcon(valge = ImageIO.read(new File("C:/valge.jpg"))))); } catch (IOException e) { } catch (NullPointerException e){ } } } } } } </code></pre> <p>and Listener</p> <pre><code>package game; import java.awt.event.*; import javax.swing.JPanel; public class Listener extends Gui implements KeyListener{ static MapRefresh mapRefresh = new MapRefresh(); static MapMake mapMake = Gui.mapMake; static JPanel panel = Gui.panel; static Fighting fighting = null; @Override public void keyPressed(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { int x = mapMake.getHeroX(); int y = mapMake.getHeroY(); if (e.getKeyCode() == 37 &amp;&amp; y &gt; 0){ mapMake.setDiscovered(x, y-1, 1); mapMake.setDiscovered(x, y, 2); mapMake.setHeroY(y - 1); } else if(e.getKeyCode() == 38 &amp;&amp; x &gt; 0){ mapMake.setDiscovered(x-1, y, 1); mapMake.setDiscovered(x, y, 2); mapMake.setHeroX(x - 1); } else if (e.getKeyCode() == 39 &amp;&amp; y &lt; 9){ mapMake.setDiscovered(x, y+1, 1); mapMake.setDiscovered(x, y, 2); mapMake.setHeroY(y + 1); } else if (e.getKeyCode() == 40 &amp;&amp; x &lt; 9){ mapMake.setDiscovered(x+1, y, 1); mapMake.setDiscovered(x, y, 2); mapMake.setHeroX(x + 1); } int z = mapMake.getContent(x, y); panel.removeAll(); System.out.println(e.getKeyCode()); mapRefresh.MapRefresh(); panel.validate(); panel.repaint(); } @Override public void keyTyped(KeyEvent e) { } } </code></pre> <p>I read that the "Try Catch" is slowing it down. Is that it? If so, how can i make it better? Or is this like complete trash?</p> <p>Keep in mind, that this is our first semester! Heres also a pastebin copy of it <a href="http://pastebin.com/VM1cTvyX" rel="nofollow">http://pastebin.com/VM1cTvyX</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload