Gå til innhold

Anbefalte innlegg

Jeg er nybegynner, så fei meg av banen og be meg lese mer hvis problemet er åpenbart for de som kan dette...

 

Får en runtime exception av koden. (Alle klasser samlet.)

 

 

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

class SwingAnimation extends JFrame implements ActionListener
{
Container c;
DrawingSurface d;
JLabel lbl;
Timer animation;

public SwingAnimation()
{
 super("A Swing Animation");
 d = new DrawingSurface();
 c = getContentPane();
 c.add(d);
 
 animation = new Timer(1, this);
 animation.start();
 
 Global.arena = new Arena(800, 600);
 
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setSize(Global.arena.width + 8, Global.arena.height + 34);
 
 Global.obj = new ArrayList<Sphere>();
 Global.obj.add(new Sphere("Blue Ball", new Position(0, 0), new Vector(Math.toRadians(70), 3), 5, 7, new Color(0, 0, 255)));
 Global.obj.add(new Sphere("Red Ball", new Position(-130, -90), new Vector(Math.toRadians(76), 2), 7, 9, new Color(255, 0, 0)));
 Global.obj.add(new Sphere("Green Ball", new Position(120, 50), new Vector(Math.toRadians(-45), 2), 12, 15, new Color(0, 255, 0)));
}

public void actionPerformed(ActionEvent ev)
{
 for (int i = 0; i < Global.obj.size(); i++)
 {
 	Global.obj.get(i).arenaCollision(Global.arena);
 	Global.obj.get(i).move();
 }
 
 repaint();
}

class DrawingSurface extends JPanel
{
 Dimension preferredSize = new Dimension(Global.arena.width, Global.arena.height);
 
 public DrawingSurface()
 {
 	super();
 }
 
 public Dimension getPreferredSize()
 {
 	return preferredSize;
 }
 
 public void paintComponent(Graphics g)
 {
 	super.paintComponent(g);
 	
 	for (int i = 0; i < Global.obj.size(); i++)
 	{
   g.setColor(Global.obj.get(i).color);
   double xPos = Global.arena.width / 2 + (Global.obj.get(i).pos.x - Global.obj.get(i).radius) / Global.METERS_PER_PIXEL;
   double yPos = Global.arena.height / 2 + (Global.obj.get(i).pos.y - Global.obj.get(i).radius) / Global.METERS_PER_PIXEL;
   double diameter = (Global.obj.get(i).radius * 2) / Global.METERS_PER_PIXEL;
   g.fillOval((int)xPos, (int)yPos, (int)diameter, (int)diameter);
 	}
 }
}

public static void main(String[] args)
{
 SwingAnimation app = new SwingAnimation();
 app.setVisible(true);
}
}

class Global
{
public static double METERS_PER_PIXEL = 1;
public static double GAMMA = 6.67 * Math.pow(10, -11);
public static ArrayList<Sphere> obj;
public static Arena arena;
}

class Arena
{
int width;
int height;

public Arena(int width, int height)
{
 this.width = width;
 this.height = height;
}
}

class Position
{
double x;
double y;

public Position(double x, double y)
{
 this.x = x;
 this.y = y;
}
}

class Vector
{
double angle;
double mag;

public Vector(double angle, double mag)
{
 this.angle = angle;
 this.mag = mag;
}
}

