-3

i have an error with my code saying:

at javax.swing.JComponent.paintComponent(JComponent.java:783)
at com.game.screen.screen.PaintComponent(screen.java:29)
at com.game.screen.screen.GameScreen(screen.java:39)
at com.game.gamecore.Core.StartGame(Core.java:15)
at com.game.gamecore.Core.main(Core.java:6)

my code: com.game.screen

package com.game.screen;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;

import javax.swing.*;

public class screen extends JPanel implements ActionListener  {
    /**
     * 
     */
    ActionListener AL = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

        }
    };

    Timer timer = new Timer(5, AL);
    double x = 0, y = 0, velY = 0,velX = 0;


    public void PaintComponent(Graphics pixel){
        super.paintComponent(pixel);
        Graphics2D g2 = (Graphics2D) pixel;
        Ellipse2D circle = new Ellipse2D.Double(x,y,40,40);
        g2.fill(circle);
        timer.start();
    }
    public void GameScreen(){

        JFrame GameScreen = new JFrame("THE GAME");
        GameScreen.setSize(600, 600);
         PaintComponent(null);
        GameScreen.setVisible(true);




    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

}

com.game.gameCore.Core

package com.game.gamecore;

public class Core {
    public static void main(String[] args) {
    //  new Menu();
    new Core().StartGame(); 
    }

    public void StartGame(){

        String GameState = "idle";

        if(GameState=="idle"){
        GameState="Running";
        new com.game.screen.screen().GameScreen();


        } else if(GameState=="Running") {

            //enter process kill code here

        }
    }
}

how do i call my PaintComponent(grahpics G) code?

ineedto type something random else this thing wont let me post it... i will just keep typing untill it stops giving me error...

Sven Tjeerdsma
  • 283
  • 4
  • 8
  • You don't get error, but exception. Also `at ..` points location which was involved with exception, but main part describing why exception was thrown should be explained before that `at..` section. Please [edit] your question and post proper stacktrace including exception message. – Pshemo Jun 18 '16 at 23:21
  • You should not be calling `paintComponent` manually. I'd suggest [reading a tutorial](https://docs.oracle.com/javase/tutorial/uiswing/painting/) on the subject. – resueman Jun 18 '16 at 23:23
  • BTW: `if(GameState=="idle")` -> [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). I know that your code may work now since you are using literals, but in the future when you will start reading Strings from some resources (console, file), or when you will create new strings dynamically (like with `replace` or `substring` methods) you will see that `==` will start failing you. – Pshemo Jun 18 '16 at 23:24
  • it is not the gamestate or the stacktrace that bothers me its how i call that graphics. i created an void and inserted my code there normally you call in in the main class but now you need to insert it into your JFrame. my question is what method (of JFrame do i need to call? – Sven Tjeerdsma Jun 18 '16 at 23:31

1 Answers1

3

Problems and some solutions:

  1. PaintComponent != paintComponent. String case matters for Java identifiers, so use the right one, here paintComponent.
  2. You don't use @Override prior to your paintComponent method override. If you had done this, the compiler would have pointed out that your method wasn't a true override, forcing you to more carefully inspect your method signature to see why, and you'd likely have found the case error yourself.
  3. Regarding if(GameState=="idle"){, don't compare Strings using == or !=. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two object references are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead do, if ("idle".equals(GameState)) {, or better yet, use enums for game state as this would be much more foolproof.
  4. You post only part of your exception's stacktrace with your question but leave out the crucial beginning part, the part that tells you exactly what exception is being thrown. What is it? A NullPointerException?
  5. re, timer.start(); -- you're calling this from within your paintComponent method, and you shouldn't do this. You do not have full control over when or even if paintComponent is called, and so never should have object state changing code within this method.
  6. PaintComponent(null); You're calling your painting method directly, something you should never do. Instead call repaint() if you want to suggest to the JVM to do painting for you. This is the reason for your NullPointerException (that we now know is occurring). You're passing in a null reference for the Graphics object, and then trying to call methods on it. Don't do this.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • yes it is a Nullpointer exception, why question is can you clean up my code on how it would work? because i dont see what you're trying to tell me the gamestate thing is not important right now because it will always be idle – Sven Tjeerdsma Jun 19 '16 at 01:17
  • @SvenTjeerdsma: you don't seem to be clear on how this site works just yet, but please understand that it is not a "please clean up my code for me" site. We can help answer questions and sometimes spot errors, but you are responsible for cleaning up your own code, and trying to do otherwise can sometimes cause your question to be closed. Just fyi. Please consider going through the [tour] and do take a look at the [help] section as well as the [how to ask good questions](http://stackoverflow.com/help/how-to-ask) section. – Hovercraft Full Of Eels Jun 19 '16 at 01:19
  • @SvenTjeerdsma: answer edited. You'll want to go through the Swing graphics tutorial for more on how to do Swing painting: * [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) – Hovercraft Full Of Eels Jun 19 '16 at 01:23
  • i know how this site works, you dont understand what i mean. my question was meant to be to get an code example, im a beginning programmer and do not understand all the terms, maybe a "change this into that" might work more then all the explanations i won't understand – Sven Tjeerdsma Jun 19 '16 at 01:32
  • @SvenTjeerdsma: and that's exactly what I mean. This isn't a "get a code example site", but rather a question/answer site. Again, please go through the links posted above. Although note that plenty of examples exist for you to look at, many of them mine. [For example](http://stackoverflow.com/search?q=user%3A522444+swing+animation) – Hovercraft Full Of Eels Jun 19 '16 at 01:36