简体   繁体   English

按下键时绘制图像

[英]Painting an image when key is pressed

I'm making a simple Java applet that displays a traffic light. 我正在制作一个显示交通灯的简单Java小程序。 If there are no keys being pressed the background is white. 如果没有按下按键,则背景为白色。 If you hit the "1", "2", or "3" keys on the numpad then the traffic light should change colors to respectfully: red, green, and yellow. 如果您点击小键盘上的“1”,“2”或“3”键,那么红绿灯应该更改颜色:红色,绿色和黄色。 It is not working because when I press any keys, nothing happens. 它没有用,因为当我按任意键时,没有任何反应。 all isX booleans are initialized to false except for isReleased. 除了isReleased之外,所有isX布尔值都被初始化为false。

@Override
public void keyPressed(KeyEvent e) {
isReleased = false;
switch(e.getKeyCode()){
    case KeyEvent.VK_NUMPAD1:
        isRed=true;
        break;
    case KeyEvent.VK_NUMPAD2:
        isGreen=true;
        break;
    }

}

@Override
public void keyReleased(KeyEvent e) {
isReleased = true;
}

@Override
public void keyTyped(KeyEvent e) {
}

 @Override
 public void paint(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.drawImage(trafficLight, 0, 0, null);

    if(isReleased==true){
        g.drawImage(blank, 0, 0, this);
    }else{
        if(isRed==true){
        g.drawImage(red, 0, 0, this);
    }
    if(isGreen==true){
        g.drawImage(green, 0, 0, this);
        }
    }
}

NOTE 注意
(updated) My full code can be seen here: http://pastebin.com/8ZNQUWJy (更新)我的完整代码可以在这里看到: http//pastebin.com/8ZNQUWJy

Let's ignore that fact that you code is incomplete, so it's impossible to know what you are extending from or if you've actually added a KeyListener . 让我们忽略你编码不完整的事实,因此不可能知道你从哪个扩展或者你是否真的添加了一个KeyListener

KeyListener is notorious for being fickle about it's focus state. KeyListener因其对焦点状态变幻无常而臭名昭着。 Basically, this means, KeyListener will only respond to keystrokes if the component it is registered to is focusable AND has keyboard focus... 基本上,这意味着,如果注册的组件是可聚焦的并且具有键盘焦点,则KeyListener将仅响应击键...

A better solution would be to use the Key Bindings API , which provides better control over the focus level that will trigger key events. 更好的解决方案是使用Key Bindings API ,它可以更好地控制将触发关键事件的焦点级别。

You are also breaking the paint chain. 你也打破了油漆链。 Painting is a complex series of method calls, chaining together to produce the final result. 绘画是一系列复杂的方法调用,链接在一起产生最终结果。 By failing to call super.paint , you've broken this chain, introducing the possibility of paint artifacts. 由于没有调用super.paint ,你已经破坏了这个链,引入了绘制工件的可能性。

You should also avoid overriding paint generally, but especially of top level containers, like windows, as they are not double buffered. 您还应该避免一般覆盖paint ,尤其是顶级容器,如窗户,因为它们不是双缓冲的。 It is recommended that instead, you extend from something JPanel or JComponent and override it's paintComponent method instead. 相反,建议您从JPanelJComponent扩展并覆盖它的paintComponent方法。

Take a look at Performing Custom Painting for more details. 有关更多详细信息,请参阅执行自定义绘画

Updated based on seeing full source 基于查看完整源代码更新

  • Avoid AWT based components (like Applet ) the API is out of date and not many people use it any more, instead you should consider using a JApplet instead. 避免使用基于AWT的组件(如Applet ),API已过时,并且很多人不再使用它,相反,您应该考虑使用JApplet But personally, I would avoid applets until you have a better understanding of the overall API. 但就个人而言,在您对整体API有更好的理解之前,我会避免使用applet。
  • Don't call setSize within an applet, the size is determined by the html tag which describes the applet to the browser 不要在applet中调用setSize ,大小由html标记决定,该标记描述了浏览器的applet
  • Your run method is pointless as it doesn't actually do anything useful and may actually be causing your application to "hang" 你的run方法毫无意义,因为它实际上没有做任何有用的事情,实际上可能导致你的应用程序“挂起”
  • Your update method is only painting the trafficLight BufferedImage , but because you fail to call super.update , nothing else is painted. 您的update方法仅绘制trafficLight BufferedImage ,但由于您无法调用super.update ,因此不会绘制任何其他内容。 It would be better to grid rid of it and do your painting in the paint method, but make sure you are calling super.paint . 网格摆脱它并在paint方法中进行paint会更好,但请确保调用super.paint But as I already said, you'd be better of using something like JPanel and overriding it's paintComponent method. 但正如我已经说过的,你最好使用像JPanel这样的东西并覆盖它的paintComponent方法。

Take a look at Creating a GUI with Swing for more details 有关更多详细信息,请参阅使用Swing创建GUI

You may also find Why CS teachers should stop teaching Java applets of some worth... 您可能还会发现为什么CS教师应该停止教授一些有价值的Java小程序 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM