I am having issues with a 2D platform game. I get an error that reads what the title of the question said. Here are my Game.java and Window.java files. Please tell me what I should do.
I've tried a ton of things and I just don't know where to go or what to do. Thanks in advance :)
Window.java
package com.sam.platform.window;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Window
{
public Window(int w, int h, String title, Game game)
{
game.setPreferredSize(new Dimension(w, h));
game.setMaximumSize(new Dimension(w, h));
game.setMinimumSize(new Dimension(w, h));
JFrame frame = new JFrame(title);
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
}
Game.java
package com.sam.platform.window;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Window;
import java.awt.image.BufferStrategy;
import com.sam.platform.framework.ObjectId;
public class Game extends Canvas implements Runnable
{
private static final long serialVersionUID = -414187095722102896L;
private boolean running = false;
private Thread thread;
public static int WIDTH, HEIGHT;
//Object
Handler handler;
private void init()
{
WIDTH = getWidth();
HEIGHT = getHeight();
handler = new Handler();
handler.addObject(new Player(100, 100, handler, ObjectId.Player));
handler.createLevel();
this.addKeyListener(new KeyInput(handler));
}
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public void run()
{
init();
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS:" + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}
}
private void tick()
{
handler.tick();
}
private void render()
{
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//////////////////////////////////
//Draw Here
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
handler.render(g);
//////////////////////////////////
g.dispose();
bs.show();
}
public static void main(String args[]){
new Window(900, 900, "Hop", new Game()); //error is here "The constructor Window(int, int, String, Game) is undefined"
}
}
Your Window class is fine, but the class you're importing in Game is java.awt.Window.
You could solve this by doing new com.sam.platform.window.Window(...), but I would advise against it, it will just confuse you.
Rename the class to something like GameWindow instead.
You have two different classes called Window in use in your Game class. One is com.sam.platform.window.Window. The other is java.awt.Window. Since you have imported java.awt.Window into your Game class, it thinks you are trying to instantiate one of those (not your own Window class).
I suggest renaming you own class to disambiguate (and avoid confusion) to, say, GameWindow.
Related
If I create a window with JFrame, several rows of pixels along the bottom and several columns along the right hand side are outside the window that is created so they don't show up. For example when I create a 800x600 window, the window will only show stuff in the top left 786x563 area. The amount that gets cut off also seems to vary with the size of the window. So far I've just manually adjusted the size and position of things. Currently in my code is set up like this:
import java.awt.*; //Canvas and Dimension
import java.util.*;
import javax.swing.JFrame;
import java.awt.image.BufferStrategy;
import java.awt.event.*;
public class Main extends Canvas implements Runnable {
static final int WIDTH = 800, HEIGHT = WIDTH/4*3; //800, 600
private Thread thread;
private boolean running = false;
public Main() { //constructor
new Window(WIDTH, HEIGHT, "Game", this);
requestFocus();
}
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;
frames = 0;
}
}
stop();
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs==null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//creates a line that should be 38 units above the bottom but instead is right along the bottom
g.setColor(Color.GREEN);
g.fillRect(1, 562, 30, 1);
g.dispose();
bs.show();
try{Thread.sleep(5);} catch(Exception e) {}
}
public void tick() {
}
public static void main(String args[]) {
new Main();
}
}
class Window extends Canvas {
public Window(int w, int h, String t, Main game) {
JFrame frame = new JFrame(t);
frame.setPreferredSize(new Dimension(w,h));
frame.setMinimumSize(new Dimension(w,h));
frame.setMaximumSize(new Dimension(w,h));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
game.start();
}
}
If I didn't include enough of my code to diagnose the problem please tell me so I can put the rest of it.
I'm using this youtube tutorial: https://www.youtube.com/watch?v=1gir2R7G9ws
Here is the compiler error:
Game.java:18: error: cannot find symbol
new Window(WIDTH, HEIGHT, "Let's Build A Game!", this);
^
symbol: class Window
location: class Game
Here is my Game.java code:
package com.tutorial.main;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Canvas;
import java.awt.image.BufferStrategy;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 8073316534757788976L;
public static final int WIDTH = 640, HEIGHT = WIDTH/12*9;
private Thread thread;
private boolean running = false;
public Game(){
new Window(WIDTH, HEIGHT, "Let's Build A Game!", 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 = 100000000 / 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();
}
public static void main(String args[]){
new Game();
}
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();
}
}
Here is my Window.java:
package com.tutorial.main;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Window extends Canvas{
private static final long serialVersionUID = -3359827712233484029L;
public Window(int width, int height, String title, Game game){
JFrame frame = new JFrame(title);
frame.setPreferredSize(new Dimension(width, height));
frame.setMaximumSize(new Dimension(width, height));
frame.setMinimumSize(new Dimension(width, height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(game);
frame.setVisible(true);
game.start();
}
}
Why isn't the instance recognized?
There are is a java core class called Window so should rename your Class to GameWindow or Something else. The Compiler probably gets confused because the core class called Window has a different Constructor so just rename it.
I've been working on a game but messed something up and I can't figure what it is. I've simplified the code as much as possible in this example and it retains the same issue.
In this example, instead of the red square going upwards without leaving a trace, it leaves behind a red trace meaning the graphics are not being disposed of properly or the buffer strategy isn't working.
How do I make the red square not leave behind a trail?
import java.awt.*;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class game extends Canvas implements Runnable {
private boolean running = false;
private int w = 1920/2, h = 1080/2, move = 300;
private JFrame frame;
private Thread thread;
public game() {
frame = new JFrame("Test");
frame.setPreferredSize(new Dimension(w,h));
frame.setMaximumSize(new Dimension(w,h));
frame.setMinimumSize(new Dimension(w,h));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(this);
frame.setVisible(true);
start();
setBackground(Color.black);
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
delta--;
}
if(running) render();
}
}
private void tick() {
move--;
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.red);
g.fillRect(300, move, 50, 50);
g.dispose();
bs.show();
}
public static void main(String[] args) { new game(); }
}
How do I make the red square not leave behind a trail?
g.setColor(Color.red);
g.fillRect(300, move, 50, 50);
Before you draw your red square you need to paint the entire background of your canvas.
g.setColor(...);
g.fillRect(...);
g.setColor(Color.red);
g.fillRect(300, move, 50, 50);
I've been trying to get the background colour of my jframe to turn blue, but all it does is show the default jframe colours. How do i get the background to actually display blue?
In other words, it won't allow me to get a blue background but the jframe will open with no colour whatsoever.
package DisplayPackagev1;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Sea_InvadersDisplay extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static void main(String[] args){
Sea_InvadersDisplay display = new Sea_InvadersDisplay();
JFrame frame = new JFrame();
frame.add(display);
frame.pack();
frame.setTitle("Sea Invaders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
display.start();
}
private boolean running = false;
private Thread thread;
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {e.printStackTrace();}
}
public int FPS;
public Sea_InvadersDisplay(){
this.setSize(1300, 690);
this.setFocusable(true);
}
#Override
public void run() {
long timer = System.currentTimeMillis();
long lastLoopTime = System.nanoTime();
final int TARGET_FPS = 60;
final long OPTIMAL_TIME = 999999999 / TARGET_FPS;
int frames = 0;
this.createBufferStrategy(3);
BufferStrategy bs = this.getBufferStrategy();
while(running){
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
// double delta = updateLength / ((double) OPTIMAL_TIME); means that when I update it, it doesn't jump.
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
FPS = frames;
frames = 0;
System.out.println(FPS);
}
draw(bs);
try{
Thread.sleep(((lastLoopTime - System.nanoTime()) + OPTIMAL_TIME) / 999999999);
} catch(Exception e){};
}
}
public void draw(BufferStrategy bs){
do {
do {
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
}while(bs.contentsRestored());
bs.show();
} while (bs.contentsLost());
}
// Buffer Strategy, way to use Buffer Damage so that there isn't any flickering. ALos takes up less resources
}
when you add your Sea_InvadersDisplay you fill all area of that frame with this component. So even if your JFrame background would have any background color it would ne be visible because of the overlaying Sea_InvadersDisplay.
instead try to set the background color of your Sea_InvadersDisplay.
public static void main(String[] args){
Sea_InvadersDisplay display = new Sea_InvadersDisplay();
display.setBackground( Color.BLUE ); //set background on display instead
JFrame frame = new JFrame();
...
display.start();
}
frame.getContentPane().setBackground( Color.BLUE );
You must call the above line.
For example:
public static void main(String[] args){
Sea_InvadersDisplay display = new Sea_InvadersDisplay();
JFrame frame = new JFrame();
frame.add(display);
frame.pack();
frame.setTitle("Sea Invaders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.getContentPane().setBackground( Color.BLUE );
frame.setVisible(true);
display.start();
}
I'm learning to make games using a a tutorial on youtube. Everything seems fine except when I run the program. A frame shows up with the accurate width I want but the height looks like it sets to a default no matter what value I give it.
package ca.vanzeben.game;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 160;
public static final int Height = WIDTH/ 12*9;
public static final int SCALE = 3; // able to move screen
public static final String NAME = "Juego";
private JFrame frame;
public boolean running = false;
public int tickCount = 0;
private BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
public Game() { //game constructor
setMinimumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
setMaximumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // closes game completely
frame.setLayout(new BorderLayout());
frame.add(this,BorderLayout.CENTER); //adds canvas to JFrame and centers it
frame.pack();//keeps everything sized correctly
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public synchronized void start() {//so u can start from the applet
running = true;
new Thread(this).start();
}
public synchronized void stop(){
running = false;
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000.0/60; //nanoseconds per tick or per update
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0.0; //how many unprocessed nano seconds
while(running){
long now = System.nanoTime();
delta+=(now-lastTime)/nsPerTick;
lastTime = now;
boolean shouldRender = true;
while(delta>=1){
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
try{
Thread.sleep(2);
}catch(InterruptedException e){
e.printStackTrace();
}
if(shouldRender){
frames++;
render();
}
if(System.currentTimeMillis()-lastTimer>=1000){
lastTimer += 1000;
System.out.println(frames + " frames " + ticks + " ticks ");
frames = 0;
ticks = 0;
}
}
}
public void tick(){ //updates the game, updates the logic
tickCount++;
for(int i =0;i<pixels.length;i++){
pixels[i] = i + tickCount;
}
}
public void render(){ //prints out ^
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth(), getHeight());
g.dispose();
bs.show();
}
public static void main(String[] args){
new Game().start();
}
}
You defined Height and used HEIGHT. Change it to:
public static final int HEIGHT = WIDTH / 12 * 9;
This is how it looked like to me:
Before:
After: