Color Fading Algorithm not working? - java

I am trying to create a fading/transitioning colors algorithm between two colors and a timer; the timer will determine how fast the colors swap between one another. The only problem is: the more fading transition objects I add in, the faster they all go. For example: If I add in one StandardFade (the class) object, it will run at the timer (alpha, in the code) I give it. However, if more objects that do 'fade' appear on the screen, the timer is no longer relevant, and they all go at the same rate, faster and faster with each object. Can anyone explain why?
//Test Game Class, Implements the StandardFades
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
public class TestGame extends Canvas implements Runnable{
private static final long serialVersionUID = -7267473597604645224L;
private Thread thread;
private boolean running = false;
private boolean consoleFPS = true;
private boolean titleFPS = true;
private TestWindow window;
//Fade objects, two is for a really long timer to test to see if they would transition at different times
StandardFade one = new StandardFade(Color.RED,Color.BLUE,.005);
StandardFade two = new StandardFade(Color.RED,Color.GREEN,.00000000000000001);
StandardFade three = new StandardFade(Color.RED,Color.YELLOW,.000005);
private int currentFPS;
private int frames;
public int levelNum;
public TestGame(int width, int height, String title){
this.window = new TestWindow(width,height, title, this);
this.initFades();
this.start();
}
private synchronized void start(){
if(running)
return;
else{
this.thread = new Thread(this);
this.thread.start();
this.running = true;
}
}
private synchronized void stop(){
if(!this.running)
return;
else{
try{
this.thread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
this.running = false;
System.exit(0);
}
}
/**
* This game loop was provided by online sources, though there are many examples
* of a game loop online.
*
* #author RealTutsGML
*/
public void run() {
requestFocus(); //Focuses the click/input on the frame/canvas.
long lastTime = System.nanoTime(); //The current system's nanotime.
double ns = 1000000000.0 / 60.0; //Retrieves how many nano-seconds are currently in one tick/update.
double delta = 0; //How many unprocessed nanoseconds have gone by so far.
long timer = System.currentTimeMillis();
int frames = 0; //The frames per second.
int updates = 0; //The updates per second.
while (running) {
boolean renderable = false; //Determines if the game should render the actual graphics.
long now = System.nanoTime();//At this point, the current system's nanotime once again.
delta += (now - lastTime) / ns;
lastTime = now;
//If the amount of unprocessed ticks is or goes above one...
//Also determines if the game should update or not/render. Approximately sixty frames per second.
while (delta >= 1) {
tick();
delta--;
updates++;
renderable = true;
}
if(renderable){
frames++;
render();
}
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(frames);
updates = 0;
frames = 0;
}
}
this.stop();
}
/**
* This method should tick everything that needs to be updated via positioning,
* mouse input, etc.
*/
private void tick(){
/********************PUT ALL TICKABLE METHODS IN THIS METHOD; ALL CALCULATIONS, EVERYTHING*********************/
//this.stdFadeHandler.get(0).tick();
//this.stdFadeHandler.tick();
one.tick();
two.tick();
three.tick();
/**********************************END OF TICK METHOD INFORMATION AND METHODS******************************/
}
private void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, window.getWidth(), window.getHeight());
/*******************PLACE ALL DRAWING INSTRUCTIONS WITHIN THIS SECTION OF THE RENDER METHOD*************************/
g2.setColor(one.getColor());
g2.fillRect(20, 20, 200, 200);
g2.setColor(two.getColor());
g2.fillRect(20, 300, 200, 200);
g2.setColor(three.getColor());
g2.fillRect(20, 540, 200, 200);
//this.stdFadeHandler
/*******************DO NOT PLACE ANY MORE DRAWING INSTRUCTIONS WITHIN THIS SECTION OF THE RENDER METHOD***************/
g.dispose();
g2.dispose();
bs.show();
}
private void initFades(){
}
public static void main(String[] args){
TestGame stdGame = new TestGame(800,800,"Test Standard Game");
}
Below is the actual class that MAKES the frames: StandardFade
import java.awt.Color;
import java.awt.Graphics2D;
public class StandardFade {
private static float time = 0;
private static boolean firstColor = true;
private Color color1;
private Color color2;
private double alpha;
private Color fadeColor;
/**
* Lines that implement the
* r,g and b values come from Princeton University; I will author
* them in at the bottom.
*
* Method takes two parameters and fades them into one another according to a
* timer/clock value: alpha.
* #param c1 First color to be used.
* #param c2 Second color to be used.
* #param alpha How fast colors should shift. 0 <= n <= 1.
* Closer value is to zero, the longer it will take to shift.
* ***Important note about alpha: for non-seizure inducing colors, alpha <= .0005***
*
* The issue that is occurring is that, no matter what I do, no matter if I make
* different StandardFade objects and assign them, they will always render at the
* same rate, and faster and faster, depending on how many objects are fading.
*
* #return new Color based on r, g, and b values calculated.
*
* #author (Only code utilized was lines 58-60):
* http://introcs.cs.princeton.edu/java/31datatype/Fade.java.html
*/
public StandardFade(Color c1, Color c2, double alpha){
this.color1 = c1;
this.color2 = c2;
this.alpha = alpha;
}
public void tick() {
if(time <= 1f && firstColor){
time += alpha;
}
else{
firstColor = false;
}
if(time >= 0f && !firstColor)
time -= alpha;
else{
firstColor = true;
}
//System.out.println(time);
short r = (short) (time * color2.getRed() + (1 - time) * color1.getRed());
short g = (short) (time * color2.getGreen() + (1 - time) * color1.getGreen());
short b = (short) (time * color2.getBlue() + (1 - time) * color1.getBlue());
if(r > 255) r = 255;
if(g > 255) g = 255;
if(b > 255) b = 255;
if(r < 0) r = 0;
if(g < 0) g = 0;
if(b < 0) b = 0;
this.fadeColor = new Color(r, g, b);
}
public Color getColor(){
return this.fadeColor;
}

The only problem is: the more fading transition objects I add in, the faster they all go
private static float time = 0;
You are using a static variable for the time. This variable is shared by all instances of the ColorFade class. So each fading objects updates the same variable.
Don't use a static variable (just get rid of the static keyword). Each fading object needs its own "time" variable.
private float time = 0;
I also question if the firstColor variable should be static.

That's quite a lot of code to go through (try to reduce your problem down as much as possible in future) but based on your description of the problem - it gets faster the more objects you have - I'm guessing your problem is this line:
private static float time = 0;
I assume you're aware that static fields are shared between class instances. They do not each store their own separate value.
This causes problems because each instance increments the time value every time the tick method is called. That's okay when there is only one instance, but problematic when there are more.
Simply remove the static keyword and it should work properly:
private float time = 0;

Related

Why this Simple Loop is Causing Problematic Behavior of my JFrame

I am doing an assignment from the Java Exposure textbook, which was written in 2007. This book includes some code that I usually update to use some of the more recent features (just basic stuff). However, in this one I am running into a problem. All I tried to do is replace the show with setVisible(true) and change the Frame to a JFrame and add a gfx.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);. However, I noticed that this wouldn't actually cause the window to close. If I clicked many times, maybe 1/30 tries it would close. If I reduced the delay from 10 to 1, it usually closed within 2 tries. This of course led me to believe that the delay method is causing this erratic behavior. I tried Thread.sleep, but of course that didn't work. Is there any simply way to get this code so that the frame will close when I hit the close button? If there isn't, what would be the less simple way of doing it?
Here is the code:
// Lab30st.java
// The Screen Saver Program
// Student Version
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JOptionPane;
public class Lab30st
{
public static void main(String args[])
{
GfxApp gfx = new GfxApp();
gfx.setSize(800,600);
gfx.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);}});
gfx.show();
}
}
class GfxApp extends Frame
{
private int circleCount, circleSize;
public GfxApp()
{
circleCount = 50;
circleSize = 30;
}
class Coord
{
private int xPos;
private int yPos;
public Coord(int x, int y)
{
xPos = x;
yPos = y;
}
}
public void paint(Graphics g)
{
int incX = 5;
int incY = 5;
int diameter = 30;
int timeDelay = 10;
Circle c = new Circle(g,diameter,incX,incY,timeDelay);
for (int k = 1; k <= 2000; k++)
{
c.drawCircle(g);
c.hitEdge();
}
}
}
class Circle
{
private int tlX; // top-left X coordinate
private int tlY; // top-left Y coordinate
private int incX; // increment movement of X coordinate
private int incY; // increment movement of Y coordinate
private boolean addX; // flag to determine add/subtract of increment for X
private boolean addY; // flag to determine add/subtract of increment for Y
private int size; // diameter of the circle
private int timeDelay; // time delay until next circle is drawn
public Circle(Graphics g, int s, int x, int y, int td)
{
incX = x;
incY = y;
size = s;
addX = true;
addY = false;
tlX = 400;
tlY = 300;
timeDelay = td;
}
public void delay(int n)
{
long startDelay = System.currentTimeMillis();
long endDelay = 0;
while (endDelay - startDelay < n)
endDelay = System.currentTimeMillis();
}
public void drawCircle(Graphics g)
{
g.setColor(Color.blue);
g.drawOval(tlX,tlY,size,size);
delay(timeDelay);
if (addX)
tlX+=incX;
else
tlX-=incX;
if (addY)
tlY+=incY;
else
tlY-=incY;
}
public void newData()
{
incX = (int) Math.round(Math.random() * 7 + 5);
incY = (int) Math.round(Math.random() * 7 + 5);
}
public void hitEdge()
{
boolean flag = false;
if (tlX < incX)
{
addX = true;
flag = true;
}
if (tlX > 800 - (30 + incX))
{
addX = false;
flag = true;
}
if (tlY < incY + 30) // The +30 is due to the fact that the title bar covers the top 30 pixels of the window
{
addY = true;
flag = true;
}
if (tlY > 600 - (30 + incY))
{
addY = false;
flag = true;
}
if (flag)
newData();
}
}
You are "freezing" the Event Dispatch Thread with
public void delay(int n)
{
long startDelay = System.currentTimeMillis();
long endDelay = 0;
while (endDelay - startDelay < n)
endDelay = System.currentTimeMillis();
}
This means that all the other stuff that is trying to happen (like closing the window) has to wait until the thread comes out of the "sleep".
basically you shouldn't be doing the delay in the EDT, it should be on a different thread and then ask the EDT thread to update.
Your "busy wait" delay may cause other problems too. You can improve the behavior by using Thread.sleep()
See Java Event-Dispatching Thread explanation
That's terrible.
You need to restructure the whole code.
Let's start with the really bad:
delay is (almost) a busy wait, I haven't seen busy waits since BASIC was modern. It basically holds the CPU hostage to the thread, not only does it do nothing, no other thread (almost) can use the time slice. The reason I say almost is that calling the system time function causes a context switch that could allow other threads to run, but it is still bad.
The still pretty bad:
Replacing with Thread.sleep. Better yes, no busy wait, but you are still holding the one and only UI thread. This means no other UI work can happen up to and including closing the main window.
What needs to happen:
Get an external timer (e.g. javax.swing.Timer) to trigger the draw event and do next part of the animation.
Search for "Java smooth animation" there are many examples of how to do this, double buffer and all.

