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. :)
Related
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 :)
Could someone explain to me why I am getting these errors on my coding when it comes to the main method project5 class. thearray[count++] keeps giving me errors, and I am not entirely sure where I made my error. I've been stuck on it for awhile now. Some guidance would be nice.
Here is my main method:
public class Project5 {
private Shape [] thearray = new Shape[100];
public static void main (String [] args) {
Project5 tpo = new Project5();
tpo.run();
}
public void run () {
int count = 0;
thearray[count++] = new Circle(20, 20, 40);
thearray[count++] = new Triangle(70, 70, 20, 30);
thearray[count++] = new Rectangle(150, 150, 40, 40);
for (int i = 0; i < count; i ++ ) {
thearray[i].display();
}
int offset = 0;
double totalarea = 0.0;
while (thearray[offset] != null) {
totalarea = totalarea + thearray[offset].area();
offset++;
}
System.out.println("The total area for " + offset + " Shape objects is " + totalarea);
}
}
Here is my Circle class:
public class Circle {
private double radius;
public Circle() {
radius = 1.0;
}
public Circle(double newRadius) {
radius = 1.0;
setRadius(newRadius);
}
public void setRadius(double newRadius) {
if(newRadius > 0) {
radius = newRadius;
}
else {
System.out.println("Error: "
+ newRadius + " is a bad radius value.");
}
}
public double getRadius() {
return radius;
}
public double getArea() {
return radius * radius * Math.PI;
}
}
and here is my Shape class:
abstract class Shape{
int x = 1;
int y = 1;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
public void display() {
}
public abstract double area();
}
I would post my triangle and rectangle class, but I figured this would be enough to at least have it explained where I am messing up.
I looked at your Circle class and the constructor currently only takes one argument. However, when you create a Circle you are specifying 3 arguments.
Circle should probably extend from Shape and the first two parameters are then the x and y position of the shape, correct? If this is the case, change your class as follows:
public class Circle extends Shape {
private double radius;
public Circle(int x, int y) {
super(x, y);
radius = 1.0;
}
public Circle(int x, int y, double newRadius) {
super(x, y);
setRadius(newRadius);
}
...etc
I was given an assignment to create a 3-d shape and prompt the user for the surface area and volume. I have multiple errors in my coding, but after research, I can not solve the problems.I attempted to fix the problem,but can not so I have // it out. Can anyone solve or steer me in the right direction?
The code is below:
//Point originOne = new Point(23, 94);
//Rectangle rectOne = new Rectangle(originOne, 100, 200);
//Rectangle rectTwo = new Rectangle(50,100);
//Point originOne;
//Point originOne = new Point(23, 94);
import javax.swing.JOptionPane;
public static void main(String[] args) {
int width;
int height;
Rectangle(volume,area);
JOptionpane.showMessageDialog("please input integer");
public static int volume;
int vol;
int side;
vol =side*3;
public static int area;
int
JOptionPane.showMessageDialog( null,"information",
, JOptionPane.OK_CANCEL_OPTION);
JOptionPane.showInputDialog("Please input a value");
public static int surfacearea;
}
public class Rectangle {
public int x = 0;
public int y = 0;
//constructor
public void Point(int a, int b) {
x = a;
y = b;
}
public int width = 0;
public int height = 0;
public int Point ;
public int origin;
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}
// a method for computing the area of the rectangle
public int Area() {
//return width * height;
}
}
This should help slightly.. There is still a lot wrong with it. I hope my comments help a little, if you need anything else feel free to comment.
import javax.swing.JOptionPane;
import java.awt.Point;
public class Rectangle {
public static int area;
public static int volume;
public int x = 0;
public int y = 0;
public int width = 0;
public int height = 0;
public int Point; // Uh?
public int origin; // this isnt a Point and yet you are creating an instance of Point..
//constructor
/**
* What is even the point of this????
*/
public void Point(int a, int b) {
x = a;
y = b;
}
// main needs to go in a class
public static void main(String[] args) {
int width;
int height;
Rectangle myRectangle = new Rectangle(volume,area); // actually set a variable
//JOptionpane.showMessageDialog("please input integer"); // research this..
int vol;
int side; //side isnt initialized?
vol = side*3; //this will not work
//JOptionPane.showMessageDialog( null,"information", JOptionPane.OK_CANCEL_OPTION);
//JOptionPane.showInputDialog("Please input a value");
}
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
//This isnt calling your point method...
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x; // Again, this is declared as an int.
origin.y = y; // this is declared as a int.
}
// a method for computing the area of the rectangle
public int Area() {
return width * height;
}
}
I'm having a two image(a cat and a dog) inside the world which is in my Board class. The cat moves in a random direction while the dog move only when I press the arrow keys. My problem now is that how can I make the cat disappear whenever there is a collision between the two images? Any answer or idea would be much appreciated.
Here's what I've tried...
public class Cat extends Sprite implements ImageObserver
{
private java.awt.Image catImage;
private final Board board;
private double x;
private double y;
private double speed;
private double angle;
private boolean visible;
public Cat(Board board, double x, double y, double speed)
{
this.board = board;
this.x = x;
this.y = y;
this.speed = convertToMeterPerSecond(speed);
visible = true;
URL iU = this.getClass().getResource("cat.gif");
ImageIcon icon = new ImageIcon(iU);
catImage = icon.getImage();
}
public Image getImage()
{
return catImage;
}
public void move(long dt)
{
double dt_s = dt / 1e9;
double dx_m = speed * dt_s * Math.sin(angle);
double dy_m = speed * dt_s * Math.cos(angle);
final double right_wall = board.x1_world;
final double up_wall = board.y1_world;
final double down_wall = 0.0;
final double left_wall = 0.0;
x += dx_m;
y += dy_m;
if (x >= right_wall)
{
x = right_wall;
setRandomDirection();
}
if (y > up_wall)
{
y = up_wall;
setRandomDirection();
}
if (x <= left_wall)
{
x = left_wall;
setRandomDirection();
}
if (y < down_wall)
{
y = down_wall;
setRandomDirection();
}
}
public void setRandomDirection()
{
Cat myObject = this;
myObject.setAngle(Math.PI * 2 * Math.random());
}
#Override
public void render(Graphics2D g2d)
{
AffineTransform t = g2d.getTransform();
double height = 0.3; //meter
double width = 0.3; //meter
double cat_footy = height;
double cat_footx = width / 2;
int xx = board.convertToPixelX(x - cat_footx);
int yy = board.convertToPixelY(y + cat_footy);
g2d.translate(xx, yy);
double x_expected_pixels = width * board.meter;
double y_expected_pixels = height * board.meter;
double x_s = x_expected_pixels / ((ToolkitImage) catImage).getWidth();
double y_s = y_expected_pixels / ((ToolkitImage) catImage).getHeight();
double w = ((ToolkitImage) catImage).getWidth();
double h = ((ToolkitImage) catImage).getHeight();
g2d.scale(x_s, y_s);
g2d.drawImage(getImage(), 0, 0, this); // upper left corner
g2d.setColor(Color.BLACK);
g2d.drawRect(0, 0, (int) w, (int) h);
g2d.setTransform(t);
}
public void moveAt(double distance_x, double distance_y)
{
this.x = (int) distance_x;
this.y = (int) distance_y;
}
#Override
public Rectangle getBounds()
{
double w = ((ToolkitImage) catImage).getWidth();
double h = ((ToolkitImage) catImage).getHeight();
return new Rectangle((int) x, (int) y, (int) w, (int) h);
}
public void setAngle(double angle)
{
this.angle = angle;
}
public boolean isVisible()
{
return visible;
}
public void setVisible(Boolean visible)
{
this.visible = visible;
}
#Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
return true;
}
}
for my Cat class
public class Dog extends Sprite implements ImageObserver
{
private java.awt.Image humanImage;
private final Board board;
private double x;
private double y;
private double speed;
private boolean visible;
private double angle;
private double dx_m;
private double dy_m;
public Dog(Board board, double x, double y, double speed)
{
this.board = board;
this.x = x;
this.y = y;
this.speed = convertToMeterPerSecond(speed);
visible = true;
URL iU = this.getClass().getResource("dog.jpg");
ImageIcon icon = new ImageIcon(iU);
dogImage = icon.getImage();
}
public Image getImage()
{
return dogImage;
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
dx_m = -0.3;
}
if (key == KeyEvent.VK_RIGHT)
{
dx_m = 0.3;
}
if (key == KeyEvent.VK_UP)
{
dy_m = 0.3;
}
if (key == KeyEvent.VK_DOWN)
{
dy_m = -0.3;
}
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
dx_m = 0;
}
if (key == KeyEvent.VK_RIGHT)
{
dx_m = 0;
}
if (key == KeyEvent.VK_UP)
{
dy_m = 0;
}
if (key == KeyEvent.VK_DOWN)
{
dy_m = 0;
}
}
#Override
public void move(long dt)
{
double dt_s = dt / 1e9;
final double right_wall = board.x1_world;
final double up_wall = board.y1_world;
final double down_wall = 0.0;
final double left_wall = 0.0;
x += dx_m;
y += dy_m;
if (x <= left_wall)
{
x = left_wall;
}
if (x >= right_wall)
{
x = right_wall;
}
if (y <= down_wall)
{
y = down_wall;
}
if (y >= up_wall)
{
y=up_wall;
}
}
public void setRandomDirection()
{
Dog myObject = this;
myObject.setAngle(Math.PI * 2 * Math.random());
}
#Override
public void render(Graphics2D g2d)
{
AffineTransform t = g2d.getTransform();
final double dogHeight = 1.6;// meter
final double dogWidth = 1.8; //meter
final double foot_position_y = dogHeight;
final double foot_position_x = dogWidth / 2;
int xx = board.convertToPixelX(x - foot_position_x); // to find the upper-left corner
int yy = board.convertToPixelY(y + foot_position_y); // to find the upper-left corner
g2d.translate(xx, yy);
// ratio for actual Image size
double x_expected_pixels = dogHeight * board.meter;
double y_expected_pixels = dogWidth * board.meter;
double w = ((ToolkitImage) dogImage).getWidth();
double h = ((ToolkitImage) dogImage).getHeight();
double x_s = x_expected_pixels / w;
double y_s = y_expected_pixels / h;
g2d.scale(x_s, y_s);
g2d.drawImage(getImage(), 0, 0, this); // upper left corner
g2d.setColor(Color.BLACK);
g2d.drawRect(0, 0, (int) w, (int) h);
g2d.setTransform(t);
}
#Override
public void moveAt(double distance_x, double distance_y)
{
this.x = distance_x;
this.y = distance_y;
}
public void setAngle(double angle)
{
this.angle = angle;
}
#Override
public Rectangle getBounds()
{
double width = ((ToolkitImage) dogImage).getWidth();
double height = ((ToolkitImage) dogImage).getHeight();
return new Rectangle((int) x, (int) y, (int) width, (int) height);
}
public boolean isVisible()
{
return visible;
}
public void setVisible(Boolean visible)
{
this.visible = visible;
}
#Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
return true;
}
}
for my Dog class
public class Board extends Canvas
{
private Cat cat;
public static final long SECOND = 1000 * 1000 * 1000;
public double meter;//PIXEL
private HumanBeing humanBeing;
/**
* ascending from 0 to N
* 0 : most far way...
* N : is the closest (painted the last)
*/
private final java.util.List<Sprite> z_sorted_sprites = new ArrayList<Sprite>();
private BufferStrategy strategy;
int x0_pixel;
int y0_pixel;
int x1_pixel;
int y1_pixel;
double x1_world;
double y1_world;
private final Frame frame;
public Board(Frame frame, double meter)
{
addKeyListener(new TAdapter());
this.frame = frame;
this.setIgnoreRepaint(true);
this.meter = meter;
setFocusable(true);
dog = new Dog(this, 5, 5, 40);
init();
addComponentListener(new ComponentAdapter()
{
#Override
public void componentResized(ComponentEvent e)
{
render();
}
});
}
public void init()
{
z_sorted_sprites.add(new Cat(this, 0, 0, 30));
z_sorted_sprites.add(new Cat(this, 1, 1, 10));
z_sorted_sprites.add(new Cat(this, 2, 2, 20));
z_sorted_sprites.add(new Cat(this, 3, 3, 100));
}
public void render()
{
setupStrategy();
x0_pixel = 0;
y0_pixel = 0;
x1_pixel = getWidth();
y1_pixel = getHeight();
x1_world = x1_pixel / meter;
y1_world = y1_pixel / meter;
Graphics2D g2d = (Graphics2D) strategy.getDrawGraphics();
g2d.setBackground(Color.lightGray);
g2d.clearRect(0, 0, x1_pixel, y1_pixel);
g2d.setColor(Color.BLACK);
for (double x = 0; x < x1_world; x++)
{
for (double y = 0; y < y1_world; y++)
{
int xx = convertToPixelX(x);
int yy = convertToPixelY(y);
g2d.drawOval(xx, yy, 2, 2);
}
}
for (Sprite z_sorted_sprite : z_sorted_sprites)
{
z_sorted_sprite.render(g2d);
}
dog.render(g2d);
g2d.dispose();
strategy.show();
Toolkit.getDefaultToolkit().sync();
}
public int convertToPixelX(double distance)
{
return (int) (distance * meter);
}
public int convertToPixelY(double y_world)
{
return (int) (y1_pixel - (y_world * meter));
}
public void onZoomUpdated(int value)
{
meter = value;
render();
}
private void setupStrategy()
{
if (strategy == null)
{
this.createBufferStrategy(2);
strategy = this.getBufferStrategy();
}
}
public void start() throws InterruptedException
{
long prevLoopStart = System.nanoTime();
Avg avg = new Avg();
while (true)
{
final long loopStart = System.nanoTime();
final long dt = loopStart - prevLoopStart;
for (Sprite sprite : z_sorted_sprites)
{
sprite.move(dt);
}
dog.move(dt);
render();
frame.onFpsUpdated(1.0 / dt * SECOND, avg.add(loopStart));
final long elapsed_ns = System.nanoTime() - loopStart;
long expected_elapsed_ms = 1000 / 60;
long elapsed_ms = (long) (elapsed_ns / (1000.0 * 1000.0));
long sleep_ms = expected_elapsed_ms - elapsed_ms;
if (sleep_ms > 0)
{
Thread.sleep(sleep_ms /* ms */);
}
prevLoopStart = loopStart;
}
}
private void checkCollision()
{
Rectangle r2 = cat.getBounds();
Rectangle r3 = dog.getBounds();
if (r3.intersects(r2))
{
dog.setVisible(false);
cat.setVisible(false);
}
}
static class Avg
{
java.util.List<Long> ticks = new ArrayList<Long>();
/**
* #return the rate for the last second
*/
int add(long tick)
{
ticks.add(0, tick);
if (ticks.size() < 2)
{
return -1;
}
int last = -1;
for (int pos = ticks.size() - 1; pos >= 0; pos--)
{
if (tick - ticks.get(pos) <= SECOND)
{
last = pos;
break;
}
}
while (ticks.size() - 1 > last)
{
ticks.remove(ticks.size() - 1);
}
return ticks.size();
}
}
private class TAdapter extends KeyAdapter
{
public void keyReleased(KeyEvent e)
{
dog.keyReleased(e);
}
public void keyPressed(KeyEvent e)
{
dog.keyPressed(e);
}
}
}
For my Board class
public abstract class Sprite
{
public Sprite()
{
}
public Rectangle getBounds()
{
return new Rectangle();
}
public static double convertToMeterPerSecond(double speed)
{
// 25 km / hour
// 25000 m / 3600 s
return speed / 3.6;
}
public abstract void move(long dt);
public abstract void moveAt(double distance_x, double distance_y);
public abstract void render(Graphics2D g2d);
public abstract void setVisible(Boolean visible);
}
For my sprite class
public boolean checkCollisions(java.util.List<Sprite> sprites)
{
Dog dog = this;
Rectangle r1 = dog.getBounds();
for (int i = 0; i < sprites.size(); i++)
{
Rectangle r2 = sprites.get(i).getBounds();
if (r1.intersects(r2))
{
sprites.remove(i);
}
}
return true;
}
You have given a lot of code, but as Sibbo said, I don't see your checkCollisions method being called anywhere. It should be called every loop of your game.
Check out this tutorial, specifically look at the gameLoop method in the Game class. When I made a sprite based game that required a lot of collision detection this tutorial helped me out a lot.
I'd implement method that detects that positions of cat and dog overlap in the board class since board is the only instance that "knows" both dog and cat. The implementation is pretty simple: compare coordinates (something like dog.x + dog.width < cat.x || dog.x > cat.x + cat.width etc, etc.
If future you can implement more generic method, so if you will wish to add mouse you will be able to reuse the code.
The dog class doesn't overwrite the getBounds() method. So everytime you check if the rectangle (0, 0, 0, 0) intersects for example (3, 4, 50, 50) (if the cat is at (3, 4)).
Where do you call the checkCollision() method?
EDIT:
Create a method like your checkCollision() in your Dog class:
public boolean checkCollision(Sprite s) {...}
It should return true, when a collision is detected. Call this method from the Board.start() method for every Sprite in z_sorted_sprites. IF it returns true, remove the Sprite from the list.
I have an image of a man that moves in x-axis. Now I want to move the man with its corresponding speed of 5 meter/s with delta time which is in nanoseconds and that is my problem. Could you give me some idea on how to do it?
Any help would be much appreciated...
Here's the code :
public class Board extends Canvas
{
public double meter;//PIXEL
private final java.util.List<Sprite> sprites = new ArrayList<Sprite>();
private final java.util.List<Sprite> z_sorted_sprites = new ArrayList<Sprite>();
private BufferStrategy strategy;
int x0_pixel;
int y0_pixel;
int x1_pixel;
int y1_pixel;
double x1_world;
double y1_world;
public Board(double meter)
{
this.setIgnoreRepaint(true);
this.meter = meter;
init();
addComponentListener(new ComponentAdapter()
{
#Override
public void componentResized(ComponentEvent e)
{
render();
}
});
}
public void init()
{
HumanBeing humanBeing = new HumanBeing(this, 2, 2, 0);
sprites.add(humanBeing);
z_sorted_sprites.add(humanBeing);
}
#Override
public void paint(Graphics g)
{
}
public void render()
{
setupStrategy();
x0_pixel = 0;
y0_pixel = 0;
x1_pixel = getWidth();
y1_pixel = getHeight();
x1_world = x1_pixel / meter;
y1_world = y1_pixel / meter;
Graphics2D g2d = (Graphics2D) strategy.getDrawGraphics();
g2d.setBackground(Color.lightGray);
g2d.clearRect(0, 0, x1_pixel, y1_pixel);
g2d.setColor(Color.BLACK);
for (double x = 0; x < x1_world; x++)
{
for (double y = 0; y < y1_world; y++)
{
int xx = convertToPixelX(x);
int yy = convertToPixelY(y);
g2d.drawOval(xx, yy, 2, 2);
}
}
for (Sprite z_sorted_sprite : z_sorted_sprites)
{
z_sorted_sprite.render(g2d);
}
g2d.dispose();
strategy.show();
Toolkit.getDefaultToolkit().sync();
}
public int convertToPixelX(double distance)
{
return (int) (distance * meter);
}
public int convertToPixelY(double y_world)
{
return (int) (y1_pixel - (y_world * meter));
}
public void onZoomUpdated(int value)
{
meter = value;
render();
}
private void setupStrategy()
{
if (strategy == null)
{
this.createBufferStrategy(2);
strategy = this.getBufferStrategy();
}
}
public void start() throws InterruptedException
{
long previousTime = System.nanoTime();
while (true)
{
long now = System.nanoTime();
long dt = now - previousTime;
for (Sprite sprite : sprites)
{
sprite.move(0);
}
render();
Thread.sleep(1);
previousTime = now;
}
}
}
for Human Class
public class HumanBeing extends Sprite implements ImageObserver
{
private java.awt.Image humanImage;
private final Board board;
private double x;
private double y;
private int speed;
private java.util.List<Sprite> objects = new ArrayList<Sprite>();
private int cImage;
public HumanBeing(Board board, int x, int y, int speed)
{
this.board = board;
this.x = x;
this.y = y;
this.speed = speed;
URL iU = this.getClass().getResource("human.jpg");
ImageIcon icon = new ImageIcon(iU);
humanImage = icon.getImage();
objects.add(this);
}
public Image getImage()
{
return humanImage;
}
#Override
public void move(long ns)
{
x += 0.001;
}
#Override
public void render(Graphics2D g2d)
{
AffineTransform t = g2d.getTransform();
final double humanHeight = 1.6;// meter
final double humanWidth = 1.8; //meter
final double foot_position_x = humanHeight / 2;
final double foot_position_y = humanWidth;
int xx = board.convertToPixelX(x - foot_position_x); // to find the upper-left corner
int yy = board.convertToPixelY(y + foot_position_y); // to find the upper-left corner
g2d.translate(xx, yy);
// ratio for actual Image size
double x_expected_pixels = humanHeight * board.meter;
double y_expected_pixels = humanWidth * board.meter;
double w = ((ToolkitImage) humanImage).getWidth();
double h = ((ToolkitImage) humanImage).getHeight();
double x_s = x_expected_pixels / w;
double y_s = y_expected_pixels / h;
g2d.scale(x_s, y_s);
g2d.drawImage(getImage(), 0, 0, this); // upper left corner
g2d.setColor(Color.BLACK);
g2d.setTransform(t);
}
#Override
public void moveAt(double distance_x, double distance_y)
{
this.x = distance_x;
this.y = distance_y;
}
#Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
{
return false;
}
}
Here's an idea for architecting your solution. I'm going to assume you have figured out how many pixels the image has to move every second to be the speed you want. Let's say that for your game or simulation, that means 10 pixels every second. You have a starting location and an ending location. So you know when you need to move the image. Use the class ScheduledThreadPoolExecutor and its method scheduleWithFixedRate to set up a periodic update of your image's position, issuing a call to draw the image once every second, at an updated location. Remember that your call to position your image could be delayed briefly as the Swing thread is servicing other GUI requests. But your scheduled thread is not affected. Say the scheduled thread says to Swing to put your image at position x and time 1.0 seconds. In fact, Swing gets to it a touch later at time 1.1 seconds. But you don't want that delta to screw up your timing. It doesn't because scheduleWithFixedRate will issue the next Swing call at the correct time 2.0 seconds, not at 2.1 seconds. The second image update is at exactly the right spot at the right time (or close enough depending on how busy the GUI thread is).