Why Won't the Screen Change Color

Junkie

I looked and the codes seems fine to me. Got an error but hopefully it's the source code, not something wrong with the cpu I have nor JDK.

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 {
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() {
    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){
        tick();
        render();
    }
}

public 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 Game() {
    Dimension size = new Dimension(width * scale, height * scale);
    setPreferredSize(size);

    frame = new JFrame();
}

public static void main(String[] args) {
    Game game = new Game();

    game.frame.setResizable(false);
    game.frame.setTitle("Title");
    game.frame.add(game);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);

    game.start();
}
}

Then I got this error, even when I countlessly modified the source code I had.

Exception in thread "Display" java.lang.NullPointerException
at java.awt.Component$BltBufferStrategy.showSubRegion(Component.java:4307)
at java.awt.Component$BltBufferStrategy.show(Component.java:4255)
at com.thecherno.Rain.Game.render(Game.java:58)
at com.thecherno.Rain.Game.run(Game.java:39)
at java.lang.Thread.run(Thread.java:695)

Im starting to seem if it because of an outdated JDK. Current Version I have is JDK 6.

Hovercraft Full Of Eels

You state:

What Im trying to do is change color as seen in the render method. The background to be black.

  • Use Swing components such as a JComponent or JPanel.
  • Simply call setBackground(Color.BLACK) on the component will do.
  • You appear to be creating a game loop of some type. Consider using a Swing Timer for this.

e.g.,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Game2 extends JPanel {
   private static final int PREF_W = 300;
   private static final int PREF_H = PREF_W / 16 * 9;
   private static final int SCALE = 3;
   private static final Color BACKGROUND = Color.BLACK;
   private static final int TIMER_DELAY = 20;
   private Timer swingTimer;

   public Game2() {
      setBackground(BACKGROUND);
      swingTimer = new Timer(TIMER_DELAY, new TimerListener());
      swingTimer.start();
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);   
      // TODO: add any custom painting here
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W * SCALE, PREF_H * SCALE);
   }

   private class TimerListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         // TODO add code that gets called in game loop
      }
   }

   private static void createAndShowGui() {
      Game2 mainPanel = new Game2();

      JFrame frame = new JFrame("Game2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Note that this code is based on your stated requirements and what I'm guessing are other requirements based on your code. If there are further requirements not mentioned, please elaborate them for us.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why won't my JFrame program change background color?

From Dev

Why won't the UIImage change?

From Dev

iOS button title color won't change

From Dev

Jumbotron color and background won't change

From Dev

Background color won't change in panel

From Dev

Mouseover on div won't change color

From Dev

SKView/SKScene won't change background color

From Dev

Bootstrap button highlight color won't change

From Dev

Mouseover on div won't change color

From Dev

Jumbotron color and background won't change

From Dev

Datagridview won't change cell color

From Dev

Background color won't change javascript

From Dev

Why won't the background color apply?

From Dev

Why input number won't "move" the color?

From Dev

Title color of UIButton won't change on highlighted/selected but background color will

From Dev

Accidentally set screen resolution too high, it won't change back

From Dev

willDisplayCell won't change background color consistent with logic

From Dev

Body background-color won't change while scrolling

From Dev

Android Material: Status bar color won't change

From Dev

Body background-color won't change while scrolling

From Dev

setOpaque is true, but JPanel won't change the background color

From Dev

Why won't my ScrollViewer Scrollbars appear on the screen?

From Dev

Why won't windows open an application on the same screen where it was requested?

From Dev

Why won't environment variables added to .profile show up in screen

From Dev

Why won't my ScrollViewer Scrollbars appear on the screen?

From Dev

Why won't my footer readjust to smaller screen sizes?

From Dev

Why won't my main window center properly on the screen?

From Dev

Why kinect color and depth won't align correctly?

From Dev

Reason why this background color won't override the other?

Related Related

  1. 1

    Why won't my JFrame program change background color?

  2. 2

    Why won't the UIImage change?

  3. 3

    iOS button title color won't change

  4. 4

    Jumbotron color and background won't change

  5. 5

    Background color won't change in panel

  6. 6

    Mouseover on div won't change color

  7. 7

    SKView/SKScene won't change background color

  8. 8

    Bootstrap button highlight color won't change

  9. 9

    Mouseover on div won't change color

  10. 10

    Jumbotron color and background won't change

  11. 11

    Datagridview won't change cell color

  12. 12

    Background color won't change javascript

  13. 13

    Why won't the background color apply?

  14. 14

    Why input number won't "move" the color?

  15. 15

    Title color of UIButton won't change on highlighted/selected but background color will

  16. 16

    Accidentally set screen resolution too high, it won't change back

  17. 17

    willDisplayCell won't change background color consistent with logic

  18. 18

    Body background-color won't change while scrolling

  19. 19

    Android Material: Status bar color won't change

  20. 20

    Body background-color won't change while scrolling

  21. 21

    setOpaque is true, but JPanel won't change the background color

  22. 22

    Why won't my ScrollViewer Scrollbars appear on the screen?

  23. 23

    Why won't windows open an application on the same screen where it was requested?

  24. 24

    Why won't environment variables added to .profile show up in screen

  25. 25

    Why won't my ScrollViewer Scrollbars appear on the screen?

  26. 26

    Why won't my footer readjust to smaller screen sizes?

  27. 27

    Why won't my main window center properly on the screen?

  28. 28

    Why kinect color and depth won't align correctly?

  29. 29

    Reason why this background color won't override the other?

HotTag

Archive