Question:
How do i pass variables between java classes?
Cepek.Bengals 64
2012-02-15 10:58:19 UTC
I can't seem to get this to work~ Please ignore the extra parts, this is my skeleton for a very large project.

import java.awt.*;
import java.awt.font.*;
import java.util.Random;
import java.applet.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Math;
import java.applet.Applet;
public class shooterMain extends Applet implements KeyListener
{double h = 0,w = 0;
public void test(final GetScreenSize width){

GetScreenSize.findWidth(w);
GetScreenSize.findHeight(h);}
public void stop()
{
}
public void init()
{
this.setSize(w,h);
Cursor CROSSHAIR_CURSOR = new Cursor(Cursor.CROSSHAIR_CURSOR);
setCursor(CROSSHAIR_CURSOR);

setFocusable(true);
addKeyListener(this);
setBackground(Color.black);

}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if(code == 87) {//W
}
if(code == 65) {//A
}
if(code == 83) {//S
}
if(code == 68) {//D
}
e.consume();
}
public void setCursor(Cursor CROSSHAIR_CURSOR) {
}
public void setSize(double w2, double h2)
{
w2=w;h2=h;
}




public void paint(Graphics g)
{
}









@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}


}



The screensize ( Working perfectly)

import java.awt.*;
public class GetScreenSize
{
public static void main(String args[])
{
}
public static double findWidth(double width)
{
double W=0;
// Get the default toolkit
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
Dimension dim = toolkit.getScreenSize();
W=dim.width;
width=W;
return width;
}

public static double findHeight(double height)
{
double H=0;
// Get the default toolkit
Toolkit toolkit = Toolkit.getDefaultToolkit();
// Get the current screen size
Dimension dim = toolkit.getScreenSize();
H=dim.height;
height=H;
return height;
}
}
I've run it in eclipse and textpad++ and i have no errors, but i am getting a 200x200 it seems applet window instead of my screen resolution.. (1280x800)
Three answers:
deonejuan
2012-02-15 11:04:55 UTC
applet's are sized by the in the html



you cannot resize an applet once it is initialized



you can launch a JFrame from the applet. For an example of that, look inside your JDK folder

/jdk1.7.0_01/demo/jfc/Java2Demo
?
2012-02-15 11:17:44 UTC
Well, you can declare variable as public static in your main class, then they act as global variables that all the classes can access and modify. This is considered poor form, and should not be used unless it is necessary for the specific program, though it is certainly the easiest.

The more proper way is to call public methods in your other classes and pass the variables as parameters. They will be passed as references, and the other class can modify them that way.
ginder
2017-01-13 17:32:31 UTC
in case you declared static String bob = "ah_whoosh! hoo-ah!"; then you certainly can use equipment.out.println( Touchdowns.bob ); static potential the JRE plenty it first and inits it. there is merely one replica of that fee no count how many copies of the article get built. in case you do no longer choose static, do like all different answer mentioned, declare a pointer to an merchandise.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...