2D Game not repainting - java

I checked the other questions and couldn't find one with the same case as me so here's my question.
I am making a 2 player stick fighting game that you can play on the same computer using different keys. Everything's fine but when I try to move the oval on the screen with the keys, it's not moving.
Here's the code for my first class - http://pastebin.com/wA0JXdzr
second class - http://pastebin.com/ArByyirt
I think I need to call repaint in my second class in the gameloop, but it's saying that it can't make a static reference to it.

You are trying to call a non static method directly from another class which is not legal in java. The paint() method in your first class is the non-static method. You were able to use the variables stickx2 and such because they are static as defined in your first class.
Thus, I suggest you create an object of stickFrame() in the gameLoop class and copy all your code in your stickframe main method and put it in your gameLoop main method. It is highly not recommended that you have two main methods.
Declare a Stick Frame variable below your Serialization ID.
StickFrame s;
Then instantiate it in your gameLoop constructor
s = new StickFrame();
Now we need to fix the repaint from another class problem.
To do this we need a method in the gameLoop Class.
public void repaintStickFrame()
{
s.repaint();
}
Then call it by
s.repaintStickFrame() in your loop.
or you can just call
s.repaint();//place in loop
Heres a link to a question that is similar to yours and has solutions as well
Calling repaint from another class JFrame
Heres a link that explains how you can call an objects method once you've created one (Like we did above, which allowed us to call the repaint() method from a different class):
https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html

Related

Overloading paintComponent()

I'm currently making a basic connect four game with a GUI and I'm trying to figure out how I can place the pieces on the board. I have a method that can locate which space the user would like to put the piece on, but I'm unsure how to paint the pieces in. Since paintComponent() is always called without an actual method call and the default constructor only takes a Graphics object, how could I overload paintComponent() so that it can take different arguments (these arguments are the location of the piece) and the compiler will know to call my new paintComponent() method?
how could I overload paintComponent() so that it can take different arguments
You can't. Or rather, you can create an overloaded method, but the JRE won't call it.
Instead, the code needs to keep a model of the game state, when it changes, call repaint(). In the normal paintComponent(Graphics) method, use the model to determine how it should be drawn.
Obviously, for this to work, the model must be within the scope of the method - so you might make it a class attribute.

Firing a method in my Controller class from a button in my JFrame class

So I'm doing a project for school where I'm supposed to simulate a supermarket. I'm pretty far into it, but I came across a problem I haven't had before. Long story short, (among others) I have a controller class and a JFrame class.
In the main class and method, I have an object of the Controller class. In the controller cass I have an object of the JFrame class. When I run the project, the main makes a controller, which makes a JFrame.
Now, I want to add a button into the JFrame that signals the controller I want to 'move on' the simulation. I'm kind of stumped as to how to accomplish this. Basically, I want to add a button into the JFrame that will call a method in the controller class when clicked.
I'm happy to post code if necessary, but it's more a problem of 'how do I do this?' than fixing something that is broken.
Extend the JFrame with your own JFrame, which has an overloaded constructor that takes an instance of the Controller. Then when you create an instance of the JFrame from the Controller you pass it a reference to itself using the "this" keyword. When the button inside the JFrame needs to notify the controller it should be able to call some getController() method. Of course, this is all conjecture without seeing some actual code :)

Passing variables within several JFrames and classes

