I have been trying to display the graphics for my game, but none of the graphics are being displayed on the panel.
Following is my code. The main class invokes the paint methods of the other two classes.
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();
}
}
And here's the code for the method salt:
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;
}
}
And finally the method player, which uses the imageIcon class to display an image:
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;
}
}
Now some of it is incomplete, but I cannot realize why it is displaying no graphics whatsoever. When ran, only a black frame / panel appears. I added some print statements to the tick() methods of each class, the paint() methods of each class and the paintComponent() method, and the start() method. The game will start, run each class's tick method, but paintComponent() or any of the paint() methods are ever called!