[英]my program cant read the keys because of "Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet."
I am programming a snake game on Netbeans Java 8 and when I programmed the keys part, it didn't work because of Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet
.我正在 Netbeans Java 8 上编程一个蛇游戏,当我对键部分进行编程时,它没有工作,因为
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet
的Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet
。
Can anyone tell me whats the solution or any other method I can use to control my snake specially in line 135 .谁能告诉我解决方案是什么,或者我可以用任何其他方法来控制我的蛇,特别是在第 135 行。
**package mainclass;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
/**
*
*/
public class gamepanel extends JPanel implements Runnable,KeyListener{
private static final long serialversionid = 1L;
public static final int WIDTH =500,HEIGHT =500;
private Thread thread;
private boolean running;
private boolean right =true,left=false,up=false,down=false;
private bodyparts b;
private ArrayList<bodyparts> snake;
private int xcoor = 10,ycoor = 10, size= 5;
private int ticks =0;
public gamepanel(){
setFocusable(true);
setPreferredSize(new Dimension(WIDTH,HEIGHT));
addKeyListener(this);
snake = new ArrayList<bodyparts>();
start();
}
public void start(){
running = true;
thread = new Thread(this);
thread.start();
}
public void stop(){
try {
running = false;
thread.join();
} catch (InterruptedException ex) {
Logger.getLogger(gamepanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void tick(){
if (snake.size() == 0) {
b = new bodyparts(xcoor,ycoor,10);
snake.add(b);
}
ticks++;
if(ticks>250000){
if(right)xcoor++;
if(left)xcoor--;
if(up)ycoor--;
if(down)ycoor++;
ticks = 0;
b = new bodyparts(xcoor,ycoor,10);
snake. add (b);
if(snake.size()>size){
snake.remove(0);
if(snake.size()>size){
snake.remove(0);
}
}
}
}
public void paint(Graphics g){
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 0; i < WIDTH/10; i++) {
g.drawLine(i*10, 0, i*10, HEIGHT);
}
for (int i = 0; i < WIDTH/10; i++) {
g.drawLine(0,i*10, HEIGHT, i*10);
}
for (int i = 0; i < snake.size(); i++) {
snake.get(i).draw (g);
}
}
public void run(){
while (running){
repaint();
tick();
}
}
// @Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key==KeyEvent.VK_RIGHT&&!left){
right = true;
left=false;
up= false;
down = false;
}
if(key==KeyEvent.VK_LEFT&&!right){
right = false;
left=true;
up= false;
down = false;
}
if(key==KeyEvent.VK_UP&&!down){
right = false;
left=false;
up= true;
down = false;
}
if(key==KeyEvent.VK_DOWN&&!up){
right = false;
left=false;
up= false;
down = true;
}
}
// @Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
//@Override
public void keyTyped(KeyEvent e) {
}
}**
I expect the snake to be controlled, but its not and when I am trying to control it the is a big exception Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
我希望蛇被控制,但它不是,当我试图控制它时
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
一个大异常Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
@SizeableShrimp has identified the cause of your exception. @SizeableShrimp 已确定您的异常原因。
When you implement KeyListener
in an application class you need to provide implementations for the keyPressed
and keyReleased
method.当您在应用程序类中实现
KeyListener
,您需要为keyPressed
和keyReleased
方法提供实现。 Apparently, you did this using an IDE to generate stub implementations.显然,您是使用 IDE 来生成存根实现的。
The problem is that the stub implementations won't necessarily work .问题是存根实现不一定有效。 The IDE doesn't "know" what the methods mean ... or what they should actually do in your application.
IDE 不“知道”这些方法的含义……或者它们在您的应用程序中实际应该做什么。 In this case, the IDE has inserted an implementation that is designed to throw an exception if it is called ... reminding you that you need to look at the code and implement the method properly.
在这种情况下,IDE 插入了一个实现,该实现旨在在调用时抛出异常……提醒您需要查看代码并正确实现该方法。
In this case, it is really necessary to implement the keyRelease
method properly because it will be called whenever you release a key that you previously pressed.在这种情况下,真的有必要正确实现
keyRelease
方法,因为每当您释放之前按下的键时都会调用它。
But the implementation is straightforward.但是实现很简单。 Really straightforward.
真的很直接。 Your method doesn't to do anything at all, because key releases are not relevant to your game.
您的方法根本不执行任何操作,因为密钥发布与您的游戏无关。
OR BETTER STILL : Do this a different way as per @MadProgrammer's comments!或者更好:按照@MadProgrammer 的评论以不同的方式执行此操作!
Lessons:课程:
A programmers most important debugging tool is his/her brain, and the ability to put the clues together to diagnose problems.程序员最重要的调试工具是他/她的大脑,以及将线索放在一起以诊断问题的能力。 It takes practice.
这需要练习。 My advice would be to start practicing!
我的建议是开始练习!
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
This method is causing the problem.这种方法导致了问题。 Remove the line where it throws the exception and leave it empty.
删除抛出异常的行并将其留空。 The instant you let go of a key this method is run and throws the exception.
当你放开一个键时,这个方法就会运行并抛出异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.