sorry guys, but I got confused after many hours sitting ang writting the code.
Situation:
I have two JFrames. These are different Java classes - one of them is the FirstGUI, from which we can call the other JFrame called SecondGUI. OK - that's clear.
I also have one class called Processor in which i have specific methods like "connectToPort" or "disconnectFromPort".
Also, in the FirstGUI (which has the main method) I'm creating SecondGUI object (and set setVisible to false) and Processor object with FirstGUI and SecondGUI as parameters.
Problem:
From the FirstGUI in I want to call out SecondGUI (by setVisible to true) - OK, done. But what about calling out the created at the beginning Processor object from the SecondGUI JFrame? It's important to call the SAME object, because Processor methods can for example set text in the FirstGUI JFrame.JTextPane component, and add items to JComboBox of the SecondGUI.
I don't know how to solve this, I'm always getting NullPointerException.
EDIT:
I want to add that I can't pass the Processor object as an argument while creating SecondGUI, because Second GUI is created earlier and it is an argument while creating Processor... That's the problem.
When constructing the second GUI (child), the initiating class (FirstGUI) can pass self in constructor, and also retain the reference to the constructed object. Now both GUIs have the reference to each other:
class F1 extends JFrame {
F2 child;
void createF2() {
child = new F2(this);
child.setVisible(true);
}
}
class F2 extends JFrame {
final F1 parent;
F2(F1 parent) { this.parent = parent; };
}
If you've searched out your problem or for similar problems on this site, you'll know that the most common recommendation is not to use multiple JFrames, that it is suggestive of a bad design. Better to either swap views with a CardLayout or use JDialogs of the appropriate modality.
As for your question, having one class pass information dynamically to another class, there are two main options depending on program structure.
If one class is in a modal JDialog, then the first class can pull information from the second modal class by calling appropriate getter methods after the second window is no longer visible.
If one class is displayed non-modally, then you'll want to use some type of listener such as a PropertyChangeListener to have the listening class be notified by the observed class when state changes occur.
Edit
Regarding:
From the FirstGUI in I want to call out SecondGUI (by setVisible to true) - OK, done. But what about calling out the created at the beginning Processor object from the SecondGUI JFrame? It's important to call the SAME object, because Processor methods can for example set text in the FirstGUI JFrame.JTextPane component, and add items to JComboBox of the SecondGUI.
Audrius gives you an answer for that. 1+ up-vote given to his answer.
I don't know how to solve this, I'm always getting NullPointerException.
If you get a NPE, should carefully inspect the line that throws the NPE to see which variable is null and then trace back in your code to see why. If your still stuck on a NPE and need our help, then you'll want to show pertinent code and give us more detail on the problem including noting which variable is null and why you think that it shouldn't be null.
EDIT: I want to add that I can't pass the Processor object as an argument while creating SecondGUI, because Second GUI is created earlier and it is an argument while creating Processor... That's the problem.
This is a non-issue. Since the dependent window is displayed dynamically, you can always pass a reference just prior to displaying it using a setter method.

Calling non-static methods using NetBeans GUI builder action listener

I'm quite new to Java and programming, but I'm trying to learn by doing. To avoid too much hassle early-on I've used the NetBeans GUI builder to make a basic form with buttons and labels etc.
My problem is that when I call some methods inside the auto-generated Action Listener for a button, I get an error telling me how I cannot call a non-static method ( the dispose() method ) from a static context. While I understand the distinction between static and non-static in theory, I find myself lost when I'm sitting at the keyboard. I feel like I'm missing something important.
I need help getting un-stuck on this particular problem before I can move forward.
Thanks
well you cant just call dispose() as this is an instance method, you need to call dispose on the instance of your JFrame or JDialog or what-ever-window like this:
frame1.dispose():

Accessing Swing components of another class

I have two classes gameWindow and gameEngine. The Main method is in gameWindow class & so is the swing GUI code. Now, I want to access the swing components in gameEngine. How to do that? I always get the cannot find symbol error when I try it normally. I've tried making the components public but with no luck.
Also, I tried creating a instance of gameWindow but it too, didn't help. It compiled without any error but I got a BIG runtime error(which I can't even see, the command prompt gets scrolled to it's limit).
RELP!!
P.S.: I don't think the posting the code here will help.
In order to access the components of gameWindow in gameEngine you need a reference to it. So I assume that GameWindow class creates the instance of GameEngine. Then you can do something like this:
public class GameEngine{
GameWindow window;
public GameEngine(GameWindow gm){
window = gm;
}
//rest of your code
}
public class GameWindow(){
//At the point where you create the GameEngine
GameEngine ge = new GameEngine(this);
//rest of your code
}
Now GameEngine has a reference to the GameWindow that it can use.
Having said that, I would advise that you consider a different design. Having GameEngine directly access the GameWindow sounds as if the wrong classes are responsible for the wrong things. Maybe you might try looking at using some sort of Observer such that the GameWindow looks for changes in the GameEngine then accesses the updates and modifies the view accordingly. So the GameEngine does not have to access the swing components of GameWindow.
Pass a reference to gameEngine in the contructor or use a getter/setter type method.
For example, in gameWindow, when you create the gameEngine class, have a constructor that takes a gameWindow variable.
gameEngine:
gameWindow theMainWindow = null;
public gameEngine(gameWindow mainWindow)
{
theMainWindow = mainWindow;
}
Vincent's answer is correct, but involves instantiating GameWindow which the post said gives an error. I would advise redirecting your error stack trace to a file, reading it and troubleshooting your runtime error, then following his instructions.

Categories

Resources