基本的に私はビギナー プログラマーです。C# と C++ を少し知っていますが、現在は Android アプリを構築するための Java を学んでいます。私はブランドニオ制作のYouTubeからJavaを学んでいます。とても役に立ちます。http://www.youtube.com/user/BrandonioProductions?feature=watch
これは完全なコードです:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
public class KeyListenerApp
extends Applet
implements KeyListener{
private Rectangle rect; //The rectangle that moving
private ArrayList<Integer> keysDown;
public void init()
{
rect = new Rectangle(0,0,50,50);
this.addKeyListener(this);
}
public void paint(Graphics g)
{
setSize(500,500);
Graphics2D g2 = (Graphics2D)g;
g2.fill(rect);
}
@Override
public void keyPressed(KeyEvent e) {
if(keysDown.contains(new Integer(e.getKeyCode())))
{
keysDown.add(new Integer(e.getKeyCode()));
}
moveRect();
}
@Override
public void keyReleased(KeyEvent e) {
keysDown.remove(new Integer(e.getKeyCode()));
}
public void moveRect()
{
int x = rect.x;
int y = rect.y;
if (keysDown.contains(KeyEvent.VK_UP))
y -= 2;
if (keysDown.contains(KeyEvent.VK_DOWN))
y += 2;
if (keysDown.contains(KeyEvent.VK_LEFT))
x -= 2;
if (keysDown.contains(KeyEvent.VK_RIGHT))
x += 2;
rect.setLocation(x, y);
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
}
これは完全なエラーです:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at KeyListenerApp.keyPressed(KeyListenerApp.java:34)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at KeyListenerApp.keyReleased(KeyListenerApp.java:44)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
最初にここにコードを投稿しなかったことをお詫びします。質問とは別のものにすれば、より快適になると思います。
ありがとうございました
作業コードは次のとおりです。
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
public class KeyListenerApp
extends Applet
implements KeyListener{
private Rectangle rect; //The rectangle that moving
private ArrayList<Integer> keysDown;
public void init()
{
rect = new Rectangle(0,0,50,50);
this.addKeyListener(this);
keysDown = new ArrayList<Integer>();
}
public void paint(Graphics g)
{
setSize(500,500);
Graphics2D g2 = (Graphics2D)g;
g2.fill(rect);
}
@Override
public void keyPressed(KeyEvent e) {
if(keysDown.contains(new Integer(e.getKeyCode())) == false)
{
keysDown.add(new Integer(e.getKeyCode()));
}
moveRect();
}
@Override
public void keyReleased(KeyEvent e) {
keysDown.remove(new Integer(e.getKeyCode()));
}
public void moveRect()
{
int x = rect.x;
int y = rect.y;
if (keysDown.contains(KeyEvent.VK_UP))
y -= 2;
if (keysDown.contains(KeyEvent.VK_DOWN))
y += 2;
if (keysDown.contains(KeyEvent.VK_LEFT))
x -= 2;
if (keysDown.contains(KeyEvent.VK_RIGHT))
x += 2;
rect.setLocation(x, y);
repaint();
}
@Override
public void keyTyped(KeyEvent e) {
}
}
エラーの原因:
if(keysDown.contains(new Integer(e.getKeyCode())) == false)
{
keysDown.add(new Integer(e.getKeyCode()));
}
このコードは、修正する前は次のようでした。
if(keysDown.contains(new Integer(e.getKeyCode())))
{
keysDown.add(new Integer(e.getKeyCode()));
}
エラーが発生したのは、コードで次のように言ったためです。何かが存在する場合は彼を追加し、彼が存在しない場合にのみ追加することになっていました。