luke
luke

Reputation: 13

JAVA setColor in paintComponent stays blank

I'm trying to change the background of my little programm as soon as someone hits space (32). It just won't work and I have been trying different things and everything I could find on the internet like putting the g.setColor(Color.BLUE); at the beginning of the public void paintComponent(Graphics g)block.

Here I got the following code. My Screen.java

 import java.awt.Color;
 import java.awt.Graphics;
 import javax.swing.JPanel;

 public class Screen extends JPanel implements Runnable{

Thread thread = new Thread(this);
Frame frame;

private int fps = 0; 
public int scene = 0;
public boolean running = false;

public Screen(Frame frame){
    this.frame = frame;
    this.frame.addKeyListener(new KeyHandler(this));
    thread.start();
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    super.paint(g);
    g.clearRect(0, 0, this.frame.getWidth(),this.frame.getHeight());
    g.drawString(fps + "", 10, 10);

    if (scene == 0) {
        g.setColor(Color.BLUE);
    } else if (scene == 1) {
        g.setColor(Color.GREEN);
    } else {
        g.setColor(Color.white);
    }
    g.fillRect(0, 0, getWidth(), getHeight());
}


public void run() {
   System.out.println("[Success] Frame Created!");

   long lastFrame = System.currentTimeMillis();
   int frames = 0;

   running = true;
   scene = 0;

   while(running){
       repaint();
       frames++;

       if(System.currentTimeMillis() - 1000 >= lastFrame){
           fps = frames;
           frames = 0;
           lastFrame = System.currentTimeMillis();
       }

       try {
           Thread.sleep(2);
       } catch (InterruptedException ex) {
           ex.printStackTrace();
       }
   }
   System.exit(0);
   }

    public class KeyTyped{
    public void keySPACE() {
        scene = 1;
    }
    public void keyESC(){
        running = false;
    }


   }
  }

   KeyHandler.java

 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;

 public class KeyHandler implements KeyListener {

public Screen screen;
public Screen.KeyTyped keyTyped;

public KeyHandler(Screen screen){
    this.screen = screen;
    this.keyTyped = this.screen.new KeyTyped();

  }

 public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();

    System.out.println(keyCode);

    if(keyCode == 27){
        this.keyTyped.keyESC();
    }        
    if(keyCode == 32){
        this.keyTyped.keySPACE();
    }
}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

   }    
}

I don't know why the code isn't in one block. Seems like I'm doing something wrong with it?

Upvotes: 1

Views: 100

Answers (2)

krzydyn
krzydyn

Reputation: 1032

please do the corrections:
1. use JFrame instead of Frame
2. implemement paintComponent like this:

    g.clearRect(0, 0, this.frame.getWidth(),this.frame.getHeight());
    if (scene == 0) {
        g.setColor(Color.BLUE);
    } else if (scene == 1) {
        g.setColor(Color.GREEN);
    } else {
        g.setColor(Color.white);
    }
    g.fillRect(0, 0, getWidth(), getHeight());

    g.setColor(Color.WHITE);
    g.drawString(fps + "", 10, 10);

3. make sure Screen is added JFrame 4. make sure to issue setVisible(true) on JFrame

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35011

  1. You should use InputMap / ActionMap instead of a KeyListener
  2. You need to make sure the JComponent is set to isFocusable(true), and you do component.requestFocus() when first shown to make sure that your action will trigger.

Upvotes: 1

Related Questions