그래서 2 명의 친구들과 함께 학교 프로젝트를 위해 약간의 게임을 다시 만들고 있습니다. 이전과 같은 생각 이었지만 이제 모든 그래픽 (JPanel, JLabel 등). 우리는 기본 GUI를 가지고 있지만 실행 속도는 매우 느립니다. 화살표 키를 누르면 1-2 초 정도의 반응을 보입니다. 빠른 속도로 연속으로 들어 오면 최대 15 초가 걸릴 수 있습니다. 다음은 코드입니다 :GUI 실행 속도가 매우 느림 JAVA
홈페이지
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);
}
}
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;
for (int row = 0; row < 10; row++) {
for (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) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int x = (int) (Math.random() * 2);
if (x != 1 && dragon1 != 0 && content[i][j] == 0
&& (i > 0 && i < 3 || j > 0 && j < 3)) {
dragon1--;
content[i][j] = 1;
}
}
}
}
while (dragon2 != 0 && food != 0) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
int x = (int) (Math.random() * 2);
if (x != 1 && dragon2 != 0 && content[i][j] == 0
&& (i > 2 && i < 5 || j > 3 && j < 5)) {
dragon2--;
content[i][j] = 2;
}
}
}
}
while (dragon3 != 0) {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
int x = (int) (Math.random() * 2);
if (x != 1 && dragon3 != 0 && content[i][j] == 0
&& (i > 4 && i < 7 || j > 4 && j < 7)) {
dragon3--;
content[i][j] = 3;
}
}
}
}
while (dragon4 != 0) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
int x = (int) (Math.random() * 2);
if (x != 1 && dragon4 != 0 && content[i][j] == 0
&& (i > 6 && i < 9 || j > 6 && j < 9)) {
dragon4--;
content[i][j] = 4;
}
}
}
}
while (dragon5 != 0) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
int x = (int) (Math.random() * 2);
if (x != 1 && dragon5 != 0 && content[i][j] == 0
&& (i > 8 && i < 10 || j > 8 && j < 10)) {
dragon5--;
content[i][j] = 5;
}
}
}
}
while (food != 0) {
for (int i = 0; i < 10; i++) {
for (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;
for (int i = 0; i < 10; i++) {
for (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){
}
}
}
}
}
}
와 청취자
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) {
}
}
"Try Catch"가 느려지는 것을 읽었습니다. 그게 다야? 그렇다면 어떻게하면 좋을까요? 아니면 완전한 쓰레기 같습니까?
첫 번째 학기입니다. 10 반복 각각 2 개 중첩 루프 - 또한 을 Heres 그것은 당신의 MapRefresh
클래스에서 http://pastebin.com/VM1cTvyX
거기에 2 개의 이미지가 있습니다 ("hall"및 "valge"). 코드를 시험해 보려면 그냥 삭제하십시오. – Hans