자바 프로그래밍에 익숙하지 않습니다. 나는 약간의 튜토리얼을 따라 몇 가지 비디오를 보았다. 그래픽을 표시하기 때문에 분명히 뭔가 잘못하고 있어요. 앱을로드 할 때마다 10 번 중 하나가 표시되기 때문입니다.JFrame이 그래픽을 무작위로 렌더링합니다.
게임 개념은 축구 게임입니다. 내 목표는 다른 시나리오가 존재할 때 필드의 어느 부분에 있어야 하는지를 8 살짜리에게 가르치는 것입니다.
Eclipse를 IDE로 사용하고 있습니다. 아래는 수업 및 관련 회원들의 요약입니다. 아래에 표시된 클래스는 필드와 필드에 몇 개의 선을 그리는 데 사용됩니다.
참고 :이 게시물의 크기를 최소화하기 위해 많은 로직이 제거되었습니다. 자세한 정보가 필요하면 알려주십시오.
public class SoccerPlaybook {
public static void main(String[] args) {
new SoccerPlaybook();
}
public SoccerPlaybook() {
JFrame frame = new JFrame();
frame.add(new Game(this));
}
}
public class Game extends JPanel {
public Game(SoccerPlaybook soccerPlaybook) {
this.soccerPlaybook = soccerPlaybook;
}
public void paintComponent(Graphics g) {
field.paintComponent(g);
}
}
public class Field extends JPanel {
public Field(Game game) {
this.game = game;
}
public void paintComponent(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(0, 0, game.getSoccerPlaybook().getWidth(), game.getSoccerPlaybook().getHeight());
g.setColor(Color.WHITE);
g.drawLine(200, 50, 400, 50);
g.drawLine(200, 300, 400, 300);
g.drawLine(200, 50, 200, 300);
g.drawLine(325, 100, 400, 100);
g.drawLine(325, 250, 400, 250);
g.drawLine(325, 100, 325, 250);
}
}
나는 paint 및 paintComponent를 사용해 보았습니다. 둘 다 동일한 결과를 얻은 것으로 보입니다.
도움 주셔서 감사합니다.
그게 전부입니다. 고맙습니다. – alockrem