前提・実現したいこと
プレイヤーとcheckポイントが存在する簡単なゲームを作ろうとしています。プレイヤーが各checkポイントと重なったときに色が変わる機能を実装したいです。
発生している問題・エラーメッセージ
一つ前の質問でgetBoundsを利用することを勧められたので利用したのですが現時点でエラー自体は出ていないのですが何故か重なった後にcheckポイントのカラーが変化しません
該当のソースコード
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.InputStream; import java.awt.Rectangle; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.imageio.ImageIO; import javax.swing.JFrame; public class MyJFrameTest3 extends JFrame { private final static String IMAGE_FILE_NAME ="kadai.png"; private BufferedImage bufferedImage = null; private final static int FRAME_LENGTH = 400; private final static int LENGTH_OF_PLAYER = 10; private final static int LENGTH_OF_CHECKPOINT = 10; private Rectangle player = new Rectangle(10, 260, LENGTH_OF_PLAYER, LENGTH_OF_PLAYER); private Rectangle checkpoint1 = new Rectangle(10, 200, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT); private Rectangle checkpoint2 = new Rectangle(260, 260, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT); private Rectangle checkpoint3 = new Rectangle(260, 200, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT); private Rectangle checkpoint4 = new Rectangle(10, 50, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT); private int gazouX = FRAME_LENGTH / 2; private int gazouY = FRAME_LENGTH / 2; public static void main(String[] args) { new MyJFrameTest3(); } public MyJFrameTest3() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addKeyListener(new MyKeyAdapter()); setSize(FRAME_LENGTH, FRAME_LENGTH); setLocationRelativeTo(null); setVisible(true); try (InputStream inputStream = this.getClass().getResourceAsStream(IMAGE_FILE_NAME);){ bufferedImage = ImageIO.read(inputStream); } catch (IOException e) { e.printStackTrace(); } setVisible(true); } public void paint(Graphics g) { g.drawImage(getScreen(), 0, 0, this); } private Image getScreen() { Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.black); g.drawImage(bufferedImage, 0, 0, this); g.draw(player); g.draw(checkpoint1); g.draw(checkpoint2); g.draw(checkpoint3); g.draw(checkpoint4); return screen; } private class MyKeyAdapter extends KeyAdapter { private int count = 0; @Override public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); int keyCode2 = e.getKeyCode(); int keyCode3 = e.getKeyCode(); int keyCode4 = e.getKeyCode(); if(player.getBounds().intersects(checkpoint1.getBounds())){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint2); } if(player.getBounds().intersects(checkpoint2.getBounds())){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint3); } if(player.getBounds().intersects(checkpoint3.getBounds())){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint1); } if(player.getBounds().intersects(checkpoint4.getBounds())){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint4); } switch (keyCode) { case KeyEvent.VK_LEFT: player.setLocation((int) player.getX() - 10, (int) player.getY()); break; case KeyEvent.VK_RIGHT: player.setLocation((int) player.getX() + 10, (int) player.getY()); break; case KeyEvent.VK_UP: player.setLocation((int) player.getX(), (int) player.getY() - 10); break; case KeyEvent.VK_DOWN: player.setLocation((int) player.getX(), (int) player.getY() + 10); break; default: return; } repaint(); } } }
試したこと
アドバイスをいただいて以下のようなプログラムを追加してみました
if(player.getBounds().intersects(checkpoint1.getBounds())){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint2); } if(player.getBounds().intersects(checkpoint2.getBounds())){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint3); } if(player.getBounds().intersects(checkpoint3.getBounds())){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint1); } if(player.getBounds().intersects(checkpoint4.getBounds())){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint4); }
補足情報(FW/ツールのバージョンなど)
プログラミング初心者なのでつたないプログラムだとは思うのですが詳しい方ご指摘お願いします。
あなたの回答
tips
プレビュー