Reputation: 21
ok i have 2 classes This is my main class it opens a JFrame and draws some stuff to the JFrame:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class MainWindow extends Canvas{
public static int HEIGHT = 600;
public static int WIDTH = 600;
public static void main(String[] args){
MainWindow Window = new MainWindow();
JFrame Frame = new JFrame();
Frame.add(Window);
Frame.pack();
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);
Frame.setSize(WIDTH, HEIGHT);
Frame.setLocationRelativeTo(null);
Frame.setResizable(false);
Frame.setTitle("Untitled Build 0.01");
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.GRAY);
g.drawString("Sellect A Difficulty", 100, 25);
g.drawString("Please input a number from 1 - 3", 100, 40);
g.drawString("1. Easy", 100, 55);
g.drawString("2. Medium", 100, 70);
g.drawString("3. Hard", 100, 85);
g.setColor(Color.BLACK);
}
}
This is my 2nd class it is for setting the difficulty of the game but i need the main class to call it but i am not sure how to get it to do that.
public class Difficulty {
public static final int input = 0;
static int NoInput = 1;
public static int Difficulty = 0;
@SuppressWarnings("unused")
public static void main(String[] args){
if(NoInput == 1){
//draw text to screen here
//TODO Write text to screen here about selecting difficulty
Difficulty = Keyboard.readInt();
if(input == 1){
Difficulty = 1;
}else if(input == 2){
Difficulty = 2;
}else if(input == 3){
Difficulty = 3;
}else if(input < 0 | input > 3){
//TODO draw "please input a number between 1 and 3 try again...." to screen
}
}
}
}
Upvotes: 2
Views: 84
Reputation: 61
Hovercraft Full Of Eels (nice name btw) already answered this, but I'll add in my 2 cents anyway :P
The second class uses public static void main(String[] args) this implies that it will be run separately.
A quick fix might be to make a constructor for that class, and then calling it via an object.
public class Difficulty {
public Difficulty(){
// Code here
}
}
Calling it:
Difficulty difficulty = new Difficulty();
This will call the constructor as soon as the object is created, therefore executing your code when you choose.
Upvotes: 2
Reputation: 464
Why do you have two main voids? You should be using a constructor for the difficulty class. Put all of the code into a method like this:
public Difficulty() {
//All your code here
}
You may call this method by creating a new instance of this class.
Difficulty object = new Difficulty();
The constructor will automatically be called when you create the object.
Upvotes: 2
Reputation: 285403
Your second class is little more than a program with a single static main method, and this won't work. Suggestions:
Upvotes: 2