Why Won't the Screen Change Color? - java

I've been trying to fix but it never changes the screen. I'm trying to used the Graphics as seen in the render() method. Tell me if something is wrong inside the render method so I can relax, because I can't seem to find the problem.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.*;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width / 16*9;
public static int scale = 3;
private Thread thread;
private boolean running = false;
private JFrame frame;
public synchronized void start() {
thread = new Thread();
thread.start();
running = true;
}
public synchronized void stop() {
running = false;
try{
thread.join();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
frame = new JFrame();
}
public void run() {
while(running) {
tick();
render();
}
}
void tick() {}
public void render() {
BufferStrategy bs = getBufferStrategy();
if(bs==null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
bs.dispose();
bs.show();
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("Rain");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}

Let me give you a stack trace of your error.
Your render method is not even being called here.
This is because your run method is not being called at all.
The reason behind all this is that you have not passed correct Runnable object at the time of Thread creation. It creates a Thread with empty run.
In your start method, just replace
thread = new Thread();
with
thread = new Thread(this);
And it should work.
Hope this helps. Enjoy.

Related

repaint method not calling paint method

trying to create a rectangle using paint but repaint method's not calling paint method.
even tried replacing paint with paintComponent, but still not working.
So what changes to make to make it work.
is there a problem with calling repaint method in run.
is there a problem with the instance.
trying to create a rectangle using paint but repaint method's not calling paint method.
even tried replacing paint with paintComponent, but still not working.
So what changes to make to make it work.
is there a problem with calling repaint method in run.
is there a problem with the instance.
package src;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class main extends JFrame implements Runnable{
private final int Height = 480;
private final int Width = 640;
Thread gameloop;
public static main instance = null;
private main(){
JFrame frame = new JFrame();
frame.setSize(this.Width,this.Height);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static main getInstance(){
if (instance == null){
instance = new main();}
return instance;
}
private void start(){
gameloop = new Thread(this);
gameloop.start();
}
#Override
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(g2d.getBackground());
g2d.fillRect(0, 0, instance.Width, instance.Height);
g2d.setColor(Color.red);
g2d.fillRect(0, 0, 50, 50);
}
#Override
public void run() {
Thread current = Thread.currentThread();
while (gameloop == current){
try{
Thread.sleep(5);
}
catch (InterruptedException e){
e.printStackTrace();
}
repaint();
}
}
public static void main(String[] args){
main.getInstance();
main.getInstance().start();
}
}
The original problem was that you created a new JFrame in your constructor and then made that visible, instead of using the main instance you just created, so nothing in your main class was actually being displayed in your app, just a blank JFrame that does nothing. Also your run loop made no sense.
However, your code has a lot of other problems, both in logic and style. I'd advise you to just refactor everything. Here's a cleaner version that also solves your problem.
package src;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public final class Main extends JFrame implements Runnable {
private volatile boolean running;
private Main(int width, int height) {
setSize(width, height);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void start() {
running = true;
new Thread(this).start();
}
private void stop() {
running = false;
}
#Override
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(g2d.getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(Color.red);
g2d.fillRect(0, 0, 50, 50);
}
#Override
public void run() {
while (running) {
try {
Thread.sleep(40);
}
catch (InterruptedException e){
e.printStackTrace();
}
repaint();
}
}
public static void main(String[] args) {
new Main(640, 480).start();
}
}

Eclipse: Console is running however no window is appearing

I am following a youtube guide and there are no errors in the code however, when I run the program in the console I have 'running' (I put running loop forever to make sure that the game is working) however no window is popping up for the display.
Here is the code
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;
private Thread thread;
private JFrame frame;
private boolean running = false;
public Game(){
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
frame = new JFrame();
}
public synchronized void start() {
} {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop () {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
while(running){
System.out.println("running");
}
}
public static void main(String[] args){
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("Go Home");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.setVisible(true);
game.start();
}
}
Okay, so here's a lesson, don't create an instance of JFrame inside your component classes (unless you component class is actually going to use it)
game.setVisible(true); is only setting the visibility state of the Canvas, not the frame
Instead, make the JFrame inside your main method instead
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;
private Thread thread;
private boolean running = false;
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
}
public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (running) {
System.out.println("running");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Go Home");
Game game = new Game();
frame.setTitle("Go Home");
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
game.start();
}
});
}
}
Also, your start method is actually empty...
public synchronized void start() {
} /* What's this? */ {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
and the thread is actually been started through the static initialisation of the class, not the method call
The JFrame is the main window that will contain components. It must be set visible.
On your JFrame object:
frame.setVisible(true);

Graphics not Showing

I'm trying to make a test application.First I made the basic things (frame, graphics, textures). When I ran the application it shows an empty window. At first I thought it was from my texture class. But when I tried to set the color black it also didn't worked. Here's my code:
package Main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import Audio.MusicPlayer;
import MultiThreading.ThreadPool;
import Textures.Textures;
public class Main extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
private boolean running = true;
private Thread thread;
public static Main test = new Main();
public static final int height = 842 ;
public static final int width = 595;
public static final int scale = 1;
private Textures background;
public void init(){
background = new Textures("Test paper");
System.out.println("Initiating...");
}
public void run() {
init();
System.out.println("Starting...\nReady!");
running = true;
requestFocus();
while (running) {
//System.out.println("Running...");
render();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
stop();
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.dispose();
bs.show();
g.setColor(new Color(0, 11, 54));
g.fillRect(0, 0, width, height);
background.render(g, 100, 100);
}
private synchronized void stop() {
if (!running) return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(1);
}
public static void main (String args[])
{
ThreadPool pool = new ThreadPool(2);
JFrame frame = new JFrame("Stilul gotic si stilul renascentist");
frame.add(test);
frame.setSize(width / scale, height / scale);
frame.setFocusable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.pack();
MusicPlayer audio = new MusicPlayer("J");
pool.runTask(test);
pool.runTask(audio);
pool.join();
}
}
I checked the multithreading, so it couldn't be from there (the audio file is running and I used same classes for a game). Also, in the console isn't any error...
Can anyone help me?

Why doesn't my screen turn red?

Ok, so I'm new to programming and I'm following a tutorial on Youtube to build my own game. My problem is that my screen doesn't turn red, it just stays gray. I'm sure I did something wrong but there are no errors on eclipse. Here is the code:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;
private Thread thread;
private JFrame frame;
private boolean running = false;
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
frame = new JFrame();
}
public synchronized void start() {
running = true;
thread = new Thread("Display");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (running) {
update();
render();
}
}
public void update() {
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
bs.show();
}
public static void main(String[] args){
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("JJF");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
You never actually provide the Thread with something to run...
public synchronized void start() {
running = true;
// Not the reference to this...
thread = new Thread(this, "Display");
thread.start();
}
By passing this (which is an instance of your Game class which implements Runnable), the Thread will be able to call your run method
nb:
The size of your viewable area should be defined by the component, not the frame. This can be achieved by overriding the getPreferredSize method and returning the preferred viewable size you want the component to be. Otherwise, the viewable area will be the size of the frame minus it's decoration insets, which may not meet your expectations.
In your "game-loop" you should consider having a small delay between cycles to give time for the system to actually update the screen, this takes some of the pressure of the Thread
Runnable example
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;
private Thread thread;
private JFrame frame;
private boolean running = false;
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
frame = new JFrame();
}
public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (running) {
update();
render();
// try {
// Thread.sleep(40);
// } catch (InterruptedException ex) {
// }
}
}
public void update() {
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
bs.show();
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("JJF");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}

JFrame Java setColor and fillRect staying Blank?

Why am I getting a Blank screen instead of Black when I debug or run?? I've looked everywhere and tried a lot! Please help. I'm just trying to make my screen black as I am a beginner to all of this java coding. I don't believe anything is wrong with the code as I'm not getting any errors. I'm using eclipse.
package com.techon.rain;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static int width =300;
public static int height = width / 16 * 9;
public static int scale =3;
private JFrame frame;
private Thread thread;
private boolean running = false;
public Game() {
Dimension size = new Dimension(width*scale, height*scale);
setPreferredSize(size);
frame = new JFrame();
}
public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while(running);{
update();
render();
}
}
public void update() {
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(),getHeight());
g.dispose();
bs.show();
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("Rain");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}
replace
public void run() {
while(running);{
update();
render();
}
by
public void run() {
while(running){
update();
render();
}
due to while(running); it is not executing other stetement inside loop.

Categories

Resources