How to pass parameters between Applet classes in Java with Processing - java

I have four coordinates saved in the main class as global Variables. How do I access getters or give parameters to the new Applet? It isn't possible to create an instance because the values of the main class are getting set before the code of the second Applet is run.
When the mouse is pressed and when its released the positions are saved in my main Applet. By releasing the mouse button a second Applet in another Java Class gets opened.
The goal is second Applet being the size of the rectangle the mouse just dragged.
public void mousePressed() {
setStartDetectionAreaX(mouseX);
setStartDetectionAreaY(mouseY);
}
public void mouseReleased(){
setEndDetectionAreaX(mouseX);
setEndDetectionAreaY(mouseY);
PApplet.main("SecondWindow");
}

Okay I found a solution. It it possible to give a String Array as a parameter while calling the other Applet.
Like this:
PApplet.main("SecondWindow", new String[]{pm1, pm2, pm3, pm4};
In the other class its know possible to get the array like this:
args[0]
Another possibility would be setting a static variable and change it in the other class.

Related

Handling button event from another class in Jframe

I am using netbeans to make a GUI. To make it simple, let say what I want to do is that when I press a button it just opens a new JFrame and print a text on a JPanel, a text entered by the user.
It sounds easy if I just complete the automatically generated function from Netbeans :
private void popUpButtonActionPerformed(java.awt.event.ActionEvent evt) {
// I added these three lines
JFrame newFrame = new myPopUpWindow();
myPopupWindow.getTextLabel().setText("Hello" + enteredText.getText());
this.dispose();
}
And everything works as I want. Now what I want to do is having a cleaner code. I want to put these two line in a class ButtonHandler.java in a function handleHelloPopUp(String enteredStringText) Now comes the dilemma:
If I make handleHelloPopUp static, I get some error when I access some non static variable
If I don't make handleHelloPopUp I just have to pass it as argument in my Jframe -> horrible code structure
What is the best way to do this ? Please help ...

2D Game not repainting

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

How to get a MouseListener to stop being in control

I am writing a trivia/quiz program in which the user answers questions and his/her answers are judged as correct or incorrect based on the screen coordinates they have clicked. For a clean design, I wish to load the questions using an external function, then attach a MouseListener to the screen object afterward.
My code currently resembles that given below.
void main() {
screen = new QuizScreen();
//load data for question
screen.awaitAnswer();
}
public class QuizScreen implements MouseListener{
//...variables...
//...blank methods for MousePressed, MouseReleased, etc...
public void mouseClicked(MouseEvent me){
//check if answer is right
}
public void awaitAnswer(){
QuizScreen.addMouseListener(this);
}
}
This code works fine for loading, allowing play of, and checking the answer to a single question. However, after the first question has been loaded, I want to be able to repeat the process -- to load ANOTHER (and possibly many more) questions -- by adding a loop to the "main" function. This is currently not possible, since I don't know how to get the MouseListener to stop listening to the user's clicks and return from awaitAnswer() to main().
How do I stop getting the MouseListener to listen to the user? How would I get out of an event-driven section of my code and back to an automatically executing section?
Remove the awaitAnswer method.
Create a (default) constructor for QuizScreen with statement addMouseListener(this);.
From your mouseClicked function, just execute a callback to your main program to create and destroy the QuizScreen

java: how can i pass a variable from the starter JFrame to the last without having to pass it through all JFrames?

I have this problem: I have an ArrayList that contains a few items that I want to use in a particular frame, the problem is that the array list gets full by initializing it in the main class of my project. In this class I also launch my starting frame that is chained with other frames (login frame -> middle frame --> last frame). I want to carry this ArrayList without having to carry it on through all frames and get it directly usable from main --> last frame. How can I do this?
EDIT
Wat I did it was like first frame start with this ArrayList as parameter:
Jframe jf = new LoginFrame(arraylistvariable,"Login Window");
Then in all ActionListener calls on the buttons that create new frames, disposing old ones, I set it like:
Jframe jo = new MiddleFrame(arraylistvariable,"Middle Window");
Passing this variable all over the frames but I want this to be like called only by the frame that needs this, because login frame doesn't need this variable. However, it is necessary to start the program by the login frame.
"However, it is necessary to start the program by the login frame."
No it's not.
public class MiddleFrame() {
private LoginFrame;
}
...
public static void main(String[] args){
new MiddleFrame();
}
Make the middle frame not visible upon instantiation, but make the LoginFrame visible. If the login is successfful, but make the MiddleFrame visible.
Note You don't need to use to many frame. Make use of JDialogs. See this answer for How to make a Login with a JDialog
one from the easiest way it to pass it/them trough system properties
You might want to create some class like ArrayListVariableProviderService and make it accessible either to all classes (JFrames) or via static call.
This can act as a container to many things, yet it'll abstract out all peculiarities.
Good luck.

Java with JFrame and class interaction

I have two classes in the same Package. The package is called main. Main contains two classes a Main class and a Display class. The display class was created to display a GUI with text boxes and buttons. I gave the buttons a listener that fires when the user clicks one of the button. In the main class is a vector of object that i am storing and need to display one a time in the display class' text boxes.
My Question is: can the mouse clicked action call a method in the main class that gathers the needed info an passes that back to a method in the display class to modify those text boxes> Do i need to combine my two classes into one? How would i do within the classes?
From testing i have made the main class extend the display class. I am able to start the display class from here but when i try to call a method in display from main it does nothing. If i try to call a main method from the display class it also seems to do nothing.
If you want your mechanic to fix your car (by starting it, diagnosing the problem, opening the hood, etc.), you give your car to the mechanic, don't you?
It's the same in Java. If you want the Display object (the mechanic) to access to information available in the Main object (the car), you need to give the Main object to the Display:
Main main = new Main(); // main contains data
Display display = new Display(main) // Display is constructed, and is given the main object
in Display:
public void someButtonClicked() {
String someInformation = this.main.getSomeInformation();
this.someTextField.setText(someInformation);
}

Categories

Resources