Yeah, that method isn't so good. It would be better to create a new instance of the class with a non-static version of that variable within the Main static method. It would make your life much easier.
Here is an example of something I would do:
Main Classpublic class Main {
public Main() {
}
static Main main = new Main();
public MainScreen mainscreen;
public static void main(String args[]) {
main.mainscreen = new MainScreen(main);
}
}
MainScreen Classimport java.io.*;
import java.net.*;
import java.lang.Thread;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.lang.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.List;
import java.util.*;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.geom.*;
public class MainScreen extends javax.swing.JFrame implements Runnable {
public Runnable runnable = this;
// Create the thread supplying it with the runnable object
public Thread Thread = new Thread(runnable);
//The mouse Input
public int mx, my; // the mouse coordinates
public boolean isButtonPressed = false;
//Keyboard input
public boolean[] keys = new boolean[120];
public String pressed = "";
public void run() {
while (true) {
canvas1.repaint();
try {
Thread.sleep(50);
}catch (Exception e){}
}
}
public MainScreen() {
}
Main main;
public MainScreen(Main main) {
this.main = main;
initComponents();
this.setVisible(true);
Thread.setDaemon(true);
Thread.setPriority(Thread.MIN_PRIORITY);
Thread.start();
canvas1.requestFocus();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainScreen().setVisible(true);
}
});
}
// Variables declaration - do not modify
public java.awt.Canvas canvas1;
private javax.swing.JButton jButton1;
// End of variables declaration
}
class GCanvas extends Canvas {
//For Double Buffering
private Image dbImage;
private Graphics dbg;
public MainScreen main;
public Graphics2D g2;
AffineTransform affineTransform = new AffineTransform();
Dimension d;
public GCanvas(MainScreen main) {
this.main = main;
}
public void paint(Graphics g) {
g2 = (Graphics2D) g;
d = this.getSize();
g2.setColor(Color.RED);
g2.drawString("hi", 100 ,100);
}
}
You see, I have my blank Main class which is static, inside it's static-ness; I have it create a non-static instance the the Application window, "Main Screen". On MainScreen is a canvas I made which will display the text, "Hi". It updates the canvas every few milliseconds. If you copy the code exactly, it won't work because I erased a bunch of Gui stuff that hogs space when posted, though if you copy the way I set it up; it will work wonderfully for you.
-Gandolf