Executing and Displaying Graphics Every Second Rather Than At Once - java

I'm working on a simulation for the growth of an organism, using jLabels for the organisms. However, when I implement a for loop and a timer to try and show it moving, it freezes and then displays the final position of the label rather than showing it move. Could anyone explain to me why this is happening?
public class TestView extends FrameView {
public TestView(SingleFrameApplication app) {
super(app);
initComponents();
picture = new JLabel();
picture.setIcon(new ImageIcon(System.getProperty("user.dir") +
File.separator + "mouse.gif"));
picture.setBounds(0, 0, 100, 100);
mainPanel.add(picture);
for (int x = 0; x < 20; x++) {
move();
wait(50);
}
}
public static void wait(int n) {
long t0, t1;
t0 = System.currentTimeMillis();
do {
t1 = System.currentTimeMillis();
} while (t1 - t0 < n);
}
public static void move() {
picture.setBounds(picture.getX() + 5, picture.getY(), 100, 100);
}

You might like this example of diffusion limited aggregation and other simulations.

Related

How would I be able to create an enemy that shoots at the player after a certain amount of time in libgdx?

I am currently working on trying to make enemies shoot a projectile in a straight line at the player. The projectiles are not showing on the playstate.
public class Ghost {
private Texture topGhost, bottomGhost;
private Vector2 postopGhost;
private Vector2 posBotGhost;
private Random rand;
private static final int fluct = 130;
private int GhostGap;
public int lowopening;
public static int width;
private Texture bullet;
private Vector2 bulletpos;
private Vector2 botbulletpos;
public Ghost(float x) {
GhostGap = 120; // the gap between the top and bottom ghost
lowopening = 90; //
bullet = new Texture("Bird.png");
topGhost = new Texture("Bird.png");
// middletude = new Texture("sipkemiddle.png"); //spelling mistake
bottomGhost = new Texture("Bird.png");
rand = new Random();
width = topGhost.getWidth();
posBotGhost = new Vector2(x + 120, rand.nextInt(fluct));
postopGhost = new Vector2(x + 113, posBotGhost.y + bottomGhost.getHeight() + GhostGap - 50);
bulletpos = new Vector2(postopGhost);
botbulletpos = new Vector2(posBotGhost);
}
public void repostition(float x) {
postopGhost.set(x + 75, rand.nextInt(fluct) + 200);
posBotGhost.set(x + 75, postopGhost.y + GhostGap - bottomGhost.getHeight() - 247);
}
public void timer(float dt) {
int ticker = 0;
ticker += dt;
if(ticker > 5) {
ticker = 0;
shoot();
}
}
public void shoot(){
setBulletpos(postopGhost);
bulletpos.x = (bulletpos.x + 40);
bulletpos. y = bulletpos.y;
}
So far I had no luck with spawning bullets visually that move across the X-axis of my game. Any suggestions?
You are using timer to determine when the bullet is shot i.e the shot begins, but also in there is the continuing increment for the flight of the bullet. So whenever your timer triggers from delta, the bullet resets position to postopGhost.
i.e. the bullet doesn't have any method to proceed during flight. Try this maybe. Also you need to refer to dt (in some fashion as you like) against the 40 increment because you don't know how much time has elapsed since the last render.
public void timer(float dt) {
int ticker = 0;
ticker += dt;
if(ticker > 5) {
ticker = 0;
shoot();
} else {
processBulletFlight(dt);
}
}
public void shoot(){
setBulletpos(postopGhost);
}
public processBulletFlight(dt) {
bulletpos.x = (bulletpos.x + (40*dt));
}

Decreasing an int from 10 to 0 every second in Java

I define an
int x = 10;
Now I want x to decrease every second until its 0:
if (Obstacle.activeItem == true) {
game.font.draw(game.batch, "Item active for: " + x, 100, 680);
}
How can I do that?
I've seen people do similar things using the Class Timer, but I don't know how it should look like for this case.
I tried
int x = 10;
ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
And then
if (Obstacle.activeItem == true) {
game.font.draw(game.batch, "Item active for: " + x, 100, 680);
}
execService.scheduleAtFixedRate(new Runnable() {
public void run() {
x--;
}
}, 0L, 10L, TimeUnit.SECONDS);
But that doesn't work as I want it to.
You tagged your question with libGdx so I think you work with libgdx.
Instead of creating a extra ExecutorService why you don't use the update(float delta) method to decrease your timer?
private float timer = 10;
#Override
public void render(float delta) {
timer -= delta;
if (Obstacle.activeItem == true) {
font.draw(batch, "Item active for: " + (int)timer, 100, 680);
}
}
Here's a sample of how you can implement a Timer type functionality using Executors
public class Main {
static int x = 10;
public static void main(String[] args) {
ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
execService.scheduleAtFixedRate(() -> {
System.out.println(x);
x--;
if (x == 0)
execService.shutdownNow();
}, 1L, 1L, TimeUnit.SECONDS); //initial delay, period, time unit
}
}
It is highly recommended that you read up on Executors. Consider this as a hint and implement it in your use case accordingly.
if that is libGdx i have some working sphagetti code for you:
int reducedInt = 10;
bool isReduce = false;
float timer = 1f;
in Render
timer -= delta;
if(timer<=0){
isReduce = true;
timer = 1;
}
if(isReduce){
reducedInt--;
isReduce = false;
}
This is classic LibGDX sphagetti timer code. Since you have tagged it as LibGDX.

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.

JFrame.repaint() stops getting called in a Java Canvas

EDIT: Problem solved, turns out I needed to call main.repaint() instead of frame.repaint()
I am using a class that extends Canvas and overrides the 'paint(Graphics graphics)' method. In a loop which activates 60 times a second (it works like it's supposed to), I have called frame.repaint() (the canvas is correctly added to the frame). The paint method gets called about 4 or 5 times, then stops getting called. My other method in the loop, does not stop however, proving that it's the frame.repaint() method.
To make the problem clear, the JFrame.repaint() method stops getting called after 4 or 5 attempts within a second.
To prove this, I've increased an integer every second in my update method (which is getting called 60 times per second) and I'm using that as the x cordanite as a rectangle in my frame, which should make the rectangle larger each second. The rectangle paint's for 2 seconds or so, then stops growing, however the integer is still increasing. One thing to keep in mind that the rectangle does draw for the first few times, indicating that it's some sort of issue with the frame.
Is there a better way to call the paint(Graphics graphics) method? Do
I have some flaw in my code?
Sorry if my explanation was confusing, but I attached the code below (and in a pastebin file that you can find here: http://pastebin.com/WNnK54gq)
I have been looking for the past few hour's, and haven't found any replacement for the frame.repant() method.
Thanks in advanced!
public class Main extends Canvas {
//Static Variables
public static Main main;
public static String name = "Game";
public static double version = 1.0;
public static int FPS;
//Object Variables
private JFrame frame;
private boolean running;
private int screenX;
private int screenY;
private int x = 0;
//Constructor
public Main() {
setSize(new Dimension(500, 500));
}
//Main Method
public static void main(String[] args) {
main = new Main();
main.init();
}
//Object Methods
private void init() {
frame = new JFrame(name);
frame.setSize(500, 500);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(main);
loop();
}
public void loop() {
running = true;
int fps = 0;
long timer = System.currentTimeMillis();
long lastTime = System.nanoTime();
final double ns = 1000000000.0 / 60;
double delta = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
if (fps <= 60) {
fps++;
update();
frame.repaint();
delta--;
}
}
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
log("Running at " + fps + " FPS and UPS");
FPS = fps;
fps = 0;
}
}
}
public void update() {
screenX = frame.getWidth();
screenY = frame.getHeight();
x++;
if (x >= 500) x = 0;
log("update");
//update gametstate
}
public void log(String string) {
System.out.println("[" + name + "] [" + version + "] " + string);
}
public void log() {
System.out.println("[" + name + "] [" + version + "]");
}
#Override
public void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, screenX, screenY);
//update gamestate
graphics.setColor(Color.BLUE);
graphics.fillRect(0, 200, x, 300);
log("rendered");
}
while (delta >= 1) {
if (fps <= 60) {
fps++;
update();
frame.repaint();
delta--;
}
}
Once fps hits 61, it will stop rendering or updating, because you never set fps back to 0.
while (delta >= 1) {
if (fps <= 60) {
fps++;
update();
frame.repaint();
delta--;
if(fps == 60) fps = 0;
}
}
You need to set fps back to 0.

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!

Categories

Resources