class Sphere
{
String name;
Position pos;
Vector velocity;
double mass;
double radius;
Color color;

public Sphere(String name, Position pos, Vector velocity, double mass, double radius, Color color)
{
 this.name = name;
 this.pos = pos;
 this.velocity = velocity;
 this.mass = mass;
 this.radius = radius;
 this.color = color;
}

public double getDistance(Sphere other)
{
 double distanceX = other.pos.x - this.pos.x;
 double distanceY = this.pos.y - other.pos.y;
 return Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
}

public double getAngle(Sphere other)
{
 double distanceX = other.pos.x - this.pos.x;
 double distanceY = this.pos.y - other.pos.y;
 return Math.atan2(distanceX, distanceY);
}

public void arenaCollision(Arena arena)
{
 if (this.pos.x - this.radius < (-arena.width / 2) * Global.METERS_PER_PIXEL)
 {
 	this.pos.x = (-arena.width / 2) * Global.METERS_PER_PIXEL + this.radius;
 	this.velocity.angle = 2 * Math.PI - this.velocity.angle;
 }
 else if (this.pos.x + this.radius > (arena.width / 2) * Global.METERS_PER_PIXEL)
 {
 	this.pos.x = (arena.width / 2) * Global.METERS_PER_PIXEL - this.radius;
 	this.velocity.angle = -this.velocity.angle;
 }
 if (this.pos.y - this.radius < (-arena.height / 2) * Global.METERS_PER_PIXEL)
 {
 	this.pos.y = (-arena.height / 2) * Global.METERS_PER_PIXEL + this.radius;
 	this.velocity.angle = -Math.PI - this.velocity.angle;
 }
 else if (this.pos.y + this.radius > (arena.height / 2) * Global.METERS_PER_PIXEL)
 {
 	this.pos.y = (arena.height / 2) * Global.METERS_PER_PIXEL - this.radius;
 	this.velocity.angle = Math.PI - this.velocity.angle;
 }
}

public void objectCollision(Sphere subobj)
{
 double distance = this.getDistance(subobj);
 if (distance - this.radius - subobj.radius < 0)
 {
 	// get angle to b from a
 	double objAngle = this.getAngle(subobj);
 	// get angle to a from b
 	double subobjAngle = subobj.getAngle(this);
 	
 	// decompose speeds on collision plane (collision plane: line from center this to center subobj where x is along plane and y is normal to plane)
 	double objPlaneXSpeed = this.velocity.mag * Math.cos(this.velocity.angle - objAngle);
 	double objPlaneYSpeed = this.velocity.mag * Math.sin(this.velocity.angle - objAngle);
 	double subobjPlaneXSpeed = subobj.velocity.mag * Math.cos(subobj.velocity.angle - objAngle);
 	double subobjPlaneYSpeed = subobj.velocity.mag * Math.sin(subobj.velocity.angle - objAngle);
 	
 	// calculate new speeds in x-direction
 	double objPlaneNewXSpeed = (objPlaneXSpeed * (this.mass - subobj.mass) + 2 * subobj.mass * subobjPlaneXSpeed) / (this.mass + subobj.mass);
 	double objPlaneNewYSpeed = objPlaneYSpeed; // leave unchanged in y-direction
 	double subobjPlaneNewXSpeed = (subobjPlaneXSpeed * (subobj.mass - this.mass) + 2 * this.mass * objPlaneXSpeed) / (this.mass + subobj.mass);
 	double subobjPlaneNewYSpeed = subobjPlaneYSpeed; // leave unchanged in y-direction
 	
 	// get new polar speed
 	this.velocity.mag = Math.sqrt(Math.pow(objPlaneNewXSpeed, 2) + Math.pow(objPlaneNewYSpeed, 2));
 	subobj.velocity.mag = Math.sqrt(Math.pow(subobjPlaneNewXSpeed, 2) + Math.pow(subobjPlaneNewYSpeed, 2));
 	
 	// get new polar angle (first part is angle from plane, then second part adds the angle arena-plane)
 	this.velocity.angle = Math.atan2(objPlaneNewYSpeed, objPlaneNewXSpeed) + objAngle;
 	subobj.velocity.angle = Math.atan2(subobjPlaneNewYSpeed, subobjPlaneNewXSpeed) + objAngle;
 	
 	// move object out of collision zone
 	this.pos.x = subobj.pos.x + ((this.radius + subobj.radius) * Math.sin(subobjAngle));
 	this.pos.y = subobj.pos.y + ((this.radius + subobj.radius) * Math.cos(subobjAngle) * -1);
 }
}

public void move()
{
 this.pos.x += this.velocity.mag * Math.sin(this.velocity.angle);
 this.pos.y += this.velocity.mag * Math.cos(this.velocity.angle) * -1;
}
}

 

bug.jpg

 

Takk for noen hjelp!

 

Spoiler-tag satt inn av moderator JohndoeMAKT

Lenke til kommentar
Videoannonse
Annonse

Hei,

 

Hvis du trenger flere hint:

 

Ta en grundig titt på rekkefølgen:

1. Lage DrawingSurface

...

x. Lage Global.arena

 

Og så på hva som må gjøres når du lager en instans av DrawingSurface:

1. Sette preferredSize ved hjelp av Global.arena.?

2. Kalle constructor

 

Du prøver altså å benytte Global.arena før denne har fått en verdi.

 

mvh

CPL

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...