Runtime Error
Jeg kan ikke finde fejlen kan du?Fejl
java.lang.ClassCastException: java.awt.Point
at JFrame.paint(JFrame.java:261)
at sun.awt.RepaintArea.paint(RepaintArea.java:298)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:193)
at java.awt.Component.dispatchEventImpl(Component.java:2665)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Window.dispatchEventImpl(Window.java:912)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:319)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:103)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:84)
Koden:
/*
* JFrame.java
*
* Created on 18. maj 2001, 15:15
*/
/**
*
* @author lord x
* @version
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JFrame extends javax.swing.JFrame {
/** Creates new form JFrame */
public JFrame() {
initComponents ();
setSize(400,400);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the FormEditor.
*/
private void initComponents() {
canvas1 = new java.awt.Canvas();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
getContentPane().setLayout(null);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
}
);
canvas1.setName(\"canvas1\");
//getContentPane().add(e);
//e.setLocation(90, 40);
//canvas1.setSize(canvas1.getPreferredSize());
//canvas1.setBackground(Color.white);
//canvas1.setSize(200,200);
jButton1.setText(\"w\");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
}
);
getContentPane().add(jButton1);
jButton1.setLocation(10, 30);
jButton1.setSize(jButton1.getPreferredSize());
jButton2.setText(\"e\");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
}
);
getContentPane().add(w);
getContentPane().add(jButton2);
jButton2.setLocation(10, 70);
jButton2.setSize(jButton2.getPreferredSize());
jButton3.setText(\"n\");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
}
);
getContentPane().add(jButton3);
jButton3.setLocation(10, 110);
jButton3.setSize(jButton3.getPreferredSize());
jButton4.setText(\"s\");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
}
);
getContentPane().add(jButton4);
jButton4.setLocation(10, 140);
jButton4.setSize(jButton4.getPreferredSize());
jButton5.setText(\"grow\");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
}
);
getContentPane().add(jButton5);
jButton5.setLocation(180, 0);
jButton5.setSize(jButton5.getPreferredSize());
map = new Vector();
makeMap();
}
public void makeMap() {
int i,xPos,yPos;
for(i = 0,xPos = 40,yPos = 40;i < 40;i++,xPos += 5,yPos += 5) {
Point p = new Point(xPos,yPos);
Wall wall = new Wall(p,0);
map.add(p);
}
paint = true;
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:Grow
w.grow();
repaint();
jButton5.repaint();
updateWormCordinates();
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:s
w.moveSouth();
repaint();
jButton4.repaint();
updateWormCordinates();
System.out.println( w.toString());
System.out.println(\"\");
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:n
w.moveNorth();
repaint();
jButton3.repaint();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:e
w.moveEast();
repaint();
jButton2.repaint();
updateWormCordinates();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:west
w.moveWest();
repaint();
jButton1.repaint();
updateWormCordinates();
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit (0);
}
/**
* @param args the command line arguments
*/
public static void main (String args[]) {
new JFrame ().show ();
}
public void paint(Graphics g) {
//g.setColor(Color.white);
//g.fillRect(0,0,200,200);
g.setColor(Color.black);
int x[] = w.XValues();
int y[] = w.YValues();
int i;
for(i = 0;i < x.length;i++) {
g.fillRect(x[i],y[i],5,5);
}
if(paint) {
for(i = 0;i < map.size() ;i++) {
Wall w = (Wall)map.elementAt(i);
Point p = w.getPosition();
g.drawRect(p.x,p.y,5,5);
}
}
}
public void updateWormCordinates() {
int[] d = w.XValues();
int[] f = w.YValues();
int x = d[0];
int y = f[0];
Point p = new Point(x,y);
/*
int i;
for(i = 0;i < map.size() - 1;i++) {
Wall w2 = (Wall)map.elementAt(i);
Point p1 = w2.getPosition();
if (x == p1.x && y == p1.y) {
System.out.println(\"bang\");
}
}
*/
w.setLocation(x, y);
}
// Variables declaration - do not modify
private java.awt.Canvas canvas1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private Worm w = new Worm();
Enemy e = new Enemy();
Rectangle b2 = new Rectangle (40,49,0,0);
Rectangle b = new Rectangle (5,5,0,0);
Vector map;
Wall wall;
Point s = new Point(30,30);
boolean paint = false;
// End of variables declaration
}
