How do you draw the Mandelbrot Set in Java using SWING/AWT? - java

I'm trying to draw the Mandelbrot Set, with points in the set as black, and everything else as white. In this initial version, I do not wish to be able to zoom in but rather just create a static image.
I created a ComplexNumber class, as shown below, to handle squaring and adding complex numbers together.
public class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary){
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber times(ComplexNumber number){
double a = this.real*number.real;
double b = this.imaginary*number.real;
double c = this.real*number.imaginary;
double d = this.imaginary*number.imaginary*-1;
double newReal = a+d;
double newImaginary = b+c;
ComplexNumber newComplexNumber = new ComplexNumber(newReal, newImaginary);
return newComplexNumber;
}
public ComplexNumber add(ComplexNumber number){
double newReal = this.real+number.real;
double newImaginary = this.imaginary+number.imaginary;
return new ComplexNumber(newReal, newImaginary);
}
public double abs(){
return Math.hypot(this.real, this.imaginary);
}
public double getReal() {
return real;
}
public double getImaginary() {
return imaginary;
}
}
And here is the code where I render the GUI and actually calculate the points in the Mandelbrot Set.
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class MandelBrotSet extends JComponent {
public static final int WIDTH = 800;
public static final int HEIGHT = 800;
public static final int ITERATIONS = 100;
public static final double startX = -2;
public static final double width = 4;
public static final double startY = 2;
public static final double height = 4;
public static final double dx = width/(WIDTH-1);
public static final double dy = height/(HEIGHT-1);
private BufferedImage buffer;
public MandelBrotSet() {
buffer = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
JFrame frame = new JFrame("Mandelbrot Set");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.getContentPane().add(this);
frame.pack();
frame.setVisible(true);
}
#Override
public void addNotify() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
#Override
public void paint(Graphics g) {
g.drawImage(buffer, 0, 0, null);
}
public void render(){
for (int x=0; x<WIDTH; x++){
for (int y=0; y<HEIGHT; y++){
int color = calculatePoint(x, y);
buffer.setRGB(x, y, color);
}
}
}
public int calculatePoint(int x, int y){
ComplexNumber number = convertToComplex(x, y);
ComplexNumber z = number;
int i;
for (i=0; i<ITERATIONS; i++){
z = z.times(z).add(number);
if (z.abs()>2.0){
break;
}
}
if (i==ITERATIONS) {
return 0x00000000;
}
else {
return 0xFFFFFFFF;
}
}
public static ComplexNumber convertToComplex(int x, int y){
double real = startX + x*dx;
double imaginary = 2 - y*dy;
return new ComplexNumber(real, imaginary);
}
public static void main(String[] args) {
MandelBrotSet mandy = new MandelBrotSet();
mandy.render();
}
}
After running this code, I'm getting the image below. There seems to be a small glimpse of the Mandelbrot set, but then it's obscured by a ton of black. What am I doing wrong?
Updated Solution Below. Thanks for the help.

The rendering of your image takes longer than displaying the MandelBrotSet-Object, and it is never updated afterwards.
You create an object of type MandelBrotSet, then immediately call it's render method. During the creation of that object, you put it in a JFrame which you immediately display. When the frame is displayed, the rendering is incomplete, which is why your ImageBuffer is not yet filled (it takes longer to build the mandelbrot-form).
To solve this, you could repaint the involved components and the frame. My understanding of awt's repaint functions is not good enough to tell you how exactly to do it right (maybe someone else can help there), but adding this to the end of your render method should help:
revalidate();
repaint();
Either revalidate or repaint can probably be omitted.
Would be cool, if you updated your question with the done image :)

Related

My Java Bouncing Ball Bounces up with a larger velocity

