eclipse - GUI running really slow JAVA -
so i'm making little game again school project 2 of friends. have same idea before, graphical (jpanel, jlabel etc. etc.). have basic gui, running slow. when hit arrow key, responds in 1-2 seconds, , if hit fast in row, might take 15+ seconds. here's code:
main
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 , dragons(and 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); } } mapmake
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; (int row = 0; row < 10; row++) { (int col = 0; col < 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) { (int = 0; < 3; i++) { (int j = 0; j < 3; j++) { int x = (int) (math.random() * 2); if (x != 1 && dragon1 != 0 && content[i][j] == 0 && (i > 0 && < 3 || j > 0 && j < 3)) { dragon1--; content[i][j] = 1; } } } } while (dragon2 != 0 && food != 0) { (int = 0; < 5; i++) { (int j = 0; j < 5; j++) { int x = (int) (math.random() * 2); if (x != 1 && dragon2 != 0 && content[i][j] == 0 && (i > 2 && < 5 || j > 3 && j < 5)) { dragon2--; content[i][j] = 2; } } } } while (dragon3 != 0) { (int = 0; < 7; i++) { (int j = 0; j < 7; j++) { int x = (int) (math.random() * 2); if (x != 1 && dragon3 != 0 && content[i][j] == 0 && (i > 4 && < 7 || j > 4 && j < 7)) { dragon3--; content[i][j] = 3; } } } } while (dragon4 != 0) { (int = 0; < 9; i++) { (int j = 0; j < 9; j++) { int x = (int) (math.random() * 2); if (x != 1 && dragon4 != 0 && content[i][j] == 0 && (i > 6 && < 9 || j > 6 && j < 9)) { dragon4--; content[i][j] = 4; } } } } while (dragon5 != 0) { (int = 0; < 10; i++) { (int j = 0; j < 10; j++) { int x = (int) (math.random() * 2); if (x != 1 && dragon5 != 0 && content[i][j] == 0 && (i > 8 && < 10 || j > 8 && j < 10)) { dragon5--; content[i][j] = 5; } } } } while (food != 0) { (int = 0; < 10; i++) { (int j = 0; j < 10; j++) { int x = (int) (math.random() * 2); if (x != 1 && food != 0 && 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; } } maprefresh
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; (int = 0; < 10; i++) { (int j = 0; j < 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){ } } } } } } and listener
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 && y > 0){ mapmake.setdiscovered(x, y-1, 1); mapmake.setdiscovered(x, y, 2); mapmake.setheroy(y - 1); } else if(e.getkeycode() == 38 && x > 0){ mapmake.setdiscovered(x-1, y, 1); mapmake.setdiscovered(x, y, 2); mapmake.setherox(x - 1); } else if (e.getkeycode() == 39 && y < 9){ mapmake.setdiscovered(x, y+1, 1); mapmake.setdiscovered(x, y, 2); mapmake.setheroy(y + 1); } else if (e.getkeycode() == 40 && x < 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) { } } i read "try catch" slowing down. it? if so, how can make better? or complete trash?
keep in mind, our first semester! heres pastebin copy of http://pastebin.com/vm1ctvyx
the maprefresh() method loading images vast number of times every update. beneficial store image once during maprefresh constructor so:
private imageicon imageicon; public class maprefresh extends gui{ static mapmake mapmake = gui.mapmake; imageicon = new imageicon(hall = imageio.read(new file("c:/hall.jpg"))); //... } then when adding panel use:
panel.add(new jlabel(imageicon)); you repeat other image well. should allow eliminate 'try/catch' block method should improve speed.
Comments
Post a Comment