보드 게임과 같은 자바 프로그램을 만들려고합니다. gametiles를 가지고 있는데, 현재 레이아웃을 올바르게 만들려고 할 때 단 하나의 색상 만 사용합니다. 타일이 창 너비의 절반 정도에 나타나서 바닥으로 확장되기를 원합니다. 어쩌면 9x9 또는 11x11 크기의 다른 타일로도 가능합니다. 필자는 그리드 레이아웃을 사용하여 이들을 서로 가깝게 만 드는 것이지 만지는 것은 아니지만 보드 게임처럼 보일 정도로 가까워 지도록 노력했습니다. 그러나, 내가 무엇을 시도하든간에, 타일은 공간이 너무 멀리 떨어져 있으며 창 크기를 조정할 때 모양이 바뀝니다. 나는 이것을 달성하기 위해 GridLayout 관리자를 사용 해왔다. 여기 내 코드가있다.JPanels 자바로 그리드 레이아웃
import java.awt.GridLayout;
import javax.swing.JFrame;
public class GameWindow extends JFrame {
public GameWindow(String title){
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200,400);
}
/**
* @param args
*/
public static void main(String[] args) {
GameWindow gameWindow = new GameWindow("Game");
gameWindow.setLayout(new GridLayout(0,2));
GameTile greenTile = new GameTile(0,true,0,10);
GameTile greenTile2 = new GameTile(0,true,0,10);
gameWindow.add(greenTile);
gameWindow.add(greenTile2);
gameWindow.setVisible(true);
}
}
이것은 GameWindow.java 파일입니다. 많은 코드가 GameTile의 속성처럼, 아직 진행
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
/**
* @author Zachary Parks
*
*/
public class GameTile extends JPanel {
private Color color;
private Color[] colors = {Color.BLUE, Color.YELLOW, Color.BLACK};
private int score, multiplier,initialX,initialY;
private boolean positiveEffect;
public GameTile(int colorTile, boolean effect, int initX, int initY){
color = colors[colorTile];
score = getScore(color);
multiplier = getMultiplier(color);
positiveEffect = effect;
initialX = initX;
initialY = initY;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Image image = null;
try {
image = ImageIO.read(new File("greentile.png"));
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(image,initialX,initialY,this);
}
private int getMultiplier(Color color2) {
return 0;
}
private int getScore(Color color2) {
return 0;
}
/**
* Method that returns the data from the tile in
* array of int. 0 index = added score, 1 index = tile effect score
* @return
*/
public int[] getData() {
int [] scoreData = null;
scoreData[0] = score*multiplier;
return null;
}
}
입니다
모두 내가 노력하고있어 다음과 같다 : (여전히 주로 단지 테스트 완료되지 않습니다) 내가 지금까지 가지고있는 GameTile.java입니다 이 시점에서 타일은 서로 옆에 있습니다. 이 내가 얻고 무엇을 :
그럼 정확히 무엇이 문제입니까? 배터리가 94 %이고 케이블을 연결합니까? – brummfondel
그림 방법에서 파일 입출력을 수행하지 마십시오. 이미지는 클래스의 생성자에서 읽어야합니다. – camickr
정말 그 패널과 레이아웃 관리자가 필요합니까? 왜 단순한 보드 종류를 찾고 있다면 왜 9x9 또는 11x11의 사각형을 그려 채우지 않습니까? – Arvind