我正在NetBeans Java 8上编写蛇游戏,当我编写按键部分时,由于Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet
,该游戏无法正常工作。
任何人都可以告诉我在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) {
}
}**
我希望蛇能够受到控制,但它不会受到控制,当我试图对其进行控制时,这是一个很大的例外Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
答案 0 :(得分:1)
@SizeableShrimp已经确定了导致异常的原因。
在应用程序类中实现KeyListener
时,需要提供keyPressed
和 keyReleased
方法的实现。显然,您是使用IDE生成存根实现的。
问题在于存根实现不一定会奏效。 IDE不会“知道”这些方法的含义……或它们应在您的应用程序中实际做什么。在这种情况下,IDE插入了一个设计的实现,如果调用它会抛出异常...提醒您需要查看代码并实现正确的方法。
在这种情况下,确实有必要正确实现keyRelease
方法,因为只要您释放先前按下的键,就会调用该方法。
但是实现很简单。真的很简单。您的方法根本不做任何事情,因为关键的发布与您的游戏无关。
还是更好:按照@MadProgrammer的评论,以不同的方式进行操作!
课程:
程序员最重要的调试工具是他/她的大脑,并且具有将线索综合在一起以诊断问题的能力。这需要练习。我的建议是开始练习!
答案 1 :(得分:0)
echo 'Here is your post data. Make sure you have your username.<br>';
echo '<pre>';
print_r($_POST);
echo '</pre>';
$query = "SELECT password FROM `table` WHERE username=?";
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $_POST['username']);
$stmt->execute();
echo("Error description: " . mysqli_error($conn)); //This will show you your errors after the execute();
$results = $stmt->get_result();
if($results->num_rows !== 0){
while($row = $results->fetch_assoc()){
$data[] = array(
'userexists' => 1,
'userpass' => $row['password']
);
}
}else{
echo "Your query failed to find any data.";
}
$stmt->close();
echo '<pre>';
echo 'Here is your data:<br>';
print_r($data);
echo '</pre>';
此方法引起了问题。删除引发异常的行,并将其留空。放开键后,此方法立即运行并引发异常。