java delta time problems with canvas

hi am trying to make a small game using canvas and bitmaps i want my game to run the same on all devices i found delta time is the best practice for this but for some reason when i try to implement it into my code i have display issues for example am trying to move my coluds in the sky but when i add the delta they all disapere i dont know if im doing it wrong so please can sombody help me heres the code
private float c1x = 0.0f;
private float c2x = cloudWidth;
private float c3x = cloudWidth * 2;
private float cloudSpeed = 0.1f;
private long curentTime;
private long lastTime = 0;
private double delta;
#Override
public void run(){
while(running){
if(!holder.getSurface().isValid()){
continue;
}
curentTime = System.nanoTime();
delta = curentTime - lastTime;
lastTime = curentTime;
cloudMovement();
canvas = holder.lockCanvas();
canvas.drawBitmap(bg, 0, 0, null);
canvas.drawBitmap(sun, 20, 20, null);
canvas.drawBitmap(cloud1, c1x, c1y, null);
canvas.drawBitmap(cloud2, c2x, c2y, null);
canvas.drawBitmap(cloud3, c3x, c3y, null);
holder.unlockCanvasAndPost(canvas);
}
}
private void cloudMovement(){
if(c1x <= 0 - cloudWidth){
c1x = w;
c1y = y.nextInt(rand);
}
if(c2x <= 0 - cloudWidth){
c2x = w;
c2y = y.nextInt(rand);
}
if(c3x <= 0 - cloudWidth){
c3x = w;
c3y = y.nextInt(rand);
}
c1x-=cloudSpeed * delta;
c2x-=cloudSpeed * delta;
c3x-=cloudSpeed * delta;
}
You could use a global FPS mechanism instead which forces a steady FPS on your game :)
If you track the FPS the game will run the same way on any device and you dont need to include delta-times on all update processes.
Here's a code snippet from a FpsTracker i used in an old project:
private static final long SECOND = 1000;
private static final long TARGET_FPS = 40;
private static final long FRAME_PERIOD = SECOND / TARGET_FPS;
private long time = System.currentTimeMillis();
/**
*
* #param startTime
* #return <code>true</code> if the interval between startTime and the time
* when this method was called is smaller or equal to the given
* frame period.
*
* Will return <code>false</code> if the interval was longer.
*/
public boolean doFpsCheck(long startTime) {
if (System.currentTimeMillis() - time >= SECOND) {
time = System.currentTimeMillis();
}
long sleepTime = FRAME_PERIOD
- (System.currentTimeMillis() - startTime);
if (sleepTime >= 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
//TODO handle this properly
e.printStacktrace()
}
return true;
} else {
return false;
}
}
If this method returns false it means that your operations took longer that the timeperiod you gave to one frame. You can react to this by checking the doFpsCheckreturn parameter.
Implementing this in your code would look like this:
#Override
public void run()
{
while(running)
{
if(!holder.getSurface().isValid())
{
continue;
}
startTime = System.currentTimeMillis();
cloudMovement();
canvas = holder.lockCanvas();
canvas.drawBitmap(bg, 0, 0, null);
canvas.drawBitmap(sun, 20, 20, null);
canvas.drawBitmap(cloud1, c1x, c1y, null);
canvas.drawBitmap(cloud2, c2x, c2y, null);
canvas.drawBitmap(cloud3, c3x, c3y, null);
holder.unlockCanvasAndPost(canvas);
doFpsCheck(startTime);
}
}
By the way - it is good practice to devide your game loop into pro sub processes, one being the update process, the other being the draw process.
For many different Bitmaps you should consider extracting the fields and functionalities into seperate classes containing a draw(Canvas c) and update() method. So you wont get a trillion fields on your main class.

