1

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;
 }
}
mattias
2,0983 gold badges20 silver badges27 bronze badges
asked Jan 7, 2015 at 19:22
0

4 Answers 4

1

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());
answered Jan 7, 2015 at 19:24
Sign up to request clarification or add additional context in comments.

5 Comments

okay I did that and it works but now the label won't show up on the screen
You'll need to add the JLabel to the JFrame's content pane.
awesome I think it's just a text color issue now THANK YOU
actually it is not showing, I placed it after the window.add(game) line but the only thing that showed was the label and nothing else
I'm not an expert in Swing, but maybe the label needs to be added your game component which needs to be added to the content pane.
0

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());
answered Jan 7, 2015 at 19:24

Comments

0

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.

answered Jan 7, 2015 at 19:25

Comments

0

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.

answered Jan 7, 2015 at 19:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.