Timppa Timppa - 5 months ago 12
Java Question

The method is undefined for the type JFrame

I am encountering two problems with Eclipse!
Everything worked fine months ago before replacing my broken hardware, I used to play a lot with Eclipse and learnt new stuff. However, now it seems like nothing works.

I followed this tutorial: https://www.youtube.com/watch?v=1gir2R7G9ws and copy pasted everything and now I am getting an error:


Error: Could not find or load main class


Then I started to google the solution for 2 days, it only suggested to check CLASSPATH but I wasn't very sure what to type in CMD since I have a laptop that is borrowed from school and has certain restrictions to access certain areas in my computer and the workspace is in a folder that requires administrator's priviledges.
Then I tried to make a normal HelloWorld class and it worked perfectly without any Could not find main class errors.

So I guess the problem is in the code, or is it?

Anyways, the script is here, for Window class:

package GamePackage;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Window extends Canvas {

private static final long serialVersionUID = 4882660542739611206L;

public Window(int width, int height, String title, Game game) {

JFrame frame = new JFrame(title);

frame.setPreferredSide(new Dimension(width, height));
frame.setMaximumSide(new Dimension(width, height));
frame.setMinimumSide(new Dimension(width, height));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(game);
frame.setVisible(true);
game.start();

}

}


And for the Game class:

package GamePackage;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;


public class Game extends Canvas implements Runnable {

private static final long serialVersionUID = 3429322092557125719L;

public static final int WIDTH = 640, HEIGHT = WIDTH / 16 * 9;

private Thread thread;
private boolean running = false;

public Game () {
new Window(WIDTH,HEIGHT, "Game Project", this);
}

public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}

public synchronized void stop() {
try{
thread.join();
running = false;
}catch(Exception e) {
e.printStackTrace();
}

}

public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
delta--;
}
if(running)
render();
frames++;

if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}

private void tick() {

}

private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}

Graphics g = bs.getDrawGraphics();

g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);

g.dispose();
bs.show();
}

private static void main(String[] args) {
new Game();
}
}


For Window class, I get this error:

The method is undefined for the type JFrame


So I am now thinking that can the reason be that error? I couldn't fix it anyhow and for normal HelloWorld class the program works properly.

Any ideas? Sorry I am very unprofessional with Eclipse.
Btw, I copy pasted everything to netBeans and yet got the same Main class error (and yes, I changed the class names and everything to match the netbean project).

Thanks in advance!

Answer

You have a typo in your code! You have to write:

frame.setPreferredSize(new Dimension(width, height));
frame.setMaximumSize(new Dimension(width, height));
frame.setMinimumSize(new Dimension(width, height));

instead of Side. So you basically wrote a "d" instead of "z".