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
.
Can anyone tell me whats the solution or any other method I can use to control my snake specially in line 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.
@SizeableShrimp has identified the cause of your exception.
When you implement KeyListener
in an application class you need to provide implementations for the keyPressed
and keyReleased
method. Apparently, you did this using an IDE to generate stub implementations.
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. 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.
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.
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!
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.