I am creating a java program in eclipse but whenever I try to run it it terminates instantly please help. In the console next to where it says the java location it says terminated
Main Code:
package com.revert.main;
import java.awt.Canvas;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = -2457117434114507933L;
public static final int WIDTH = 640, HEIGHT = WIDTH /12 * 9;
private Thread thread;
private boolean running = false;
public Game(){
new Window(WIDTH, HEIGHT, "Death By Block", 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(){
}
public static void main(String args[]){
}
}
Window Code:
package com.revert.main;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Window extends Canvas {
private static final long serialVersionUID = 5224458545146664877L;
public Window(int width, int height, String title, Game game){
JFrame frame = new JFrame(title);
frame.setPreferredSize(new Dimension(width, height));
frame.setMinimumSize(new Dimension(width, height));
frame.setMaximumSize(new Dimension(width, height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.add(game);
frame.setVisible(true);
game.start();
}
}
Try this
public static void main(String args[]) {
new Game();
}
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 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:
I am trying to make a simple side-scrolling game and my background color won't work. It should fill the entire page, with an fps counter, but it only has a small line of color at the top of the pop up.
Here is my entire code so far (I had to copy and paste because I'm "not a level 10").
public static final int WIDTH = 640;
public static final int HIGHT = WIDTH / 4 * 3;
public static final String TITLE = "Sploocher's Epic Quest";
private static Game game = new Game();
private boolean running = false;
private Thread thread;
public void init(){
}
public void tick(){
}
public void renderBackground(Graphics g){
}
public void renderForeground(Graphics g){
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0,0,WIDTH,HEIGHT);
//////////////////////////////////////////////////
renderBackground(g);
renderForeground(g);
g.dispose();
bs.show();
}
#Override
public void run() {
init();
long lastTime= System.nanoTime();
final double numTicks = 60.0;
double n = 1000000000 / numTicks;
double delta = 0;
int frames = 0;
int ticks = 0;
long timer = System.currentTimeMillis();
while(running){
long currentTime = System.nanoTime();
delta += (currentTime - lastTime) / n;
lastTime = currentTime;
if(delta >= 1){
tick();
ticks++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer+=1000;
System.out.println(ticks + "Ticks, FPS: " + frames);
ticks = 0;
frames = 0;
}
}
stop();
}
public static void main(String args[]){
JFrame frame = new JFrame(TITLE);
frame.add(game);
frame.setSize(WIDTH, HIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.pack();
game.start();
}
private synchronized void start(){
if(running)
return;
else
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop(){
if(!running)
return;
else
running = false;
try {
thread.join();
} catch (InterruptedException e){
e.printStackTrace();
}
System.exit(1);
}
}
If anyone could help me figure out how to fix my code, I'd really appreciate it.