Java javax.swing.Timer duration - java

I need to execute N calculations (with graphics updates) in X seconds using javax.swing.Timer.
What's the best way for do this?
If I set a fixed delay for the timer I exceed the X seconds, because the time required for each execution is delay + calculations.
To work around this I've tried to set the delay of the timer dynamically, but still the time is not accurate.
As a last chance I've tried to set the dalay to 1ms and use Thread.sleep(sleepTime) for control the duration and this work perfectly, without affect the animations.
My question is:
Is this a good solution? Can I use Thread.sleep(sleepTime) inside the javax.swing.Timer?
Edit, here's some code to better understand. I want to say that the graphics is needed only to see if the movements are correct, in the final version i only need to update the game and generate a report of the match result.
Main frame with "game loop":
package engine.test;
import engine.entity.Skill;
import engine.math.Point;
import engine.math.Vector;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.Timer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public final class EngineFrame extends JFrame implements ActionListener
{
public static final int SCALE = 6;
public static final int WIDTH = 100 * SCALE;
public static final int HEIGHT = 60 * SCALE;
public static final int DURATION = 2; // animation duration in seconds
public static final int FPS = 60;
public static final int SKIP_TICKS = 1000 / FPS;
private final JSONObject data;
private final ArrayList<Player> players;
private PlayersPanel playersPanel;
public EngineFrame(String title) throws JSONException, FileNotFoundException
{
super(title);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMenu();
data = new JSONObject(loadData());
players = buildPlayers(data.getJSONArray("teams").getJSONObject(0).getJSONArray("players"));
playersPanel = new PlayersPanel(players);
add(playersPanel);
}
public ArrayList<Player> getPlayers()
{
return players;
}
/**
* Returns the data required to run a match.
*
* #return a json string representing the match data
* #throws FileNotFoundException
*/
private String loadData() throws FileNotFoundException
{
Scanner fileInput = new Scanner(new File(
"C:\\Users\\packard bell\\Documents\\JavaProjects\\Engine\\src\\resources\\data.json"
));
String jsonText = "";
while (fileInput.hasNextLine()) {
jsonText += fileInput.nextLine();
}
return jsonText;
}
/**
* Creates and returns the player entities.
*
* #param playersData
* #return
*/
private ArrayList<Player> buildPlayers(JSONArray playersData) throws JSONException
{
ArrayList<Player> players = new ArrayList<Player>();
JSONObject playerData;
Player player;
for (int i = 0, l = playersData.length(); i < l; i++) {
playerData = playersData.getJSONObject(i);
player = new Player.Builder()
.setId(playerData.getInt("id"))
.setFirstName(playerData.getString("first_name"))
.setLastName(playerData.getString("last_name"))
.setMass(playerData.getInt("weight"))
.setSkills(buildSkills(playerData.getJSONObject("skills")))
.setInitialPosition(new Point(0, i * 10 + 20))
.setInitialVelocity(new Vector(0, 0))
.build();
players.add(player);
}
return players;
}
/**
*
*/
private Map<Skill, Double> buildSkills(JSONObject skillsData) throws JSONException
{
Map<Skill, Double> skills = new HashMap();
for (Skill skill : Skill.values()) {
skills.put(
skill,
skill.getMinValue() + (skill.getMaxValue() - skill.getMinValue()) * (skillsData.getDouble(skill.getName()) / 100)
);
}
return skills;
}
/**
*
*/
private void createMenu()
{
JMenu seekMenu = new JMenu("Seek behavior");
JMenuItem initSeek = new JMenuItem("Init seek");
initSeek.addActionListener(this);
JMenuItem runSeek = new JMenuItem("Run seek");
runSeek.addActionListener(this);
JMenuItem stopSeek = new JMenuItem("Stop seek");
stopSeek.addActionListener(this);
seekMenu.add(initSeek);
seekMenu.add(runSeek);
seekMenu.add(stopSeek);
JMenuBar bar = new JMenuBar();
bar.add(seekMenu);
setJMenuBar(bar);
}
public static void main(String[] args) throws JSONException, FileNotFoundException, InterruptedException
{
EngineFrame frame = new EngineFrame("Engine");
}
#Override
public void actionPerformed(ActionEvent e)
{
String menuString = e.getActionCommand();
if (menuString.equalsIgnoreCase("init seek")) {
Player player1 = getPlayers().get(0);
Player player2 = getPlayers().get(1);
player1.setPosition(new Point(0, 20));
player1.setVelocity(new Vector(0, 0));
player2.setPosition(new Point(0, 30));
player2.setVelocity(new Vector(0, 0));
repaint();
}
else if (menuString.equalsIgnoreCase("run seek")) {
Timer t = new Timer(1, new ActionListener() {
private final long start = System.currentTimeMillis();
private long nextGameUpdate = start;
private long sleepTime = 0;
private int loops = DURATION * 1000 / SKIP_TICKS;
#Override
public void actionPerformed(ActionEvent e) {
Player player1 = getPlayers().get(0);
Player player2 = getPlayers().get(1);
//System.out.println("Position: " + player1.getPosition());
//System.out.println("Velocity: " + player1.getVelocity());
System.out.println();
player1.getSteering().seek(new Point(50, 20));
player2.getSteering().seek(new Point(50, 30));
player1.update();
player2.update();
repaint();
nextGameUpdate += SKIP_TICKS;
sleepTime = nextGameUpdate - System.currentTimeMillis();
//System.out.println(nextGameUpdate);
//System.out.println(sleepTime);
loops--;
if (sleepTime >= 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ex) {
Logger.getLogger(EngineFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (loops <= 0) {
((Timer)e.getSource()).stop();
long end = System.currentTimeMillis();
// should be 2000ms (equals to DURATION constant)
System.out.println("Duration: " + (end - start) + "ms");
}
}
});
t.setInitialDelay(0);
t.start();
}
}
// version without swing timer (it works if called in the main method)
private void runSeek() throws InterruptedException
{
Player player1 = getPlayers().get(0);
Player player2 = getPlayers().get(1);
player1.setPosition(new Point(0, 20));
player2.setPosition(new Point(0, 30));
// run
long start = System.currentTimeMillis();
long nextGameUpdate = start;
long sleepTime = 0;
int i = DURATION * 1000 / SKIP_TICKS;
System.out.println("Loop executions: " + i);
int steps = 0;
String stepsCode = "[";
String velocitiesCode = "[";
String positionsCode = "[";
while (i > 0) {
stepsCode += steps + ", ";
velocitiesCode += player1.getVelocity().len() + ", ";
positionsCode += player1.getPosition().toVector().len() + ", ";
System.out.println("Position: " + player1.getPosition());
System.out.println("Velocity: " + player1.getVelocity());
System.out.println();
player1.getSteering().seek(new Point(50, 20));
player2.getSteering().seek(new Point(50, 30));
player1.update();
player2.update();
repaint();
nextGameUpdate += SKIP_TICKS;
sleepTime = nextGameUpdate - System.currentTimeMillis();
steps += sleepTime;
//System.out.println(sleepTime);
if (sleepTime >= 0) {
Thread.sleep(sleepTime);
}
i--;
}
stepsCode = stepsCode.substring(0, stepsCode.length() - 2) + "]";
velocitiesCode = velocitiesCode.substring(0, velocitiesCode.length() - 2) + "]";
positionsCode = positionsCode.substring(0, positionsCode.length() - 2) + "]";
long end = System.currentTimeMillis();
System.out.println("Duration: " + (end - start) + "ms");
System.out.println("Steps:");
System.out.println(stepsCode);
System.out.println("Positions:");
System.out.println(positionsCode);
System.out.println("Velocities:");
System.out.println(velocitiesCode);
}
}
Here's the JPanel that draw the entities:
package engine.test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;
import javax.swing.JPanel;
public class PlayersPanel extends JPanel
{
private ArrayList<Player> players;
public PlayersPanel(ArrayList<Player> players)
{
this.players = players;
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (Player player : players) {
int x = (int) (player.getPosition().x() * EngineFrame.SCALE);
int y = (int) (player.getPosition().y() * EngineFrame.SCALE);
g2.setColor(Color.BLACK);
g2.fillArc(x, y, 18, 18, 0, 360);
g2.setColor(new Color(0x11539f));
g2.fillArc(x + 2, y + 2, 14, 14, 0, 360);
}
}
}

Is this a good solution? Can I use Thread.sleep(sleepTime) inside the javax.swing.Timer?
No, never do this. Don't worry about changing the delay. Instead consider what it is you want to draw and when you want to draw them. You can create a class for the different object to draw, and have then maintain a delayed state that will determine when they should be drawn. If you want to change an object velocity, then increase the number of pixels you increment it's movement.
Here's an example you may find useful, that shows my first point of maintaining a delayed state. You can see the balls are thrown at different times.
Other than that, you should post some code that show's exactly what you're trying to do. Your question is somewhat vague.

Related

Animate image list by group Java Swing

Currently my code animates an image from the list from initial size to final size and continues with the next one until it completes animating all images and all have their final size.
package ui;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Timer;
import utils.SudokuGen;
// TODO: Auto-generated Javadoc
/**
* The Class ButtonPanel.
*/
public class ButtonPanel extends JPanel implements ActionListener{
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The buttons. */
private JButton[] buttons;
private int[] imgsize;
private int index,count;
private Timer timer;
private ImageButton images;
private static final int COUNT = 10;
private static final int INITSIZE = 32;
private static final int COLUMNS = 1;
private static final int ROWS = 9;
/**
* Instantiates a new button panel.
*
* #param puzzle The Sudoku matrix object
* #param images The images object
* #param sPanel The Sudoku JPanel
*/
public ButtonPanel(SudokuGen puzzle, ImageButton images, SudokuPanel sPanel){
//Create an array of JButton with 9 elements
this.buttons = new JButton[puzzle.getMyboard().getNewnumbers().size()];
this.images = images;
this.imgsize = new int[COUNT];
for (int i=0;i<COUNT;i++){
imgsize[i] = INITSIZE;
}
//Sets the layout to a 9*1 GridLayout with padding 5
this.setLayout(new GridLayout(ROWS,COLUMNS,5,5));
//Load an image, create the MouseListener and add it at index i
for (int i=1;i<this.images.getImagelist().size();i++){
ImageIcon image = new ImageIcon(this.images.getImagelist()
.get(i).getImage().getScaledInstance(32, 32, Image.SCALE_SMOOTH));
buttons[i] = createMouseListener(image, i, puzzle, sPanel);
buttons[i].setPreferredSize(new Dimension(100, 100));
this.add(buttons[i]);
}
index = 1; //first button
count = 0; //times to resize the button
timer = new Timer(5,this);
timer.start();
}
public void set(int X){
this.imgsize[index] = X;
}
/**
* Creates the mouse listener.
*
* #param image The image at index i
* #param i The index for the button i
* #param puzzle The Sudoku matrix object
* #param sPanel The Sudoku JPanel
* #return the JButton to add at index i
*/
private JButton createMouseListener(ImageIcon image, int i, SudokuGen puzzle, SudokuPanel sPanel){
JButton button = new JButton();
button.setIcon(image);
button.setActionCommand(Integer.toString(puzzle.getMyboard().getNewnumbers().get(i)));
button.addActionListener(sPanel.new ButtonActionListener());
return button;
}
#Override
public void actionPerformed(ActionEvent e) {
count++;
set(this.imgsize[index]+1); //increase the size of the image
if (this.imgsize[index] < 64){
ImageIcon image = new ImageIcon(this.images.getImagelist()
.get(index).getImage().getScaledInstance(this.imgsize[index], this.imgsize[index], Image.SCALE_SMOOTH));
buttons[index].setIcon(image);
if (count > 24){ //reached final size
count = 0; //start counting again
index++; //move to the next image
}
};
if (index == 10) timer.stop();
repaint();
}
}
I'm not able to provide an SSCCE given the amount of resources needed for it to work so apologizes for that.
My goal is to start animating the next image when the first animation finished half its process. So when the second image reaches its half animation the first one completes its full animation. Kind of a wave effect.
If there something else I could improve, I'm glad to hear suggestions.
Update: Updated method used to achieve the effect:
public void actionPerformed(ActionEvent e) {
count++;
this.imgsize[index]++;
ImageIcon image = new ImageIcon(this.images.getImagelist()
.get(index).getImage().getScaledInstance(this.imgsize[index], this.imgsize[index], Image.SCALE_SMOOTH));
buttons[index].setIcon(image);
if (count > 24){
count = 0;
index++;
}
if (count > 12 && index < 9)
{
this.imgsize[index+1]++;
image = new ImageIcon(this.images.getImagelist()
.get(index+1).getImage().getScaledInstance(this.imgsize[index+1], this.imgsize[index+1], Image.SCALE_SMOOTH));
buttons[index+1].setIcon(image);
}
if (index == 10) timer.stop();
repaint();
}
This is a somewhat overly simplified idea...
Basically, it sets up a normalised timeline (0-1) where events can occur along it, starting and ending at specified points. During these "periods" some "action" can occur.
In your case, these actions are image scaling actions, the scale of which is a proportion of the amount of time that a given event has been running, which is a proportion of the overall time line.
This all sounds wonderfully confusing, but the benefit of which is, you can change the duration of the timeline and the rest of the code will scale automatically
For example, you could take...
timeline = new Timeline(events, 10000);
which generates a timeline to run over 10 seconds and change it to
timeline = new Timeline(events, 5000);
which will generate a timeline to run over 5 seconds and you've not had to change a single line of code to make it work.
I've also spend some time working out how to automatically generate timeline events from a series of images, so you can supply n number of images, and it will generate the required events so that they overlap each other accordingly
Now, you're probably thinking that this is all overly complicated, but the point is, it's all variable, the duration over which the images are scaled, the number of images, it's all variable and you don't need to do much to add/remove images or change the duration
Cavert- I've used getScaledInstance for this example, it's neither fast nor does it generate a quality image
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
public class TestPane extends JPanel {
private Timeline timeline;
private Map<Object, Image> mapImages = new HashMap<>(5);
private List<Object> imageOrder = new ArrayList<>(5);
public TestPane() throws IOException {
BufferedImage[] images = new BufferedImage[]{
ImageIO.read(new File("...")),
ImageIO.read(new File("...")),
ImageIO.read(new File("...")),
ImageIO.read(new File("..."))
};
List<TimelineEvent> events = generateEvents(images, new ImageScaledObserver() {
#Override
public void imageScaled(Object id, Image image) {
if (!imageOrder.contains(id)) {
imageOrder.add(id);
}
mapImages.put(id, image);
repaint();
}
});
timeline = new Timeline(events, 10000);
Timer timer = new Timer(5, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (timeline.update()) {
((Timer) e.getSource()).stop();
}
}
});
timer.setInitialDelay(5000);
timer.start();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Object id : imageOrder) {
Image image = mapImages.get(id);
int x = (getWidth() - image.getWidth(this)) / 2;
int y = (getHeight() - image.getHeight(this)) / 2;
g2d.drawImage(image, x, y, this);
}
g2d.dispose();
}
}
protected List<TimelineEvent> generateEvents(BufferedImage[] images, ImageScaledObserver observer) {
double length = 1.0 / (double) (images.length);
double overlap = length * 0.5;
List<TimelineEvent> events = new ArrayList<>(images.length);
double startAt = 0.0;
for (BufferedImage image : images) {
double endAt = Math.min(startAt + length + (overlap / 2.0), 1.0);
events.add(new ScaleImageTimelineEvent(image, observer, startAt, endAt));
startAt = Math.min(endAt - (overlap / 2.0), 1.0);
}
return events;
}
public interface TimelineEvent {
public double[] range();
public boolean isWithin(double timelineProgression);
public void performAction(double timelineProgression);
}
public abstract class AbstractTimelineEvent implements TimelineEvent {
private double from, to;
public AbstractTimelineEvent(double from, double to) {
this.from = from;
this.to = to;
}
#Override
public double[] range() {
return new double[]{from, to};
}
#Override
public boolean isWithin(double timelineProgression) {
boolean within = timelineProgression >= from && timelineProgression <= to;
return within;
}
protected double localisedProgression(double timelineProgression) {
double max = from - to;
double value = timelineProgression - to;
double weight = value / max;
return 1d - weight;
}
}
public interface ImageScaledObserver {
public void imageScaled(Object id, Image img);
}
public class ScaleImageTimelineEvent extends AbstractTimelineEvent {
private BufferedImage original;
private ImageScaledObserver observer;
private UUID id;
public ScaleImageTimelineEvent(BufferedImage image, ImageScaledObserver observer, double from, double to) {
super(from, to);
this.original = image;
this.observer = observer;
this.id = UUID.randomUUID();
}
#Override
public void performAction(double timelineProgression) {
double progress = localisedProgression(timelineProgression);
Image image = null;
if (progress < 1.0) {
int width = (int) (original.getWidth() * progress);
if (width > 0) {
image = original.getScaledInstance((int) (original.getWidth() * progress), -1, Image.SCALE_FAST);
}
} else {
image = original;
}
if (image != null) {
observer.imageScaled(id, image);
}
}
}
public static class Timeline {
private List<TimelineEvent> events;
private Long startTime;
private long duration;
public Timeline(List<TimelineEvent> events, long duration) {
this.events = events;
this.duration = duration;
}
public List<TimelineEvent> getEvents() {
return events;
}
public Long getStartTime() {
return startTime;
}
public long getDuration() {
return duration;
}
public void start() {
if (startTime == null) {
startTime = System.nanoTime();
}
}
public void stop() {
startTime = null;
}
public boolean update() {
if (startTime == null) {
start();
}
boolean completed = false;
long currentTime = System.nanoTime();
long diff = currentTime - getStartTime();
long nanoDuration = TimeUnit.NANOSECONDS.convert(getDuration(), TimeUnit.MILLISECONDS);
double progress = diff / (double) nanoDuration;
if (progress > 1.0d) {
progress = 1.0d;
completed = true;
stop();
}
for (TimelineEvent evt : getEvents()) {
if (evt.isWithin(progress)) {
evt.performAction(progress);
}
}
return completed;
}
}
}
I'd also suggest having a look at some of the animation frameworks which are available, which provide additional functionality, like easement. Maybe do a search for "Timing Framework" as a suggestion
The example uses a UUID to generate a unique identifier for the imageScaled event. I wouldn't be hard to make ScaleImageTimelineEvent take an identifier of your own which is linked back to a specific image, allowing you to determine which image was scaled and perform more accurate updates

