So I'm relatively new to Java, but I'm having an issue with static content. One I'm not exactly sure what static stuff is, but I know it's ridiculously annoying, secondly, I have this small game of "Pong" I've been working on as an exercise, and I'm trying to get a Scoreboard up but it is saying that Cannot make a static reference to the non-static method getScore()
Here is my code below, any suggestions on it would helpful since I'm still a rookie.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PongGame extends JComponent implements ActionListener, MouseMotionListener{
private int ballX = 385;
private int ballY = 285;
private int paddleOpX = 0;
private int paddleX = 0;
private int ballYSpeed = 1;
private int ballXSpeed = 1;
public Integer score = 0;
private static Timer t = null;
public static void main(String[] args){
JLabel scoreBoard = new JLabel(getScore().toString());
JFrame window = new JFrame("Hit the Damn Ball");
window.setLayout(new BorderLayout());
window.getContentPane().setBackground(new Color(0, 0, 0));
PongGame game = new PongGame();
window.add(game);
window.pack();
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setVisible(true);
t = new Timer(5, game);
t.start();
window.addMouseMotionListener(game);
}
//set the size of window
public Dimension getPreferredSize(){
return new Dimension(800, 600);
}
@Override
protected void paintComponent(Graphics g){
g.setColor(new Color(110, 65, 13));
g.fillRect(paddleX, 510, 150, 15);
g.setColor(new Color(90, 0,0));
g.fillRect(paddleOpX, 90, 150, 15);
g.setColor(Color.WHITE);
g.fillOval(ballX, ballY, 30, 30);
}
@Override
public void actionPerformed(ActionEvent e) {
paddleOpX = (ballX+5);
ballX +=ballXSpeed;
ballY +=ballYSpeed;
if(ballX >= paddleX-30 && ballX <= (paddleX + 150) && ballY >= 480){
ballYSpeed = -ballYSpeed;
//ballYSpeed = -1;
setScore();
}
if(ballX >= paddleX-30 && ballX <= (paddleX + 150) && ballY > 480){
ballYSpeed = -ballYSpeed;
//ballYSpeed = 1;
setScore();
}
if(ballX >= paddleOpX-30 && ballX <=(paddleOpX + 150) && ballY <= 106){
ballYSpeed = ballYSpeed*-1;
}
if(ballY > 570){
ballXSpeed = 0;
ballYSpeed = 0;
t.stop();
System.out.println(score);
}
if(ballX >= 770){
ballXSpeed = -ballXSpeed;
//ballXSpeed = -1;
}
if(ballY <= 0){
ballXSpeed = 0;
ballYSpeed = 0;
t.stop();
System.out.println(score);
//ballYSpeed = 1;
}
if(ballX <= 0){
ballXSpeed = ballXSpeed*-1;
//ballXSpeed = 1;
}
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
paddleX = e.getX()-75;
if(ballX >= paddleX-30 && ballX <= (paddleX + 150) && ballY == 480 ){
ballYSpeed = ballYSpeed-1;
ballXSpeed = ballXSpeed < 0 ? -2 : 2;
//ballYSpeed = 1;
}
repaint();
}
private void setScore(){
score++;
}
public Integer getScore(){
return score;
}
}
4 Answers 4
The instance method getScore() needs an object on which to call the method because it's not static, but you don't have one yet.
Move PongGame game = new PongGame(); to the first line of main, then change
JLabel scoreBoard = new JLabel(getScore().toString());
to
JLabel scoreBoard = new JLabel(game.getScore().toString());
5 Comments
add the JLabel to the JFrame's content pane.private void setScore() method is not static method and your are trying to call this method from static method, you have to either make this method as static or call this method using object.
To create static method you have to use static modifier in method syntax.
private static void setScore(){
score++;
}
OR
you have to create instance of PongGame class and then call that method.
PongGame pg = new PongGame();
JLabel scoreBoard = new JLabel(pg.getScore().toString());
Comments
The method getScore() is not static (because you are not declaring it as static), so it needs an instance to be called. In other words, you have to create an instance of your class PongGame:
public static void main(String[] args){
PongGame game = PongGame(...);
...
}
and then use it to invoke the method:
game.getScore();
Note: A static method exists only once per class, so you don't need an instance of the class to call it. In the other hand, a non-static method need an instance to be called.
Comments
getScore() needs it to be static because you did not put "new", so without an instance of it, it cannot compute.
JLabel scoreBoard = new JLabel(getScore().toString());
needs it and you have not declared it as "static".
Static means it is loaded to memory only once and for all (which is good for some situations (like a web method running on your server(probably has low memory))).
Simple solution: create an instance and use it.
getScore gs= new getScore()
or make it static and use it as
getScore.TosTring();
You need to instantiate the superclass before this.