I'm making a simple Java program to bounce a ball up and down. The problem is that the ball bounces up higher than its starting point with each bounce. I expect the ball to bounce back up exactly to the height that it started from.
The ball physics can be found in the circle class in the doPhysics() method where I suspect the problem can be found
import java.awt.*;
import java.util.*;
public class Main{
public static Frame frame = new Frame();
public static Physics physics = new Physics();
public static ArrayList<Circle> circles = new ArrayList<Circle>(); //array for the points
public static void main(String args[]) {
Circle circle = new Circle(100, 300, 50, Color.BLACK);
circles.add(circle);
run();
}
public static void run() {
physics.timer.start();
}
}
import java.awt.*;
public class Circle {
private int x;
private int y;
private double xAccel= 0;
private double yAccel = 0;
private double xVel= 0;
private double yVel = 0;
private Color colour;
private int radius;
public Circle(int x, int y, int radius, Color colour) {
setX(x);
setY(y);
setRadius(radius);
setColour(colour);
}
public void draw(Graphics2D g2d) {
g2d.setColor(colour);
g2d.fillOval(x, y, radius*2, radius*2);
}
public void doPhysics() {
hitGround();
System.out.println(yVel);
yVel += Physics.getGravity();
y -= yVel;
}
public void hitGround() {
if(y + radius*2 > Frame.panel.h ) {
yVel = -yVel;
}
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setColour(Color colour) {
this.colour = colour;
}
public void setRadius(int radius) {
this.radius = radius;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Color getColour() {
return colour;
}
public int getRadius() {
return radius;
}
}
import java.awt.*;
import javax.swing.*;
class Frame extends JFrame {
public static Panel panel;
public Frame() {
panel = new Panel();
this.setTitle("Fun");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(panel);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
class Panel extends JPanel {
public int w = 500;
public int h = 500;
public Panel() {
this.setPreferredSize(new Dimension(w, h));
this.setBackground(Color.red);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
for(Circle circle : Main.circles) {
circle.draw(g2d);
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class Physics implements ActionListener {
private static double gravity = -.1;
public Timer timer;
public Physics() {
timer = new Timer(1, this);
}
public static double getGravity() {
return gravity;
}
#Override
public void actionPerformed(ActionEvent e) {
for(Circle circle : Main.circles) {
circle.doPhysics();
}
Main.frame.repaint();
}
}
The problem is mainly caused by using integer values for position (x and y). On each iteration the values are rounded and the errors get accumulated.
Solution: declare double x and double y and only use the rounded integer values for drawing.
Above should reduce the problem, but not completely solve it. The code is doing a rough integration over timeĀ¹ by using the velocity calculated after the time interval (see Numerical Integration). This can be improved by doing an average of the velocities before and after it was changed. Roughly:
double preVel = yVel;
yVel += Physics.getGravity();
y -= (preVel + yVel)/2;
which can be simplified (pure math) to:
yVel += Physics.getGravity();
y -= yVel - Physics.getGravity()/2;
This should work fine since the acceleration is constant. Not the case if the acceleration is also changing. And it is also susceptible to precision errors being accumulated over time.
1 - see Numerical integration and Temporal discretization

Overload Constructor with Class in Parameter

Was giving the first code below. What is the proper way to create the Turtle Class? -- Basically, I am trying to get this to show no error: Turtle t = new Turtle(STARTX, STARTY, w);
I think my problem might be here with the overload constructor: public Turtle (double STARTX, double STARTY, Class w )
import java.awt.*; //import color;
public class PA1{
//These are constant values that you can use
private static final int STARTX = 100;
private static final int STARTY = 100;
private static final int CHAR_WIDTH = 100;
private static final int CHAR_HEIGHT = 100;
private static final int CHAR_SPACING = 50;
public static void main(String[] args){
//set the width and height of the world
int width = 1000;
int height = 1000;
World w = new World(width, height);
//create a turtle at the starting x and starting y pos
Turtle t = new Turtle(STARTX, STARTY, w);
//Set the turtle pen width.
t.setPenWidth(15);
//This is just an example. Feel free to use it as a reference.
//draw a T
//Assume that the turtle always starts in the top left corner of the character.
t.turn(90);
t.forward(CHAR_WIDTH);
t.backward(CHAR_WIDTH/2);
t.turn(90);
t.forward(CHAR_HEIGHT);
//Move the turtle to the next location for the character
t.penUp();
t.moveTo(STARTX+CHAR_WIDTH+CHAR_SPACING*1, STARTY);
t.penDown();
//WRITE YOUR CODE HERE
}
}
I created 2 new class:
public class World {
//World w = new World(width, height);
private double defaultWidth;
private double defaultLength;
public World () {
defaultWidth = 0.0;
defaultLength = 0.0; }
public World (double width, double length){
defaultWidth = width;
defaultLength = length; }
public double getWidth () {
return defaultWidth; }
public double getLength () {
return defaultLength; }
public void setWidth (double width){
defaultWidth = width; }
public void setLength(double length){
defaultLength = length; }
}
and
public class Turtle {
// Turtle t = new Turtle(STARTX, STARTY, w);
private double defaultSTARTX;
private double defaultSTARTY;
//private double defaultW;
public Turtle () {
defaultSTARTX = 0.0;
defaultSTARTY = 0.0;
//defaultW = 0.0;
}
public Turtle (double STARTX, double STARTY, Class w ){
defaultSTARTX = STARTX;
defaultSTARTY = STARTY;
//defaultW = w;
}
public double getSTARTX () {
return defaultSTARTX; }
public double getSTARTY () {
return defaultSTARTY; }
public void setSTARTX (double STARTX){
defaultSTARTX = STARTX; }
public void setSTARTY(double STARTY){
defaultSTARTY = STARTY; }
}
If you really need to pass a World object to the Turtle object, you should define the Turtle constructor as this:
public Turtle (double STARTX, double STARTY, World w ){
defaultSTARTX = STARTX;
defaultSTARTY = STARTY;
defaultW = w;
}
Also, you must declare "w" not as double, as you have done at some point, but as a "World" class variable in Turtle:
private World defaultW;
Passing Class as a constructor parameter, you are trying to pass a generic class definition, not an Object instance of any class. The diference is subtle, but is there, and is your most likely issue.
I found many issues in the code you provided above. I do not know if it was the full code or not. I tried to fix all the problems in your code - related to your constructor and other java standards. Below is working for me -
public class MyClass {
private static final int STARTX = 100;
private static final int STARTY = 50;
public static void main(String args[]) {
int width = 1000;
int height = 1000;
World w = new World(width, height);
//create a turtle at the starting x and starting y pos
Turtle t = new Turtle(STARTX, STARTY, w);
System.out.println(t.getSTARTX() + " : " + t.getSTARTY() + " : " + t.getWorld().getLength() + " : " + t.getWorld().getWidth());
}
}
class World {
private double width;
private double length;
public World () {
this.width = 0.0;
this.length = 0.0;
}
public World (double width, double length) {
this.width = width;
this.length = length;
}
public double getWidth () { return this.width; }
public double getLength () { return this.length; }
public void setWidth (double width){ this.width = width; }
public void setLength(double length){ this.length = length; }
}
class Turtle {
private double STARTX;
private double STARTY;
private World world;
public Turtle () {
this.STARTX = 0.0;
this.STARTY = 0.0;
this.world = new World();
}
public Turtle (double STARTX, double STARTY, World w) {
this.STARTX = STARTX;
this.STARTY = STARTY;
this.world = w;
}
public double getSTARTX () { return this.STARTX; }
public double getSTARTY () { return this.STARTY; }
public World getWorld(){ return this.world; }
public void setSTARTX (double STARTX){ this.STARTX = STARTX; }
public void setSTARTY(double STARTY){ this.STARTY = STARTY; }
public void setWorld (World world){ this.world = world; }
}
Hope it resolves your query.
Happy coding. :)

Multiple paint() methods in Java applet?

So I am trying to generate random rectangles that the player must avoid. My collision method was working with a single, randomly generated rectangle. I want to draw 10 or so of these and then I will add a finish line and a timer.
Right now, I understand my problem, but I am not sure how to fix it. The ball/player's movement is executed by changing the x or y coordinates by 10 and then repainting the circle. I currently have the rectangles in the same paint method, so each time the player moves the rectangles are regenerated. I would like them to stay in the same place after the initial random generation. I don't really know how to do this though...
Also, if I can get the rectangles to stay in the same place, will my collision method still work with multiple rectangles? or would I need to revise that as well?
I am just going to post the whole program's code because I'm not sure which parts will need to be revised.
import java.awt.*;
import java.awt.Rectangle;
import java.awt.Shape;
import javafx.scene.shape.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.geom.Area;
import javax.swing.JOptionPane;
import java.applet.Applet;
public class Main extends Applet
implements ActionListener{
boolean end = false;
private Rectangle rectangle;
//creates buttons to move player
private Button run = new Button("Run");
private Button jump = new Button("Jump");
private Button fall = new Button("Fall");
//creates player and obstacles
private Circle player = new Circle(110,110,20);
private makeRect block = new makeRect(150, 120, 30, 10);
//initiates the buttons with actionListener
public void init(){
add(run);
add(jump);
add(fall);
run.addActionListener(this);
jump.addActionListener(this);
fall.addActionListener(this);
}
//draws the player and blocks on the screen
public void paint(Graphics g){
for(int numBlocks = 0; numBlocks<11; numBlocks++){
block.draw(g);}
player.draw(g);
}
//if methods to be control movement
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof Button){
if(e.getSource() == run)
player.horiz(10);
else if (e.getSource()== jump){
player.vert(-10);
}
else if (e.getSource()== fall){
player.down(10);
}
repaint();
collision();
}
}
public void collision(){
if(player.getBounds().intersects(block.getBounds())){
JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
System.exit(ABORT);
end = true;
}
}
class Circle{
private final Color theColor = Color.BLUE;
private int radius;
private int x,y;
public Circle(){
x = 110; y = 110;
radius = 20;
}
public Circle(int x0, int y0, int rad){
x = x0; y = y0; radius = rad;
}
public void draw(Graphics g){
g.fillOval(x - radius, y-radius, 2*radius, 2*radius);
g.setColor(theColor);
}
public void horiz(int val){
for(int c = 0; c<val+1; c++){
x++;
repaint();}
}
public void vert(int val){
y += val;
}
public void down(int val){
y += val;
}
public Rectangle getBounds(){
return new Rectangle(x-radius, y-radius, 2*radius, 2*radius);
}
}
class makeRect{
private int Xmax = 250;
private int Xmin = 140;
private int Wmax = 50;
private int Hmax = 25;
private int Wmin = 10;
private int Hmin = 5;
Random rand = new Random();
private int randx;
private int randh;
private int x, y, width, height;
public makeRect(){
x = 150; y = 120;
width = 30; height = 10;
}
public makeRect(int x0, int y0, int w0, int h0){
x = x0; y = y0; width = w0; height = h0;
}
public void draw(Graphics g) {
int randx = rand.nextInt((Xmax-Xmin)+1)+Xmin;
int randh = rand.nextInt((Hmax-Hmin)+1)+Hmin;
int randw = rand.nextInt((Wmax-Wmin)+1)+Wmin;
g.drawRect(randx, 110+randh, randh, randw);
}
public Rectangle getBounds(){
return new Rectangle(randx, 110+randh, 30, 10);
}
}
}
Thanks!
For that you will need to construct 10 rects (consider using array) first upon initialization, with random postition for each. I mean, postition randomization occurs when the rects are constructed, not when it's drawn.
What you have there is a same rectangle drawn 10 times at different places each time the paint() gets called.

n-body simulation - IndexOutOfBoundsException occurring randomly

Let me be brief. I am creating a n-body simulation for a school-project. I've encountered a problem: I get the error IndexOutOfBoundsException only sometimes when I run the simulation. There does not seem to be any correlation between when I get the error. I am assuming I am destroying the bodies (via the collision detection method) too quickly for it to register, and it tries to access an index that no longer exists. I thought
if(bodies.get(i)!=null&&bodies.get(n)!= null)
in the update method would fix it, but it did not. Could someone take a look at the code and tell me what might be causing the error? To recreate the error: Simply press 'o' in the simulation, or randomly spawn massive bodies with 'm' until it occurs. Please keep in mind that I am only in high school, and I have not got a lot of programming experience.
MAIN:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main extends JPanel implements Runnable, KeyListener{
public static int width = 1400;
public static int height = (width/16) * 9;
public String title = "Orbital Mechanics Simulator -";
public Dimension dim = new Dimension(width, height);
public boolean running;
Color bgColor = new Color(0x000000);
public static long timeScale = (long) Math.pow(10,7.5); //how many simulation-seconds one IRL-second represents.
public static long distanceScale = ((Physics.astUnit*10L) / width); //how many meters does one pixel represent
ArrayList<Body> bodies = new ArrayList<Body>();
long initFpsTime;
long secondFpsTime;
long nowTime;
long lastTime;
long timeElapsed;
float deltaTime;
int fps = 200;
public Main(){
this.setPreferredSize(dim);
this.setBackground(bgColor);
this.start();
}
public void start(){
running = true;
Thread program = new Thread(this, "update");
program.start();
}
public void run(){
lastTime = System.nanoTime();
while(running){
nowTime = System.nanoTime();
deltaTime = nowTime - lastTime;
update(deltaTime);
initFpsTime = System.currentTimeMillis();
if((initFpsTime - secondFpsTime) > Math.pow(10,3) / fps){
render();
secondFpsTime = System.currentTimeMillis();
}
}
}
public void update(float deltaTime){
for(int i=0; i<bodies.size();i++){
resetForces();
bodies.get(i).update((float)(deltaTime / Math.pow(10,9))*timeScale);
lastTime = System.nanoTime();
//sets the forces for all bodies
for(int n=0; n<bodies.size();n++){
if(bodies.get(i)!=bodies.get(n)){
if(bodies.get(i)!=null&&bodies.get(n)!= null)
bodies.get(i).setForce(Physics.getFx(bodies.get(i), bodies.get(n)), Physics.getFy(bodies.get(i), bodies.get(n)));
//collision detection
if(Physics.getDistanceBetween(bodies.get(i), bodies.get(n)) < (bodies.get(i).radius + bodies.get(n).radius)*distanceScale){
collision(bodies.get(i),bodies.get(n));
}
}
}
}
}
//DIFFERENT SYSTEMS
public void solarSystem(){
Body sun;
Body earth;
Body mars;
sun = new Body("Sun", Physics.massSun, 20, 0,0, new Color(0xffff00), (float)0, (float)0);
earth = new Body("earth", Physics.massEarth, 10, Physics.astUnit/distanceScale,0, new Color(0x0000ff), (float)0, (float)0);
mars = new Body("Mars", Physics.massEarth, 10, (long)(1.5*Physics.astUnit/distanceScale) ,0, new Color(0x00ff00), (float)0, (float)0);
earth.setVelocity(0,(float)Physics.getInitVy((long)Physics.getDistanceBetween(earth, sun), sun));
mars.setVelocity(0,(float)Physics.getInitVy((long)Physics.getDistanceBetween(mars, sun), sun));
bodies.add(sun);
bodies.add(earth);
bodies.add(mars);
}
public void twoBodies(){
bodies.add(new Body("Sun", Physics.massSun, 20, 0,0, new Color(0xffff00), (float)0, (float)0));
bodies.add(new Body("earth", Physics.massEarth, 10, Physics.astUnit/distanceScale,0, new Color(0x0000ff), (float)0, (float)0));
bodies.add(new Body("Mars", Physics.massMars, 10, (long)(1.5*Physics.astUnit/(distanceScale)),0 ,new Color(0x00ff00), (float)0, (float)0));
//earth.setVelocity(0,(float)Physics.getInitVy((long)Physics.getDistanceBetween(earth, sun), sun));
//mars.setVelocity(0,(float)Physics.getInitVy((long)Physics.getDistanceBetween(mars, sun), sun));
}
public void createRandomBody(){
bodies.add(new Body("randomBody",Physics.massSun,10, Physics.randomXPos(), Physics.randomYPos(), Physics.randomColor(),(float)0,(float)0));
}
public void createMassiveBody(){
bodies.add(new Body("Sun",Physics.massSun,10, Physics.randomXPos(), Physics.randomYPos(), Physics.randomColor(),(float)0,(float)0));
}
public void createSmallBody(){
bodies.add(new Body("Earth",Physics.massEarth,10, 0, 0, Physics.randomColor(), (float)0, (float)0));
}
public void createSystem(){
for(int i=0; i<20;i++){
for(int n=0; n<20;n++){
bodies.add(new Body("Random", Physics.massSun, 4, -width/2 + n*20 , height/2 - i*20 , Color.WHITE, (float)0, (float)0 ));
}
}
}
public void resetForces(){
if(bodies.get(0) != null);
for(int i=0;i<bodies.size();i++){
if(i<=0 && i < bodies.size()){
bodies.get(i).resetForce();
}
}
}
public void collision(Body a, Body b){
Body newBody = new Body("newBody", a.mass+b.mass, 20, (a.xPos+b.xPos)/2, (a.yPos+b.yPos)/2, Physics.randomColor(), (a.mass*a.vx + b.mass*b.vx)/(a.mass+b.mass), (a.mass*a.vy+b.mass*b.vy)/(a.mass+b.mass));
bodies.remove(a);
bodies.remove(b);
bodies.add(newBody);
}
public void render(){
repaint();
}
public void keyPressed(KeyEvent event) {
switch(event.getKeyChar()){
case 'r': createRandomBody();
break;
case 'f': twoBodies();
break;
case 'm':
createMassiveBody();
break;
case 'z':
createSmallBody();
break;
case 'o':
createSystem();
break;
case 's':
solarSystem();
break;
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //quick vs quality - preferring quality
for(int i=0;i<bodies.size();i++){
bodies.get(i).displayPlanet(g2d);
}
}
public static void gui(){
Main main = new Main();
JFrame frame = new JFrame();
frame.setResizable(false);
frame.setTitle(main.title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(main);
frame.addKeyListener(main);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){ //Event dispatching thread
public void run(){
gui();
}
});
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
BODY
import java.awt.Color;
import java.awt.Graphics;
public class Body {
public static int xOrigo = Main.width / 2;
public static int yOrigo = ((Main.width * 9) / 16) / 2;
public static int numOfBodies; // how many bodies the program contains.
public static float distanceScale = Main.distanceScale;
public String name;
public float mass, fx, fy, accX, accY, vx, vy, initVx, initVy, deltaX, deltaY;
public long xDisplay, yDisplay;
public long xPos, yPos;
public int radius;
public Color color;
public Body(String name, float mass, int radius, long xPos, long yPos, Color color, float initVx, float initVy){
this.name = name;
this.mass = mass;
this.radius = radius;
this.color = color;
numOfBodies++;
setX(xPos);
setY(yPos);
setVelocity(initVx, initVy);
}
public void update(float deltaTime){
this.deltaX += this.vx * deltaTime;
if(this.deltaX >= distanceScale){
this.incX();
deltaX = 0;
}else if(this.deltaX <= -distanceScale){
this.decX();
deltaX = 0;
}
this.deltaY += this.vy * deltaTime;
if(this.deltaY >= distanceScale){
this.incY();
deltaY = 0;
}else if(this.deltaY <= -distanceScale){
this.decY();
deltaY = 0;
}
this.vx += (this.accX * deltaTime);
this.vy += (this.accY * deltaTime);
}
public void setX(long xPos){
this.xPos = xPos;
this.xDisplay = xOrigo + xPos - radius;
}
public void setY(long yPos){
this.yPos = yPos;
this.yDisplay = yOrigo - yPos - radius;
}
public void incX(){
this.xPos++;
this.xDisplay = xOrigo + xPos - radius;
}
public void decX(){
this.xPos--;
this.xDisplay = xOrigo + xPos - radius;
}
public void incY(){
this.yPos++;
this.yDisplay = yOrigo - yPos - radius;
}
public void decY(){
this.yPos--;
this.yDisplay = yOrigo - yPos - radius;
}
public void setForce(float fx, float fy){
this.fx += fx;
this.accX = fx / this.mass;
this.fy += fy;
this.accY = fy / this.mass;
}
public void setVelocity(float vx, float vy){
this.vx += vx;
this.vy += vy;
}
public void displayPlanet(Graphics g){
g.setColor(this.color);
g.fillOval((int )this.xDisplay, (int)this.yDisplay, this.radius*2, this.radius*2); //temporary fix
}
public long getXPos(){
return this.xPos;
}
public long getYPos(){
return this.yPos;
}
public void resetForce(){
this.fx = 0;
this.fy = 0;
}
}
PHYSICS:
import java.awt.Color;
import java.util.Random;
public class Physics {
public static Random rand = new Random();
public static final double G = 6.67384*(Math.pow(10, -11));
public static long astUnit = 149597871000L; //L IS TO INDICATE IT'S A LONG VALUE, otherwise neg value
public static float massEarth = (float)(5.97219*Math.pow(10,24));
public static float massSun = (float)(1.9891*Math.pow(10,30));
public static float massMars = (float) (6.41693*(Math.pow(10,23)));
public static float randomMass(){
return (float) Math.pow(((rand.nextDouble()*(massSun-massEarth))),rand.nextDouble())+massEarth;
}
public static double randInitV(){
return (double) rand.nextDouble()*Math.pow(10,4);
}
public static int randomXPos(){
return rand.nextInt(Main.width)-Main.width/2;
}
public static int randomYPos(){
return rand.nextInt(Main.height)-Main.height/2;
}
public static Color randomColor(){
return new Color(rand.nextInt(0xffffff));
}
public static int randomRadius(){
return rand.nextInt(50)+5;
}
public Physics(){
}
/*
public static Vector getVectorBetween(Body a, Body b){
float force = (float)((G*a.mass*b.mass) / Math.pow(getDistanceBetween(a,b),2));
double angle = Math.atan2(Math.abs(a.y - b.y),Math.abs(a.x - b.x));
Vector vector = new Vector(force,angle);
return vector;
}
*/
public static double getInitVy(long d, Body a){
return Math.sqrt((G*a.mass) / d);
}
public static float getFx(Body a, Body b){
float force = getForceBetween(a,b);
double angle = getAngleBetween(a,b);
float fx = (float)(force*Math.cos(angle));
if(a.xPos > b.xPos){
return -fx;
}else{
return fx;
}
}
public static float getFy(Body a, Body b){
float force = getForceBetween(a,b);
double angle = getAngleBetween(a,b);
float fy = (float)(force*Math.sin(angle));
if(a.yPos > b.yPos){
return -fy;
}else{
return fy;
}
}
public static float getForceBetween(Body a, Body b){
float force = (float)((G*a.mass*b.mass) / Math.pow(getDistanceBetween(a,b),2));
return force;
}
public static double getDistanceBetween(Body a, Body b){
double xKatet = Math.abs(a.getXPos()*Main.distanceScale - b.getXPos()*Main.distanceScale);
double yKatet = Math.abs(a.getYPos()*Main.distanceScale - b.getYPos()*Main.distanceScale);
double distance = Math.hypot(xKatet, yKatet);
return distance;
}
public static double getAngleBetween(Body a, Body b){
long deltaX = Math.abs(a.xPos-b.xPos);
long deltaY = Math.abs(a.yPos-b.yPos);
double angle = Math.atan2(deltaY, deltaX);
return angle;
}
}
(Given the few information and the wall of code, I may be going too far out on a limb, but in doubt, I can delete the answer)
You are accessing the same list with different threads. The collision handling method
public void collision(Body a, Body b) {
...
bodies.remove(a);
bodies.remove(b);
bodies.add(newBody);
}
is executed by the main physics thread that is started in the Main#start() method. This thread is modifying the list. And this may happen while the Swing Event Dispatch Thread (that is also reponsible for painting) is iterating over the bodies in the paintComponent method:
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
...
for(int i=0;i<bodies.size();i++){
bodies.get(i).displayPlanet(g2d);
}
}
Adding additional checks will not solve this issue. You'll need some form of synchronization. The brute-force-hammer would be to just synchronize on the bodies list:
public void collision(Body a, Body b) {
...
synchronized (bodies)
{
bodies.remove(a);
bodies.remove(b);
bodies.add(newBody);
}
}
and
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
...
synchronized (bodies)
{
for(int i=0;i<bodies.size();i++){
bodies.get(i).displayPlanet(g2d);
}
}
}
(basically, something like this would have to be done in all places where one thread might read the list while another thread is writing to the list).
But again: This is rather pragmatic. You could consider one of the thread-safe data structures from the java.util.concurrent package, or some manual, more fine-grained locking solution, probably with some ReadWriteLock.
It's far too much code to give a more focussed answer here (but at least, I wanted to point out what is (almost certainly) the reason for your problem)
You have a problem in your method Main.update(), the essentials of which are:
public void update(float deltaTime){
for(int i=0; i<bodies.size();i++){
// ...
//sets the forces for all bodies
for(int n=0; n<bodies.size();n++){
// ...
//collision detection
if(Physics.getDistanceBetween(bodies.get(i), bodies.get(n)) < (bodies.get(i).radius + bodies.get(n).radius)*distanceScale){
collision(bodies.get(i),bodies.get(n));
}
}
}
}
In the event that i is bodies.size() - 1 and there is a collision with another body, the two colliding bodies are replaced with a single body, reducing the total number of bodies by 1. At that point i is bodies.size(), which is not a valid index into bodies. You nevertheless continue the inner loop, during which you perform bodies.get(i) again, generating an IndexOutOfBoundsException.

Multiple moving Graphics in Java JFrame

I'm new enough to Java and I'm learning Game Design at the minute. I'm kind of at the beginning so its not a Game really.
I'm dealing with a single thread and an array of numbers.I have to get multiple square shapes moving around a screen at the same time. I have the code running fine for one square but run into trouble when I try implement it to multiple squares. I'm working with two Interfaces and I don't know how to use the Array to make multiple cases of a single random square. Any help would really be appreciated.
Cheers guys.
import java.awt.*;
import javax.swing.*;
public class MovingSquares extends JFrame implements Runnable
{
private static final Dimension WindowSize = new Dimension(600, 600);
private static final int NUMGAMEOBJECTS = 30;
private GameObject[] GameObjectsArray = new GameObject[NUMGAMEOBJECTS];
static int strtCoX = (int) (Math.random() * 600);
static int strtCoY = (int) (Math.random() * 600);
public MovingSquares() {
this.setTitle("Crazy squares");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2;
int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
Thread t = new Thread(this);
t.start();
}
public void run() {
while (true) {
try {
Thread.sleep(20);
} catch (InterruptedException e) { }
GameObject.move();
this.repaint();
}
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, WindowSize.width, WindowSize.height);
int size = 50;
int R = (int) (Math.random() * 256);
int G = (int) (Math.random() * 256);
int B = (int) (Math.random() * 256);
Color c = new Color(R, G, B);
g.setColor(c);
g.fillRect(strtCoX, strtCoY, size, size);
}
public static void main(String[] args) {
MovingSquares M = new MovingSquares();
}
}
Second Interface - GameObject class.
import java.awt.*;
public class GameObject
{
private static double x;
private static double y;
private Color c;
public static int ranCoX = (int) (Math.random() * 20);
public static int ranCoY = (int) (Math.random() * 20);
public GameObject() {
int strtX = (int) x;
int strtY = (int) y;
int speedX = (int) (Math.random() * 10);
int speedY = (int) (Math.random() * 10);
}
public static void move() {
int velX = (int) (Math.random() * ranCoX);
int velY = (int) (Math.random() * ranCoY);
if (MovingSquares.strtCoY > 600)
MovingSquares.strtCoY = MovingSquares.strtCoY - 550;
else if (MovingSquares.strtCoX > 600)
MovingSquares.strtCoX = MovingSquares.strtCoX - 550;
MovingSquares.strtCoX += velX;
MovingSquares.strtCoY += velY;
}
public static void paint(Graphics g) {
int size = 50;
x = (Math.random() * 600);
y = (Math.random() * 600);
int R = (int) (Math.random() * 256);
int G = (int) (Math.random() * 256);
int B = (int) (Math.random() * 256);
Color c = new Color(R, G, B);
g.setColor(c);
g.fillRect((int)x, (int) y, size, size);
}
}
Here's a version of Moving Squares that hopefully will help you understand how to do an animation.
One of the first things I did was to separate the classes into either a model class, a view class, or a controller class. The model / view / controller pattern helps to separate concerns.
Let's look at the revised version of your model class, the GameObject class.
package com.ggl.moving.squares;
import java.awt.Color;
import java.awt.Graphics;
public class GameObject {
private static final int size = 50;
private double x;
private double y;
private double dx;
private double dy;
private int drawingWidth;
public GameObject(int drawingWidth) {
x = Math.random() * drawingWidth;
y = Math.random() * drawingWidth;
dx = Math.random() * 30D - 15D;
dy = Math.random() * 30D - 15D;
this.drawingWidth = drawingWidth;
}
public void move() {
int lowerLimit = size;
int upperLimit = drawingWidth - size;
x += dx;
if (x < lowerLimit) {
x += upperLimit;
} else if (x > upperLimit) {
x -= upperLimit;
}
y += dy;
if (y < lowerLimit) {
y += upperLimit;
} else if (y > upperLimit) {
y -= upperLimit;
}
}
public void draw(Graphics g) {
g.setColor(generateRandomColor());
g.fillRect((int) x, (int) y, size, size);
}
private Color generateRandomColor() {
int R = (int) (Math.random() * 256);
int G = (int) (Math.random() * 256);
int B = (int) (Math.random() * 256);
Color c = new Color(R, G, B);
return c;
}
}
There's only one static field left, the size of the square. Everything else is a dynamic variable or method. This allows us to create more than one instance of the GameObject.
I changed the name of the drawing method from paint to draw. This is so we don't get confused about which methods are Swing methods, and which methods are our methods.
We pass the width of the drawing panel into the constructor. That way, we only have to define the width in one place. You can see in the move method that we allow a margin the size of the square in the drawing area.
The constructor defines a random initial position and initial velocity. Th move method merely keeps the square moving in a straight line.
Next, let's look at the revised version of your main class, the MovingSquares class.
package com.ggl.moving.squares;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MovingSquares implements Runnable {
private static final int DRAWING_WIDTH = 600;
private static final int NUMGAMEOBJECTS = 30;
private GameObject[] gameObjectsArray = new GameObject[NUMGAMEOBJECTS];
private JFrame frame;
private MovingPanel movingPanel;
private ObjectsRunnable objectsRunnable;
public MovingSquares() {
for (int i = 0; i < gameObjectsArray.length; i++) {
gameObjectsArray[i] = new GameObject(DRAWING_WIDTH);
}
}
#Override
public void run() {
frame = new JFrame();
frame.setTitle("Crazy squares");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});
movingPanel = new MovingPanel(gameObjectsArray, DRAWING_WIDTH);
frame.add(movingPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
objectsRunnable = new ObjectsRunnable(this, gameObjectsArray);
new Thread(objectsRunnable).start();
}
private void exitProcedure() {
objectsRunnable.setRunning(false);
frame.dispose();
System.exit(0);
}
public void repaintMovingPanel() {
movingPanel.repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new MovingSquares());
}
}
We define the width of the drawing panel here, as well as an array to hold the game objects.
We start the Swing application on the Event Dispatch thread (EDT) by invoking the SwingUtilities invokeLater method. A Swing application must always start on the EDT.
We create the game objects in the constructor and create the Swing components in the run method. I moved the drawing panel into its own class, which we'll talk about later.
We use a window listener so we can stop the thread when we're done with the application.
We pack the JFrame. The only place we're specifying a size is when we create the drawing panel. We use a JFrame. The only time you extend a Swing component, or any Java class, is when you want to override one of the methods.
Let's look at the drawing panel class, the MovingPanel class.
package com.ggl.moving.squares;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class MovingPanel extends JPanel {
private static final long serialVersionUID = -6291233936414618049L;
private GameObject[] gameObjectsArray;
public MovingPanel(GameObject[] gameObjectsArray, int width) {
this.gameObjectsArray = gameObjectsArray;
setPreferredSize(new Dimension(width, width));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
for (int i = 0; i < gameObjectsArray.length; i++) {
gameObjectsArray[i].draw(g);
}
}
}
We override the paintComponent method to draw on the JPanel. Since the game objects draw themselves, we call the draw method on each game object.
Finally, let's look at the animation runnable, the ObjectsRunnable class.
package com.ggl.moving.squares;
import javax.swing.SwingUtilities;
public class ObjectsRunnable implements Runnable {
private volatile boolean running;
private GameObject[] gameObjectsArray;
private MovingSquares movingSquares;
public ObjectsRunnable(MovingSquares movingSquares,
GameObject[] gameObjectsArray) {
this.movingSquares = movingSquares;
this.gameObjectsArray = gameObjectsArray;
this.running = true;
}
#Override
public void run() {
while (running) {
updateObjects();
xxx();
sleep();
}
}
private void updateObjects() {
for (int i = 0; i < gameObjectsArray.length; i++) {
gameObjectsArray[i].move();
}
}
private void xxx() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
movingSquares.repaintMovingPanel();
}
});
}
private void sleep() {
try {
Thread.sleep(200L);
} catch (InterruptedException e) {
}
}
public synchronized void setRunning(boolean running) {
this.running = running;
}
}
This is a straightforward animation or game loop. I put the repaint call in another SwingUtilities invokeLater method to ensure that the Swing components are updated on the EDT.
I hope this was helpful to you.
Static variables will only be available once per Runtime. To create several GameObjects you have to avoid the static keywords.
Then call new GameObject() several times to create serveral instances of GameObject, each with its own set of variables.
edit:
move the variables strtCoX and strtCoY into GameObject (as suggested by #andrew-thomson )
fix all references to strtCoX and strtCoY inside of GameObject
change g.fillRect(strtCoX, strtCoY, size, size); to for (GameObject currentObject : GameObjectsArray) g.fillRect(currentObject.strtCoX, currentObject.strtCoY, size, size);
Explanation: The coordinate are attributes of GameObject. Each GameObject should have its own coordinates.
=> Your code should work just as before
remove all static keywords in GameObject
change GameObject.move(); to for (GameObject currentObject : GameObjectsArray) currentObject.move();
initialize you GameObjectArray in public MovingSquares() constructor like this: for (int i = 0; i < GameObjectsArray.length; i++) GameObjectsArray[i] = new GameObject();
Explanation: with new GameObject() we are creating 30 instances (in that loop inside the constructor). Therefore we also have to call move() 30 times (and that is why that is also nested in a loop)
=> Wear sunglasses!

Categories