How can I display Time to my game

I have a falling square game that I am currently making. I need to have a score system, but I don't know how to integrate a and display that onto the game frame.
My question is how can I make a timer that counts upwards and display that in real time onto the frame of my game.
My code:
package GamePackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import javax.swing.ImageIcon;
public class Game extends JPanel {
//changing these values will change the size of the game, while still remaining functional
//within the size limit specified.
public static final int WINDOW_WIDTH = 875;
public static final int WINDOW_HEIGHT = 720;
private Timer countdownTimer;
private javax.swing.JLabel lblTimer;
private String Highscore = "";
private int count;
int Health = 1;
//Creates a Square object Array
Square[] squareArray = new Square[20];
//Triangle[] TriangleArray = new Triangle[10];
Player thePlayer = new Player();
public Game() {
//initializes square objects
for (int i = 0; i < squareArray.length; i++)
squareArray[i] = new Square();
}
public static void music(){
try{
AudioStream Music = new AudioStream (new FileInputStream("/SplashDemoPackage/URF.wav"));
AudioPlayer.player.start(Music);
}catch (IOException error){}
}
private void StartGame(){
count = 14;
countdownTimer = new Timer(1000,new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblTimer.setText(Integer.toString(count));
count = count -1;
if(count<0){
lblTimer.setText("0");
}
}
});
countdownTimer.start();
}
public void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
//paints square objects to the screen
for (Square aSquareArray : squareArray) {
aSquareArray.paint(graphics);
}
// for (Triangle aTriangleArray : TriangleArray) {
// aTriangleArray.paint(graphics);
// }
thePlayer.paint(graphics);
// if(Highscore.equals("")){
// Highscore = this.GetHighScore();
// }
}
// public void DrawScore(Graphics g){
// g.drawString("Score: " + score, 0, B);
// g.drawString("HighScore: " + HighScore, 0,);
// }
public void update() {
// calls the Square class update method on the square objects
if(Health > 0){
for (Square aSquareArray : squareArray) aSquareArray.update();
}
// if(Health > 0){
// for (Triangle aTriangleArray : TriangleArray) aTriangleArray.update();
// }
}
private void mouseMove(MouseEvent evt) {
if(Health > 0){
thePlayer.PlayerMove(evt);
}
}
public void collision(){
Rectangle rectangle1 = thePlayer.bounds();//player rectangle
for (Square square: squareArray) {
if(square.GetBounds().intersects(rectangle1)){//testing all squares vs player
Health = Health - 1;
System.out.println("HIT");
if (Health == 0){
System.out.println("LOST");
}
}
}
// for (Triangle triangle: TriangleArray) {
// if(triangle.GetBounds().intersects(rectangle1)){//testing all triangles vs player
// Health = Health - 1;
// System.out.println("HIT");
// if (Health == 0){
// System.out.println("LOST");
// }
// }
// }
}
private static void GUI() throws InterruptedException {
Game game = new Game();
Player thePlayer = new Player();
JLabel lblStart = new JLabel();
JFrame frame = new JFrame();
frame.add(game);
frame.setVisible(true);
frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("Dodge The Squares");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
// Create a new blank cursor.
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
cursorImg, new Point(0, 0), "blank cursor");
// Set the blank cursor to the JFrame.
frame.getContentPane().setCursor(blankCursor);
frame.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
mouseMove(evt);
}
private void mouseMove(MouseEvent evt){
game.mouseMove(evt);
}
});
while (true) {
game.update();
game.repaint();
game.collision();
Thread.sleep(10);
}
}
public void DrawScore(Graphics g) throws InterruptedException{
while (true) {
int time = 0;
time = time + 1;
g.drawString("Score: " + time, 0, WINDOW_WIDTH * WINDOW_HEIGHT + 10);
Thread.sleep(1000);
}
}
public static void main(String[] args) throws InterruptedException {
new Scoreboards().setVisible(true);
music();
GUI();
}
}
For game usages I would suggest System.currentTimeMillis() because it is better performance wise than System.nanoTime(). An example of how you could track the time in a game:
public class Stopwatch {
private long startTime;
public void start() {
startTime = System.currentTimeMillis();
}
public float getElapsedTimeSeconds() {
return (System.currentTimeMillis() - startTime) / 1000f;
}
}
Usage:
Stopwatch stopwatch = new Stopwatch();
stopwatch.start();
Thread.sleep(5000);
System.out.println("Time elapsed in seconds: " + stopwatch.getElapsedTimeSeconds());

