前提・実現したいこと
簡単なゲームを作ろうとしています。プレイヤーとcheckポイントを設定してもしそのプレイヤーがチェックポイントの上に重なるとcheckポイントの色が変化するようにしたいです。checkポイントを設定して表示するとこまではできたのですが色を変化させる段階で自分なりにif文を追加してみたのですが行き詰ってしまい質問させていただきました。初心者のためつたないプログラムだとは思いますがどなたかご教授いただけると幸いです。
発生している問題・エラーメッセージ
座標指定にsetBoundsというものを利用したのですがその部分でvoidをbooleanに変換できませんというエラーが出ています。
該当のソースコード
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.setBounds(260, 260, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT)){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint2); } if (player.setBounds(260, 200, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT)){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint3); } if (player.setBounds(10, 200, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT)){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint1); } if (player.setBounds(10, 50, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT)){ 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文としてこの部分を追加しました。
if (player.setBounds(260, 260, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT)){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint2); } if (player.setBounds(260, 200, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT)){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint3); } if (player.setBounds(10, 200, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT)){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint1); } if (player.setBounds(10, 50, LENGTH_OF_CHECKPOINT, LENGTH_OF_CHECKPOINT)){ Image screen = createImage(FRAME_LENGTH, FRAME_LENGTH); Graphics2D g = (Graphics2D) screen.getGraphics(); g.setColor(Color.white); g.draw(checkpoint4); }
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー