paintComponent中的JAVA setColor保持空白

时间:2015-11-05 15:29:29

标签: java

一旦有人遇到空间,我就试图改变我的小程序的背景(32)。它只是不起作用,我一直在尝试不同的东西,我可以在互联网上找到的所有内容,如放g.setColor(Color.BLUE);public void paintComponent(Graphics g)区块的开头。

这里我得到了以下代码。 我的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) {

   }    
}

我不知道为什么代码不在一个块中。好像我做错了什么?

2 个答案:

答案 0 :(得分:1)

  1. 您应该使用InputMap / ActionMap而不是KeyListener
  2. 您需要确保将JComponent设置为isFocusable(true),并在首次显示时执行component.requestFocus()以确保您的操作会触发。

答案 1 :(得分:0)

请做更正:
1.使用JFrame代替Frame
2.像这样执行paintComponent

    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。确保添加Screen JFrame 4.确保在setVisible(true)

上发出JFrame