Finished project, how to add a background in NetBeans

I am using NetBeans IDE 8.2 and I created a simple clone game from tutorials.
I am looking to add a background to the app and my research keeps pointing to using JFrame Forms and a JLabel.
None of the tutorials touched on backgrounds or JFrame Forms/JLabels. So I am uncertain how to take my completed project and add a background. I have attempted to reproduce JFrame Forms and JLabel code only to be unable to put my classes/interfaces "inside?" or "on top?" of the JFrame Form/JLabel.
I apologize if this really isn't an ideal first question, I just signed up and this is my first dip into the Java pool. Game class with JFrame (not Form) settings
EDIT: Adding full paste of my Game.java class.
package game;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.*;
public class Game {
public final static int WIDTH = 1920, HEIGHT = 1080;
private String gameName = "Tutorial Game";
private Canvas game = new Canvas();
private Input input;
private ArrayList<Updatable> updatables = new ArrayList<>();
private ArrayList<Renderable> renderables = new ArrayList<>();
// Helper methods for update/render Arrays
public void addUpdatable(Updatable u) {
updatables.add(u);
}
public void removeUpdatable(Updatable u) {
updatables.remove(u);
}
public void addRenderable(Renderable r) {
renderables.add(r);
}
public void removeRenderable(Renderable r) {
renderables.remove(r);
}
public void start() {
// Initialize windows
Dimension gameSize = new Dimension(Game.WIDTH, Game.HEIGHT);
JFrame gameWindow = new JFrame(gameName);
gameWindow.setDefaultCloseOperation(3);
gameWindow.setSize(gameSize);
gameWindow.setResizable(false);
gameWindow.setLocationRelativeTo(null);
gameWindow.add(game);
game.setSize(gameSize);
game.setMinimumSize(gameSize);
game.setMaximumSize(gameSize);
game.setPreferredSize(gameSize);
gameWindow.setVisible(true);
// Initialize Input
input = new Input();
game.addKeyListener(input);
// Game loop
final int TICKS_PER_SECOND = 60;
final int TIME_PER_TICK = 1000 / TICKS_PER_SECOND;
final int MAX_FRAMESKIPS = 5;
long nextGameTick = System.currentTimeMillis();
int loops;
float interpolation;
long timeAtLastFPSCheck = 0;
int ticks = 0;
boolean running = true;
while(running) {
// Updating
loops = 0;
while(System.currentTimeMillis() > nextGameTick && loops < MAX_FRAMESKIPS) {
update();
ticks++;
nextGameTick += TIME_PER_TICK;
loops++;
}
// Rendering
interpolation = (float) (System.currentTimeMillis() + TIME_PER_TICK - nextGameTick)
/ (float) TIME_PER_TICK;
render(interpolation);
// FPS Check
if(System.currentTimeMillis() - timeAtLastFPSCheck >= 1000) {
System.out.println("FPS: " + ticks);
gameWindow.setTitle(gameName + " - FPS: " + ticks);
ticks = 0;
timeAtLastFPSCheck = System.currentTimeMillis();
}
}
}
private void update() {
for(Updatable u : updatables) {
u.update(input);
}
}
private void render(float interpolation) {
BufferStrategy b = game.getBufferStrategy();
if(b == null) {
game.createBufferStrategy(2);
return;
}
Graphics2D g = (Graphics2D) b.getDrawGraphics();
g.clearRect(0, 0, game.getWidth(), game.getHeight());
for(Renderable r : renderables) {
r.render(g, interpolation);
}
g.dispose();
b.show();
}
}

Drawing with timer is not working

I have problem with drawing in Java. I think my code is right, but when i place it to loop with timer, it will not print anything. I want to do every second repeat the doDrawing(g) method. Now it will only prints the text prom system.out but no drawing.
package src;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JPanel;
import javax.swing.Timer;
public class surface extends JPanel{
private void doDrawing(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
//souradnice stredu ciferniku
final int sx = 250;
final int sy = 250;
// inicializace promennych
int uhel = 0;
int delka = 150;
int xHodina,xMinuta,xSekunda,
yHodina,yMinuta,ySekunda;
// získání aktuálního času
int HOUR = Calendar.getInstance().get(Calendar.HOUR);
int MINUTE = Calendar.getInstance().get(Calendar.MINUTE) + 1;
int SECOND = Calendar.getInstance().get(Calendar.SECOND) + 1 ;
//výpočet jednotlivých úhlů pro jednotlivé ručičky a jejich vykreslení
xSekunda = (int) ((int) sx + Math.round( Math.sin(( 6 * SECOND * Math.PI / 180)) * delka));
ySekunda = (int) ((int) sy - Math.round( Math.cos(( 6 * SECOND * Math.PI / 180)) * delka));
//vyhreslení ručiček
g2d.drawLine(sx, sy, xSekunda, ySekunda);
//info
System.out.print(" "+(6 * SECOND)+ " "+ HOUR+" "+MINUTE+" "+SECOND+" "+xSekunda+" "+ySekunda+"\n");
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
int delay = 1000; //milliseconds
//Somewhere there is that problem ..
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
doDrawing(g);
}
};
new Timer(delay, taskPerformer).start();
}
}
The problem is somewhere here.
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
int delay = 1000; //milliseconds
//Somewhere there is that problem ..
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
doDrawing(g);
}
};
new Timer(delay, taskPerformer).start();
}
}
Never start a Timer inside of paintComponent. This method is for drawing and drawing only and nothing, I mean absolutely nothing else.
You should start your Timer elsewhere, perhaps in the class's constructor, and have it change fields of your class, and then call repaint(). The paintComponent method should then use those fields to decide what and where to paint.
For example your doDrawing() method (without the Graphics parameter) could create a Line2D, add this to a List<Line2D>, and then call repaint(). The paintComponent(...) method could then iterate through this list drawing each line:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.Timer;
public class surface extends JPanel {
private List<Line2D> lineList = new ArrayList<>();
public surface() {
int delay = 1000; // milliseconds
// Somewhere there is that problem ..
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
doDrawing();
}
};
new Timer(delay, taskPerformer).start();
}
private void doDrawing() {
// souradnice stredu ciferniku
final int sx = 250;
final int sy = 250;
// inicializace promennych
int uhel = 0;
int delka = 150;
int xHodina, xMinuta, xSekunda, yHodina, yMinuta, ySekunda;
// získání aktuálního času
int HOUR = Calendar.getInstance().get(Calendar.HOUR);
int MINUTE = Calendar.getInstance().get(Calendar.MINUTE) + 1;
int SECOND = Calendar.getInstance().get(Calendar.SECOND) + 1;
// výpočet jednotlivých úhlů pro jednotlivé ručičky a jejich vykreslení
xSekunda = (int) ((int) sx + Math.round(Math
.sin((6 * SECOND * Math.PI / 180)) * delka));
ySekunda = (int) ((int) sy - Math.round(Math
.cos((6 * SECOND * Math.PI / 180)) * delka));
Line2D line = new Line2D.Double(sx, sy, xSekunda, ySekunda);
lineList.add(line);
repaint();
// info
System.out.print(" " + (6 * SECOND) + " " + HOUR + " " + MINUTE + " "
+ SECOND + " " + xSekunda + " " + ySekunda + "\n");
}
#Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
for (Line2D line2d : lineList) {
g2d.draw(line2d);
}
}
}
Edit my bad, you only want to draw one line with this code, not a List of lines. If so, get rid of the list and instead create a Line2D field that is changed by your Timer and drawn by your paintComponent:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Line2D.Double;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
// !! Class names should begin with upper-case letter
public class Surface extends JPanel {
private static final double DELKA = 150;
private static final int SX = 250;
private static final int SY = SX;
private static final int DELAY = 1000;
// private List<Line2D> lineList = new ArrayList<>();
private Timer timer = new Timer(DELAY, new TaskPerformer());
private Line2D line;
public Surface() {
timer.start();
}
private void doDrawing() {
int xSekunda, ySekunda;
int HOUR = Calendar.getInstance().get(Calendar.HOUR);
int MINUTE = Calendar.getInstance().get(Calendar.MINUTE) + 1;
int SECOND = Calendar.getInstance().get(Calendar.SECOND) + 1;
xSekunda = (int) ((int) SX + Math.round(Math
.sin((6 * SECOND * Math.PI / 180)) * DELKA));
ySekunda = (int) ((int) SY - Math.round(Math
.cos((6 * SECOND * Math.PI / 180)) * DELKA));
// Line2D line = new Line2D.Double(SX, SY, xSekunda, ySekunda);
line = new Line2D.Double(SX, SY, xSekunda, ySekunda);
// lineList.add(line);
repaint();
// info
System.out.print(" " + (6 * SECOND) + " " + HOUR + " " + MINUTE + " "
+ SECOND + " " + xSekunda + " " + ySekunda + "\n");
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(2 * SX, 2 * SY);
}
#Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
// to give smoother lines
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// iterate through our list and draw lines it holds
if (line != null) {
g2d.draw(line);
}
}
private class TaskPerformer implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (!isShowing() && timer != null && timer.isRunning()) {
timer.stop();
} else {
doDrawing();
}
}
}
private static void createAndShowGui() {
Surface mainPanel = new Surface();
JFrame frame = new JFrame("surface");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

My first tries on a java game end me up with 7 fps when I read images?

Lately I've been trying to build a little game (2d, nothing big) off the knowledge I acquired in my CS class. After reading documentations of those Graphic-related classes for like 2 weeks, I ended up with this situation:
I have a running system that operates at 60 game logic updates/60 frames per second (that one works quite nicely :D). As a first little test, I wanted to make a image move on the screen. Thats the code (partly mine, partly from some tutorials):
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final String NAME= "PokeCraft PRE-ALPHA";
public static final int HEIGHT=720;
public static final int WIDTH=HEIGHT*16/9;
public static final int SCALE=1;
private int fps=0;
private int tps=0;
private boolean running;
private int tickCount;
public void start(){
running = true;
new Thread(this).start();
}
public void stop(){
running = false;
}
public void render(){
BufferStrategy bufferStrategy =getBufferStrategy();
if(bufferStrategy==null){
this.createBufferStrategy(3);
return;
}
/* render function */
Graphics g = (Graphics) bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, super.getWidth(), super.getHeight());
Image img = null;
try{
String imgPath = "data/MF.png";
img = ImageIO.read(getClass().getResourceAsStream(imgPath));
} catch(Exception e){
System.out.println(e);
}
g.drawImage(img, tickCount, 0, null);
Font font = new Font("Verdana",0,11);
g.setFont(font);
g.setColor(Color.RED);
g.drawString(NAME+" / "+fps+" fps, "+tps+"tps", 5, 15);
g.dispose();
bufferStrategy.show();
}
public void run() {
long lastTime= System.nanoTime();
double unprocessed = 0;
double nsPerTick = 1000000000.0/60.0;
int frames = 0;
int ticks = 0;
long lastTimer1 = System.currentTimeMillis();
while(running){
long now = System.nanoTime();
unprocessed += (now-lastTime)/nsPerTick;
lastTime= now;
boolean shouldRender= false;
while(unprocessed >= 1){
ticks++;
tick();
unprocessed -= 1;
shouldRender = true;
}
if(shouldRender){
frames++;
render();
}
if(System.currentTimeMillis()-lastTimer1 > 1000){
lastTimer1 += 1000;
System.out.println(ticks+" ticks, "+frames + " fps");
fps=frames;
tps=ticks;
ticks = 0;
frames = 0;
}
}
}
public void tick(){
tickCount++;
}
public static void main(String[] args){
Game game= new Game();
game.setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
game.setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
game.setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
JFrame frame = new JFrame(Game.NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(game);
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
}
The ImageIO.read(...) is really hitting the performance really hard (according to VisualVM, it takes ~200ms/run). How can I tackle that problem?
Reading an image is an inherently costly operation.
Therefore, you should read the image once, when you start the game, and hold it in memory to access later.
Avoid loading the image each time it is rendered. Make it a class variable and load it only once. Like this:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final String NAME = "PokeCraft PRE-ALPHA";
public static final int HEIGHT = 720;
public static final int WIDTH = HEIGHT * 16 / 9;
public static final int SCALE = 1;
private int fps = 0;
private int tps = 0;
private Image img = null;
private boolean running;
private int tickCount;
public void start() {
running = true;
new Thread(this).start();
}
public void stop() {
running = false;
}
public void render() {
BufferStrategy bufferStrategy = getBufferStrategy();
if (bufferStrategy == null) {
this.createBufferStrategy(3);
return;
}
/* render function */
Graphics g = (Graphics) bufferStrategy.getDrawGraphics();
g.clearRect(0, 0, super.getWidth(), super.getHeight());
if (img == null) {
try {
String imgPath = "data/MF.png";
img = ImageIO.read(getClass().getResourceAsStream(imgPath));
} catch (Exception e) {
System.out.println(e);
}
}
g.drawImage(img, tickCount, 0, null);
Font font = new Font("Verdana", 0, 11);
g.setFont(font);
g.setColor(Color.RED);
g.drawString(NAME + " / " + fps + " fps, " + tps + "tps", 5, 15);
g.dispose();
bufferStrategy.show();
}
public void run() {
long lastTime = System.nanoTime();
double unprocessed = 0;
double nsPerTick = 1000000000.0 / 60.0;
int frames = 0;
int ticks = 0;
long lastTimer1 = System.currentTimeMillis();
while (running) {
long now = System.nanoTime();
unprocessed += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = false;
while (unprocessed >= 1) {
ticks++;
tick();
unprocessed -= 1;
shouldRender = true;
}
if (shouldRender) {
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer1 > 1000) {
lastTimer1 += 1000;
System.out.println(ticks + " ticks, " + frames + " fps");
fps = frames;
tps = ticks;
ticks = 0;
frames = 0;
}
}
}
public void tick() {
tickCount++;
}
public static void main(String[] args) {
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
JFrame frame = new JFrame(Game.NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(game);
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
}
Same goes for the font, although that may not be impacting performance as much as the image loading.
Disk operations are incredibly slow, and there's no need to access the file each loop, which is what you're currently doing. Make your img variable a class variable and instantiate it before entering the while(running) loop in your run() method.

Categories

Resources