BufferedImage, int[] pixels and rendering. How do they work they work together?

There is something in the following code that I am unable to understand. After digging through google for a while, I decided it would be better to ask someone.
I am following a game programming tutorial on youtube, and I feel I understand (to some degree) everything I have written, except for some lines which concern the rendering part of the program.
package com.thomas.game;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import com.thomas.game.graphics.Screen;
import com.thomas.game.input.Keyboard;
public class Game extends Canvas implements Runnable {
private static final int WIDTH = 300;
private static final int HEIGHT = (WIDTH / 16) * 9;
private static final int SCALE = 3;
private static final String TITLE = "Game";
private JFrame frame;
private Thread thread;
private Screen screen;
private BufferedImage image;
private Keyboard key;
private int[] pixels;
private boolean running = false;
private int x = 0, y = 0;
public Game() {
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
screen = new Screen(WIDTH, HEIGHT);
frame = new JFrame();
initializeFrame();
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
this is what I don't understand. I get the image raster, from which I get the databuffer. I typecast that databuffer into a (DatabufferInt), which allows me to retrieve an int[] through the getData() method. After this is done, pixel.length has a value of 48600, and every index contains the int value 0. Operating with this int[] makes the program render like it is supposed to. However, if I don't typecast and retrieve the int[] in the above manner, and instead say pixels = new int[48600], i end up with a black screen.
I guess what I want to know is: what is the difference between these two int[], or rather, what makes the first one work? How does it work?
key = new Keyboard();
addKeyListener(key);
setFocusable(true);
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1E9/60;
double delta = 0;
long now;
int ticks = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while(running) {
now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
while(delta >= 1) {
tick();
ticks++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer >= 1000) {
timer += 1000;
frame.setTitle(TITLE + " | ups: " + ticks + " fps: " + frames);
ticks = 0;
frames = 0;
}
}
}
private void render() {
BufferStrategy bs = getBufferStrategy(); // retrieves the bufferstrategy from the current component (the instance of Game that calls this method)
if(bs == null) {
createBufferStrategy(3);
return;
}
screen.clear();
screen.render(x, y);
getPixels();
Graphics g = bs.getDrawGraphics(); // retrieves a graphics object from the next in line buffer in the bufferstrategy, this graphics object draws to that buffer
g.drawImage(image, 0, 0, getWidth(), getHeight(), null); // draws the bufferedimage to the available buffer
g.dispose();
bs.show(); // orders the next in line buffer (which the graphics object g is tied to) to show its contents on the canvas
}
private void tick() {
key.update();
if(key.up)
y--;
if(key.down)
y++;
if(key.left)
x--;
if(key.right)
x++;
}
public void initializeFrame() {
frame.setTitle(TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(this);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public synchronized void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void getPixels() {
for(int i = 0; i < pixels.length; i++)
pixels[i] = screen.pixels[i];
}
}
It seems like the bufferedimage gets values from the pixels array. But I don't understand how these two communicate, or how they are connected. I haven't explicitly told the bufferedimage to get its pixels from the pixels array, so how does it know?
I will also attach the Screen class, which is responsible for updating the pixels array.
package com.thomas.game.graphics;
import java.util.Random;
public class Screen {
private int width, height;
public int[] pixels;
private final int MAP_SIZE = 64;
private final int MAP_SIZE_MASK = MAP_SIZE - 1;
private int[] tiles;
private int tileIndex;
private int xx, yy;
private Random r;
public Screen(int w, int h) {
width = w;
height = h;
pixels = new int[width * height];
tiles = new int[MAP_SIZE * MAP_SIZE];
r = new Random(0xffffff);
for(int i = 0; i < tiles.length; i++) {
tiles[i] = r.nextInt();
}
tiles[0] = 0;
}
public void clear() {
for(int i = 0; i < pixels.length; i++)
pixels[i] = 0;
}
public void render(int xOffset, int yOffset) {
for(int y = 0; y < height; y++) {
yy = y + yOffset;
for(int x = 0; x < width; x++) {
xx = x + xOffset;
tileIndex = (yy >> 4 & MAP_SIZE_MASK) * MAP_SIZE + (xx >> 4 & MAP_SIZE_MASK);
pixels[y * width + x] = tiles[tileIndex];
}
}
}
}
I really hope someone can explain this to me, it would be greatly appreciated. The program is working like it is supposed to, but I don't feel comfortable continuing on the tutorial until I grasp this.
Basic types like short, int, long etc are not Objects.
However, int[] is an array. Arrays are objects in java. Java manipulates objects by reference, not value.
In this line you are not creating a new object. You are storing a reference to the object int[] in your variable pixels. Anything you change in pixels, gets changed inside of the int[] object in image:
pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
I've created an example, try running this code:
public class Data {
private int[] values = {25,14};
public int[] getValues() {
return values;
}
public static void main(String[] args) {
Data d = new Data();
System.out.println(d.getValues()[0]);
int[] values = d.getValues();
values[0] = 15;
System.out.println(d.getValues()[0]);
}
}
Output:
25
15
Note that you have this code...
pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
while it should be like this...
pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
Change image to img.
Hope it works!

How do I make the frame rate consistent?

I'm making simple 2D games. I have a game loop, and in the game loop i have an update
method. I make things move by adding 1 to xPos whenever it loops. This means that if you have a slow fps then everything goes in slow motion and if you have a high fps everything moves
really quick.
This is my code:
long fpsTimer;
int frames;
public void run(){
running = true;
fpsTimer = System.nanoTime();
while(running){
render();
update();
try{
Thread.sleep(6);
}catch(InterruptedException e){}
frames++;
if(System.nanoTime() >= fpsTimer+1000000000){
System.out.println(frames+" fps");
frames = 0;
fpsTimer = System.nanoTime();
}
}
}
All Code
import java.awt.*;
import java.awt.image.*;
import javax.swing.JFrame;
import java.awt.event.*;
public class Game extends Canvas implements Runnable{
public static final int WIDTH = 800;
public static final int HEIGHT = 300;
public JFrame f;
private String title = "Untitled Test";
private Image image;
private Sprite player;
public Game(){
player = new Sprite(100, 100);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setMaximumSize(new Dimension(WIDTH, HEIGHT));
setMinimumSize(new Dimension(WIDTH, HEIGHT));
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
player.keyPressed(e.getKeyCode());
}
public void keyReleased(KeyEvent e){
player.keyReleased(e.getKeyCode());
}
});
}
public static void main(String[] args){
Game g = new Game();
g.f = new JFrame(g.title);
g.f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g.f.add(g);
g.f.setResizable(false);
g.f.pack();
g.f.setLocationRelativeTo(null);
Thread gameLoop = new Thread(g);
gameLoop.start();
g.f.setVisible(true);
}
private void render(){
BufferStrategy bs = getBufferStrategy();
if(bs==null){
createBufferStrategy(3);
return;
}
image = createImage(WIDTH, HEIGHT);
Graphics g = image.getGraphics();
player.draw(g);
g.dispose();
Graphics bsg = bs.getDrawGraphics();
bsg.drawImage(image, 0, 0, WIDTH, HEIGHT, null);
bsg.dispose();
bs.show();
}
private void update(){
player.move();
}
long fpsTime;
int frames;
public void run(){
fpsTime = System.nanoTime();
while(true){
render();
update();
try{
Thread.sleep(6);
}catch(InterruptedException e){}
frames++;
if(System.nanoTime() >= fpsTime+1000000000){
System.out.println(frames+" fps");
frames = 0;
fpsTime = System.nanoTime();
}
}
}
}
First of all you should not have a constant sleep time. Instead, the value should be dynamically computed. Maybe its easier to use Timer#scheduleAtFixedRate(...) cause this already takes care of that for you.
Then, 6 ms per iteration seems much too less. 60 frames per second is ideal (if you just have 30, its ok too i think), so 16 ms is enough (or about 32 for 30 fps). (Note that the refresh rate of your screen is the upper limit - it should be about 60 Hz - more doesn't make sense).
Thirdly think about dynamically computing the moves of your objects. Instead of having a constant delta that you add to the coordinates you should better have some kind of 'move function' that calculates the coordinates based on the current time.
Let's say you want to move an object along the x-axis with a constant speed of pps pixels per second. The function for the x coordinate of that object would then be:
x(t) := x0 + (t - t0) * pps / 1000
(t is the time that has passed since start in ms, x0 is the initial x coordinate of the object when it appeared first, t0 is the time at which the object appeared, pps is the number of pixels the object should move per second).
This makes your objects move with the same speed - no matter what framerate you've got.
Note that this approach gets more complex if your objects should react to user input or other events (like collisions etc.).

How to make graphics appear at random and disappear on collision with another graphic?

I have written some code on moving a graphics (that is surrounded by a rectangle), and I am trying to draw another oval (with a rectangle around it) that will generate at random. Right now, it's generating WAY fast, and I don't want to use Thread.sleep; because it will stop listening for the keys (to my knowledge?). So is anyone good with multi-threading that could help me do this or know how to make a graphic appear until it is touched by the movable graphic.
The Graphics Generator in main class:
public void paintComponent(Graphics g){
//System.out.println("X = " + al.x + ", Y = " + al.y);
boolean intersect = false;
int points = 0;
g.drawString("Points: " + points, 5, 445);
Rectangle r1 = new Rectangle(al.x, al.y, 10, 10);
g.setColor(Color.BLACK);
g.fillOval(al.x, al.y, 10, 10);
Random randX = new Random();
Random randY = new Random();
int xInt = randX.nextInt(590);
int yInt = randY.nextInt(440);
Rectangle dCoin = new Rectangle(xInt, yInt, 10, 10);
g.setColor(Color.YELLOW);
g.fillOval(xInt, yInt, 10, 10);
/*
* (???)
*
* for(int idx = 1; idx == 1; idx++){
* if(xInt < 590 && yInt < 440){
* }
* }
*
* Check if graphic collides with another:
*
* if(r1.intersects(r2)){
* doSomething;
* }
*
*/
repaint();
}
}
BTW: r1 surrounds the movable graphic, and r2 is the rectangle surrounding the randomly generated graphic. I had to make invisible rectangles around the ovals to get the r1.intersects(r2) method.
You should use the Swing Timer class to periodically generate ActionEvents on the Event Dispatch Thread. This avoids the problem of the application becoming unresponsive to keystrokes and other user input.
The actionPerformed callback is the hook into your routing to move and repaint the object(s) you wish to animate. Within the animation routine you can record the time ellapsed since the last time the method was called in order to maintain the desired velocity.
Timer timer = new Timer(1000, new ActionListener() {
long lastTime = System.currentTimeMillis();
public void actionPerformed(ActionEvent evt) {
long timeNow = System.currentTimeMillis();
long timeEllapsed = timeNow - lastTime;
lastTime = timeNow;
if (timeEllapsed > 0L) {
for (Moveable mv : moveables) {
mv.updatePosition(timeEllapsed);
}
for (Drawable d : drawables) {
d.repaint();
}
}
}
});

Categories

Resources