ゲーム用のグラフィックスを表示しようとしましたが、グラフィックスがパネルに表示されていません。Javaグラフィックスが表示されない
以下は私のコードです。メインクラスは、他の2つのクラスのペイントメソッドを呼び出します。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Simulator extends JFrame implements KeyListener, Runnable, ActionListener {
private final int WIDTH, HEIGHT;
private Boolean right;
private int xMotion;
public Salt salt;
public Player playR;
Boolean running = false;
private Thread thread;
public static int score, highScore;
private int saltSpeed;
public Simulator(int width, int height) {
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(width, height);
panel.setBackground(Color.BLACK);
frame.add(panel);
playR = new Player();
this.HEIGHT = height;
this.WIDTH = width;
int xCordSalt = (int) (Math.random() * 631);
saltSpeed = 1;
salt = new Salt(saltSpeed);
right = true;
running = true;
}
public static void main(String[] args) {
Simulator game = new Simulator(640, 480);
game.start();
}
public void paintComponent(Graphics g)
{
salt.paint(g);
playR.paint(g);
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
repaint();
tick();
run();
}
public void stop() {
running = false;
System.exit(0);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_D) {
right = true;
} else if (e.getKeyCode() == KeyEvent.VK_A) {
right = false;
}
}
public void tick() {
salt.tick(this, playR);
playR.tick();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_D)
{
playR.setDirection(true);
}
else if(e.getKeyCode() == KeyEvent.VK_A)
{
playR.setDirection(false);
}
}
@Override
public void run() {
while (running) {
tick();
repaint();
try {
thread.sleep(7);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void incrementScore() {
score++;
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
tick();
}
}
そしてここでメソッド塩のためのコードです:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
public class Salt extends Rectangle{
private final int WIDTH = 10;
private final int HEIGHT = 10;
public int xCordSalt, yCordSalt;
private int speed;
Rectangle boundBox;
public Salt(int speedx)
{
xCordSalt = (int)Math.random()*641;
yCordSalt = 0;
speed = speedx;
boundBox = new Rectangle(xCordSalt, yCordSalt, WIDTH, HEIGHT);
boundBox.setBounds(xCordSalt, yCordSalt, WIDTH, HEIGHT);
}
public void tick(Simulator sim, Player playR)
{
boundBox.setBounds(xCordSalt, yCordSalt, WIDTH, HEIGHT);
if(yCordSalt >= 480)
{
//sim.stop();
}
else if(checkCollision(playR))
{
sim.incrementScore();
speed++;
yCordSalt = -speed;
}
yCordSalt = yCordSalt + speed;
}
public boolean checkCollision(Player playR)
{
if(this.getBoundBox().intersects(playR.getBoundBox()))
{
return true;
}
return false;
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(xCordSalt, yCordSalt, WIDTH, HEIGHT);
}
public Rectangle getBoundBox()
{
return boundBox;
}
public double getSpeed()
{
return speed;
}
}
そして最後に画像を表示するImageIconのクラスを使用する方法プレーヤー、:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Player extends JPanel {
private int xCord, yCord;
public Rectangle boundBox;
private static ImageIcon ryan;
boolean isRight;
public Player() {
ryan = new ImageIcon("E:\ryan.png");
xCord = 640/2;
yCord = 460;
boundBox = new Rectangle(xCord, yCord, 20, 20);
}
public static void main(String[] args) {
}
public void tick() {
}
public void setDirection(Boolean right)
{
if(right)
isRight = true;
else
isRight = false;
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(ryan.getImage(), xCord, yCord, null);
}
public Rectangle getBoundBox()
{
return boundBox;
}
}
これではまだ不完全な部分もありますが、なぜグラフィックが表示されていないのか分かりません。実行すると、黒のフレーム/パネルのみが表示されます。私はいくつかのprint文を各クラスのtick()メソッド、各クラスのpaint()メソッド、paintComponent()メソッド、およびstart()メソッドに追加しました。ゲームが開始され、各クラスのtickメソッドが実行されますが、paintComponent()またはpaint()メソッドのいずれかが呼び出されます。
「E:\ ryan.png」を「E:\\ ryan.png」に変更する必要があります。バックスラッシュ(\)はエスケープ文字です。実際のバックスラッシュを文字列に配置するには、別のバックスラッシュでバックスラッシュをエスケープする必要があります。 – VGR
@ursinusTheStrong Oh manどうやってエスケープシーケンスを忘れることができますか?私に思い出させてくれてありがとう! –
JFrameにはpaintComponentメソッドがないため、呼び出されないため、@Overrideを使用する必要があります。 JPanelから継承しているクラスを作成し、paintComponentメソッドをオーバーライドしてそこでカスタムパンチングを実行します。まずsuper.paintComponentを呼び出してください。 1つのアイデアは、あなたのゲームのいくつかの要素を記述するEntityを作成することです。これらの要素のうちの1つが「ペイント可能」になります。これはGraphicsの参照を渡すことができ、あなたがする必要があるのは、エンティティのリストを維持し、更新し、ペイントすることです。 – MadProgrammer