Hallo zusammen,
ich habe ein Problem das ich mir nicht erklären kann. Ich schreibe ein Programm das ein Balkenmuster synchron zur Mausbewegung verschieben soll. Das klappt so weit auch super. Nur leider bekomme ich jedes mal wenn ich das Programm ausführe gelegentlich eine Exception(besonders Häufig im Debug - Modus). Hier ist die Exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Stripes.paint(Stripes.java:164)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(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.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)
Und hier der Code:
Wenn mir jemand sagen könnte wo mein Fehler liegt wäre ich sehr dankbar!
Viele Grüße
Kristian
ich habe ein Problem das ich mir nicht erklären kann. Ich schreibe ein Programm das ein Balkenmuster synchron zur Mausbewegung verschieben soll. Das klappt so weit auch super. Nur leider bekomme ich jedes mal wenn ich das Programm ausführe gelegentlich eine Exception(besonders Häufig im Debug - Modus). Hier ist die Exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Stripes.paint(Stripes.java:164)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(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.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)
Und hier der Code:
Java:
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import java.awt.image.MemoryImageSource;
import java.awt.Robot;
import javax.swing.Timer;
import java.awt.Image;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.JFrame;
public class Stripes extends JFrame implements ActionListener, MouseMotionListener, KeyListener {
/**
*
*/
private static final long serialVersionUID = -3314217310459535679L;
Timer t = new Timer(20,this);
int x = 0;
int y = 0;
int[][] stripes = new int[1][6];
int moveX = 1;
int moveY = 0;
int mouseX = 0;
int mouseY = 0;
int stripesWidth = 0;
int actualWidth;
int counter = 0;
long oldTime = 0;
long newTime = 0;
long timeBtwMeasures = 0;
int pixelsMoved = 0;
Queue<Long> times=new LinkedList<Long>();
Queue<Integer> distances=new LinkedList<Integer>();
boolean mouseLocked = false;
boolean measurementActive = false;
boolean keepRunning = true;
Image img = createImage(new MemoryImageSource(1, 1, new int[2], 0, 0));
Toolkit kit = Toolkit.getDefaultToolkit();
public Robot robot;
public void setStripes(int width, int number){
stripes = new int[number +1][6];
this.stripesWidth = width;
}
private void stripesLoop(){
while(keepRunning){
recalcPositions();
repaint();
}
}
private void calcStripes(int width){
int screenWidth = getWidth();
int number = stripes.length -1;
int spaceWidth = (screenWidth - (width*number)) / number;
for (int i = 0; i < stripes.length; i+=1){
stripes[i][0] = (i*width)+(i*spaceWidth);
stripes[i][1] = 0;
}
actualWidth = getWidth();
}
public static void main(String[] args){
Stripes s = new Stripes();
s.calcStripes(20);
//s.stripesLoop();
}
public Stripes() {
addKeyListener(this);
addMouseMotionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setUndecorated(true);
this.setSize(800,600);
setStripes(20,5);
this.setVisible(true);
this.createBufferStrategy(2);
try {
robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void recalcPositions() {
for (int i = 0; i < stripes.length; i+= 1){
/*Wegen der Drehung der Achsen nicht:
stripes[i][0] += moveX;
stripes[i][1] += moveY;
Sondern:*/
stripes[i][0] += moveY;
stripes[i][1] = 0;
if (stripes[i][0] < 0 && i != stripes.length -1){
stripes[stripes.length -1][0] = getWidth() + stripes[i][0];
stripes[stripes.length -1][1] = 0;
stripes[stripes.length -1][2] = 1;
}
if (stripes[i][0] > getWidth() - stripesWidth && i != stripes.length -1){
stripes[stripes.length -1][0] = (stripes[i][0] - getWidth());
stripes[stripes.length -1][1] = 0;
stripes[stripes.length -1][2] = 1;
}
if ((stripes[i][0] > 0 && stripes[i][0] < getWidth() -stripesWidth) && i == stripes.length - 1/*stripes[i][2] == 1*/){
stripes[i][2] = 0;
}
if (stripes[i][0] < 0 - stripesWidth ){
stripes[i][0] = stripes[stripes.length -1][0];
stripes[i][2] = 1;
}
if (stripes[i][0] > getWidth()){
stripes[i][0] = stripes[stripes.length -1][0];
stripes[i][2] = 1;
}
}
moveX = 0;
moveY = 0;
}
public void paint(Graphics g) {
BufferStrategy bf = this.getBufferStrategy();
g = null;
if (actualWidth != getWidth()){
calcStripes(stripesWidth);
}
try {
g = bf.getDrawGraphics();
// It is assumed that mySprite is created somewhere else.
// This is just an example for passing off the Graphics object.
g.setColor(Color.blue);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.green);
for (int i = 0; i < stripes.length; i+=1){
if(i<stripes.length-1 || (i == stripes.length -1 && stripes[i][2] == 1 )){
g.fillRect(stripes[i][0], stripes[i][1], stripesWidth, getHeight());
}
}
} finally {
// It is best to dispose() a Graphics object when done with it.
g.dispose();
}
// Shows the contents of the backbuffer on the screen.
bf.show();
//Tell the System to do the Drawing now, otherwise it can take a few extra ms until
//Drawing is done which looks very jerky
Toolkit.getDefaultToolkit().sync();
}
public void actionPerformed(ActionEvent e) {
}
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent e) {
int newX = e.getX();
int newY = e.getY();
newTime = System.nanoTime();
moveX = newX - mouseX;
moveY = newY - mouseY;
mouseX = newX;
mouseY = newY;
if (moveY != 0){
takeTime();
}
if(mouseLocked){
robot.mouseMove(400,240);
mouseX=400;
mouseY=240;
}
recalcPositions();
repaint();
}
private void takeTime() {
counter +=1;
if (oldTime == 0){
oldTime = System.nanoTime();
times.add((long) 0);
distances.add(0);
}else{
times.add(System.nanoTime() -oldTime);
distances.add(moveY);
oldTime = System.nanoTime();
}
}
public void keyPressed(KeyEvent event) {
int code = event.getKeyCode();
if (code == KeyEvent.VK_A){
mouseLocked = !mouseLocked;
if (mouseLocked){
Cursor cursor = kit.createCustomCursor(img, new java.awt.Point(0,0),"Transparent");
setCursor(cursor);
}else{
setCursor(Cursor.getDefaultCursor());
}
robot.mouseMove(400,240);
mouseX=400;
mouseY=240;
}
if (code == KeyEvent.VK_M){
if (measurementActive){
measurementActive = !measurementActive;
try {
saveData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
measurementActive = !measurementActive;
}
}
}
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent event) {
}
private boolean saveData() throws IOException{
FileWriter fw = new FileWriter("ausgabe42.txt");
BufferedWriter bw = new BufferedWriter(fw);
int size = times.size();
bw.write(Integer.toString(size)+";" +counter+";");
bw.newLine();
for(int i = 1; i<=size;i+=1){
bw.write(times.remove()+";"+distances.remove()+";");
bw.newLine();
}
bw.close();
return true;
}
}
Wenn mir jemand sagen könnte wo mein Fehler liegt wäre ich sehr dankbar!
Viele Grüße
Kristian