3

I want an circular movement of an image in JAVA, i thought I have the solution but it doesn't work and i'm a bit clueless now.

For calculating the points it needs to go im using pythagoras to calculate the height (point B).

if it does one round im satisfied but more rounds would be cool.

The image size is around 500 x 300 pixels.

Here's my code :

package vogel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class Vogel extends Component {
    private int x;
    private int r;
    private int b;
    BufferedImage img;

    public vogel() {
       try {
           img = ImageIO.read(new File("F:/JAVA/workspace/School/src/vogel/vogel.png"));
       } catch (IOException e) {
       } 
       r = 60;
       x = 10;
    }

    @Override
    public void paint(Graphics g) {
        for(int i = -x; i <= x; i++) {      
            b = (int)Math.sqrt(r^2 - i^2);
            g.drawImage(img, x, b, this);
        }        
    }

    public static void main(String[] args) { 
    JFrame f = new JFrame("Vogel");   
    f.setSize(1000,1000);
    f.add(new Vogel());        
    f.setVisible(true);

    for (int number = 1; number <= 1500000; number++) {
        f.repaint();

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {}
        }
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
RR_1990
  • 193
  • 1
  • 3
  • 15
  • 1
    there's a very similar thread http://stackoverflow.com/questions/7752167/let-an-image-fly-in-a-circle-in-java – Hachi Oct 13 '11 at 10:26
  • Also, could you indent code consistently & be more careful with how you post it to the forum. Both your postings so far did not use code formatting for the final closing `}`. – Andrew Thompson Oct 13 '11 at 10:31

2 Answers2

3

Using your loop in the paint(Graphics) method, it draws 21 birds with one repaint.

You should do it with an angle stored in an object variable and use the Math.sin() and Math.cos() function calculate the x and y position. The angle should be increased with every repaint().

To add:

// To control the radius of moving
private final double MAX_X = 200;
private final double MAX_Y = 200;

private double angle = 0;

@Override
public void paint(Graphics g) {
    // increase angle (should be a double value)
    angle += 0.1;

    // rotate around P(0/0), assuming that 0° is vector (1/0)
    int x = (int) (Math.cos(angle) * MAX_X);
    int y = (int) (Math.sin(angle) * MAX_Y);

    // move P to center of JFrame (width and height = 1000)
    x += 500;
    y += 500;

    // image is 500x300, calc upper left corner
    x -= 250;
    y -= 150;

    // draw
    g.drawImage(img, x, y, null);
}

To remove:

private double x, b, r;

So this is the code, try it.

Sibbo
  • 3,796
  • 2
  • 23
  • 41
  • i think the angle needs to be in radians - use Math.toRadians(angle) – Bob Oct 13 '11 at 10:49
  • http://www.cafeaulait.org/course/week4/40.html use this to check out some examples of java.lang.Math Methods – Jay Oct 13 '11 at 11:03
1

Addition to Sibbo's code to convert angle to rads

private double angle = 0.1;

@Override
public void paint(Graphics g) {
    // increase angle (should be a double value

    double random = angle * 2.0 * Math.PI/360.0; //this will convert it to rads
    // rotate around P(0/0)
    int x = (int) (Math.cos(random) * MAX_X);
    int y = (int) (Math.sin(random) * MAX_Y);

    // move P to center of JFrame (width and height = 1000)
    x += 500;
    y += 500;

    // image is 500x300, calc upper left corner
    x -= 250;
    y -= 150;
    angle++
    // draw
    g.drawImage(img, x, y, null);
}
Jay
  • 520
  • 2
  • 9
  • 23
  • 1
    I'm just calculating with rads, since speed of movement is an optical issue and nothing like with 5°/s it's perfect ;) – Sibbo Oct 13 '11 at 10:59