Draw jpanel at double position - java

I have a jpanel that I repaint in a step event. It takes a cars position x and draws it. This works with integers but I'd like to use a double. The car objects x and y are needed to be doubles because I want to rotate and accelerate in a direction. Any ideas? Here's my current code:
public class Map extends JPanel implements ActionListener {
private void step() {
for(int i = 0; i <cars.length; i++) {
Car car = cars[i];
car.move();
repaint(car.getX()-1, car.getY()-1, car.getWidth()+2, car.getHeight()+2);
}
}
}

One possibility is for you to use doubles in your program's model, the classes that describe the state of your system, and use ints (by casting or rounding) in the view -- your GUI graphics code -- when displaying the state of the model.
Another is for you to consider using classes that implement the Shape interface, such as Rectangle2D, Ellipse2D, and Path2D as these use double or float specifiers for positioning
Your current code is broken in that the code is called on the Swing event thread, but appears to attempt to animate within a for loop. That's not going to work, and will only show the starting and finishing state of the animation due to it blocking the Swing event thread. Instead use a Swing Timer to drive the animation.

Related

Timer Delay Problems Java

I am trying to make a snake type game and I am having trouble making the "pellets" appear at random places at fixed intervals (I want to make it 10 seconds). When I run the program after the brief delay I gave it (1000 milliseconds) the pellets begin to appear extremely rapidly flashing on the screen in different locations. What I want to do is make the pellets appear one at a time in random locations every 10 seconds instead of flashing around rapidly. Any help would be appreciated.
P.S. I have never done something like this before so, apologies if the code may seem a bit crude. Any advice with coding in general is also very appreciated.
Edit: I know this is still incorrect but I just want to know if I am at least on the right track so far. Now the "pellet" is just sitting there in the top right hand corner of the frame. Is there a problem now with my timer or the list or just everything in general. By the way if updating my code just to show new problems is frowned upon in this website let me know and I'll just ask for help in the comments section and stop with the edits.
package snake;
import java.awt.* ;
import java.awt.event.*;
import java.util.* ;
import javax.swing.*;
import javax.swing.Timer;
/**
*
* #author Carlos
*/
public class Pellet extends JPanel
{
Random randomNumber = new Random() ;
int x = 0 ;
int y = 0 ;
private Game game ;
private Timer timer ;
private final int DELAY = 100 ;
private ArrayList<Pellet> al = new ArrayList<>() ;
public Pellet(Game game)
{
this.game = game ;
}
#Override
public void paint(Graphics g)
{
super.paintComponent(g);
g.fillOval(x, y, 10, 10);
for(int i = 0 ; i < al.size() ; i++)
{
Pellet p = al.get(i) ;
p.paintComponent(g);
}
}
public void pelletTimer()
{
timer = new Timer(DELAY, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
x = randomNumber.nextInt(game.getWidth()) ;
y = randomNumber.nextInt(game.getHeight()) ;
Pellet p = new Pellet(game) ;
al.add(p) ;
repaint() ;
}
}) ;
timer.start();
}
}
You've lots of issues with your code attempt, so let's try to go through them.
First off this is a Swing program and so you must take care to not stomp on the Swing event thread, something that a java.util.Timer will do.
You're also creating your java.util.Timer within a paint method, something that should never be done since this method is for painting and painting only, and should not be slowed down or involved with non-painting code.
Your paint method is not a true paint method override of a Swing component since it has the wrong signature (it uses a Graphics2D parameter not the expected Graphics parameter) and your class does not extend a Swing component, so this method will do nothing of use for you.
Suggestions:
Draw in a paintComponent method override in a class that extends JPanel.
Call the super.paintComponent method first.
Use a javax.swing.Timer or "Swing" Timer to drive your animation.
In your Timer create a new pellet, add it to an ArrayList of pellets, and call repaint() which will tell the JVM to repaint your GUI and thus it will automatically call your paintComponent method for you.
In your paintComponent method, after calling the super's method, iterate through the pellet ArrayList drawing each individual pellet as you do.
Check out the Swing tutorials especially the graphics section. You can find a link to them here: swing info.
And check out this site for Swing animation examples, many written by me. You can find some results with this search.

Graphical Components

I have done a program that numerically solves a set of differential equations which describes how an "arbitrary" illness move in an isolated and constant population, it was a programming assignment from a class I took a while ago. What I've done to extend it is to add some graphical components that can pause, reset and "play" the simulation, as well as some components that allows me to change some constants in the equations.
All this was an exercise in programming as I find it to be fun and exciting and want to become better.
However, at the moment I'm stuck, what I want to do now is to make a very simple form of animation of it. I want to visualize the data I get for the number of infected, susceptibles and resistants in a grid as points. I managed to create the grid and have an idea of how to place the dots.
The problem I have is how to draw the dots as the program is working, I can draw one dot in the grid but only as the grid is created, that's it. I need to be able to create a dot at a specific place in the grid, this goes on until the number of dots reaches a finite number, say 30. At that points I want to have the first dot, the one the left, removed, all the dots shifted to the left and place the new dot at the furthest right of the grid, the whole thing is then repeated.
I think I will be able to figure it out with some help/hints about the paintComponent() method and whether I need to use repaint() method at all, I can't get my head around these for some reason. I've read through my course literature on Java, but despite the extensive sections where he explains most of the different graphical components he does not say that much about those methods, only that you don't call for the paintComponent() method, it is done automatically.
If there is something unclear let me know and I'll try to clarify it.
Thanks in advance.
//
Fox Mulder
I think I will be able to figure it out with some help/hints about the paintComponent() method and whether I need to use repaint() method at all, I can't get my head around these for some reason.
Basically, say you create a custom component by extending JPanel. When you #Override the paintComponent() method, it get implicitly called for you, so you never have to call it. So what ever you paint inside the method, gets drawn on your surface. For example
public class DrawingPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, y, 10, 10);
}
}
When you call repaint() you are basically causing the paintComponent method to be call implicitly. So to answer your question, Yes you will need to call it if you want to animate, as you will need to update some kind of variable (like the x and y) in the paintComponent() method, to see any change in the drawing.
You can see more at Performing Custom Painting
Not to handle the actual animation, you'll want to use a javax.swing.Timer. You can see more at How to use Swing Timers. Here's the basic construct
Timer ( int delayInMillis, ActionListener listener )
where delayInMillis is the time to delay between ticks(in this case animations) and the ActionListener listens for "ticks". Each tick, the actionPerformed of the ActionListener is called. There, you can put the code to update any variables you use for animation.
So for example you update the x and y, in the actionPerformed, then call repaint()
public class DrawingPanel extends JPanel {
int x = 50;
int y = 50;
public DrawingPanel() {
Timer timer = new Timer(40, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
x += 5;
y += 5;
repaint();
}
});
timer.start();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(x, y, 10, 10);
}
}
Now this was just a simple example. But in your case you want to animate a scatter plot. So what you can do is have a list of Points and in the actionPerformed you can add pull points from that list and push them into another list that is to be drawn. So say you have this
List<Point> originalPoints;
List<Point> pointsToDraw;
...
#Override
protected void paintComponent(Grapchics g) {
super.paintComponent(g);
for (Point point : pointsToDraw) {
g.fillOval(point.x - 5, point.y - 5, 10, 10);
}
}
Basically all the points in pointsToDraw list will be drawn. Initially it will be empty. And in the timer, you can add to the list, until the originalPoints list is exhausted. For example.
List<Point> originalPoints;
List<point> pointsToDraw;
private int currentIndex = 0;
public DrawingPanel(List<Point> originalPoints) {
this.originalPoints = originalPoints;
pointsToDraw = new ArrayList<>();
Timer timer = new Timer(40, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
if (currentIndex == originalPoints.size() - 1) {
((Timer)e.getSource()).stop();
} else {
pointsToDraw.add(originalPoints.get(currentIndex));
currentIndex++;
}
repaint();
}
});
timer.start();
}
So basicall you just keep a current index. When the index reaches the size of the original list, you stop the timer. Otherwise you just pop from the originalPoints and push to the pointsToDraw. For every point you add the pointsToDraw, a repaint() is called, and there will be another point for the paintComponent to draw a circle with.
The END
UDPATE
I just reread your question, and I think I have have misunderstood it. If you want all the points drawn, then basically just have one list. And draw all the points initially. with each tick, just remove the first index, advance all the rest up an index, and add a new one to the end. Though this is the implementation of a LinkedList so you may just want to use that

How to paintComponent to display objects in Java?

Okay, so I have a GameField class, and a GameObject class and a Panel class.
The GameObject class describes an object, which has an x and y position, width and height, and x and y direction (in which it is currently moving). The GameField class has a few different instances of these objects, some stored by themselves, and some stored in primitive arrays.
The Panel class is supposed to display these objects on the screen. I used JPanel for this.
However, when it comes to actually displaying them on the screen, I'm a bit lost. I need to implement a function called paintComponent(Graphics graphics), which takes in a Graphics object.
To start, I want to display all the objects on the screen, and set their colour. Their size, position, etc. are handled elsewhere. How can I use these attributes to set the actual objects to have a size, position and direction?
I may need to override the paintComponent function to display all the objects in GameField.
If you could help me out with some code, that'd be great.
I'm not quite clear on what you mean by "their size, position, etc. are handled elsewhere". For now, let's assume that you have approximately the following structure (fields and other methods ommitted for clarity) :
class GameObject {
java.awt.Color getColor() { ... }
java.awt.Point getPosition() { ... }
java.awt.Point getDirection() { ... }
java.awt.Dimension getSize { ... }
}
class GameField {
List<GameObject> getGameObjects() { ... }
}
class Panel extends JPanel {
private GameField getGameField() { ... }
#Override
public void paintComponent(Graphics g) {
// this is where the GameObjects must be painted
}
}
The paintComponent method is responsible for the screen representation of the Panel class. If you override it, you have just won that responsibility from it. Luckily, drawing is - if tedious - rather simple. You asked about that Graphics parameter. Very simply put, it is set for you by the magic of Java and gives you a toolbox to use for drawing.
First, you will want to have a clean slate whenever the panel is repainted. You cannot delete anything once it is painted, but you can easily paint the entire panel in a background color of your choice.
g.setColor(Color.white); // everything that is now painted will be white
g.fillRect(0, 0, getWidth(), getHeight()); // fills the entire area with the set color
Now, for each GameObject you have, let's place rectangle in the objects' defined color and size on the screen, with it's center on the object's position.
for (GameObject object : getGameField().getGameObjects()) {
g.setColor(object.getColor());
g.fillRect(object.getPosition().x - (object.getSize().x / 2), object.getPosition().y - object.getSize().y / 2, object.getSize().x, object.getSize().y);
}
The fillRect method requires the first two arguments to be the top-left corner of the rectangle. So to have it centered on the object's position, we subtract half the size from the position for the x and y values respectively. Now you have, for every GameObject, a rectangle of the right diameter in the object's color at the right position.
You should read up on the javadoc on java.awt.Graphics to find out how to draw other stuff, maybe image sprites or lines for the direction or something. It is cumbersome but doable.

Java Swing mouseDragged callback speed

I have a question regarding the callback speed of the mouseDragged message of the MouseMotionListener in Java Swing. This post is sort of related but it's not entirely the same so I started a question of my own.
I'm making a small in-house application with no eye on commercial distribution that is basically a digitalized TCG (Trading Card Game) emulator. For any of you familiar with MtG (Magic the Gathering), you might've heard from such a similar program. I'm trying to create something that looks sort of like this, but less fancy.
My GUI consists of a JFrame with menu and then some panels containing various buttons and labels, but I'll only go over the relevent parts to explain my problem.
In essence, I'm using a vertical split JSplitPane with a JPanel on the left, with in that a JScrollPane with a JList in it, which represents at any time the cards in your hand that you can play. On the right side of the split, I have a JLayeredPane with a background image in the DEFAULT_LAYER (subclass of JPanel that overrides the draw function to add an image) and, on various layers above the PALETTE_LAYER, I display the cards that are in play (gathered in an ArrayList) by means of custom CardPanels (another subclass of JPanel that illustrates a card). The entire JLayeredPane is thus a representation of the table in front of you with all the cards you've already played.
I first started by adding a MouseListener and a MouseMotionListener to the JLayeredPane to pick up events, allowing me to register a mouse press, check if this was above a card, then use the mouse dragged event to move the card around and finally mouse release to place it back . This all works perfectly fine and if I add logging information I can see the mouseDragged callback function is called often, allowing for a visually fast dragging motion without lag.
Today I decided to add functionality to allow the user to drag a card from his hand to the "table" (instead of double clicking on the card in the JList), so I added the appropriate listeners to the JList along with filling in some functions like MousePressed and MouseReleased. On a mouse press, I check what card from the list was clicked, I lock the list, create a custom CardPanel (but don't add it anywhere yet, I just allocate and initiate it!) and set a flag. In mouse dragged, I check if this flag is set. If it is, I check where the cursor is. If it is anywhere above the JLayeredPane, I add the CardPanel to the DRAG_LAYER and set another flag. If this second flag is set in successive calls to mouse dragged, I don't add the panel again but I just change the location. This functionality is practically the same as the one in my previous mouse dragged callback. On mouse release, I unlock the list and add the CardPanel on the correct layer in the JLayeredPane.
Everything is working as intended so I'm pretty sure my code is okay, but there is just one slight issue:
When dragging a card from the list to the layered pane (instead of from the layered pane to the layered pane), I notice the mouseDragged callback is called at a pretty low frequency by the JList (approx 10 times per second), introducing some visually disturbing lag (compared to approx 30 times per second in the first case of dragging).
I'm going to add some code snippets as to clarify my problem but I'm afraid adding all the code to allow you to run it yourself would be serious overkill.
The main question in this post is: does anybody know why the mouseDragged is called faster by one MouseMotionListener than by another MouseMotionListener? The listener to the JLayeredPane component makes fast successive calls, the listener to the JList calls significantly slower.
Note: I'm developing in Netbeans and I'm using the built-in graphical Swing Interface Builder. I'm using a JFrame form as my main class.
public class MyFrame extends JFrame{
...
protected JLayeredPane layeredPane;
protected JList cardsInHandList;
...
...
protected ArrayList<String> cardsInHand;
...
private void attachListeners(){
layeredPane.addMouseListener(new MouseAdapter(){
public void MousePressed(MouseEvent e){
// set a flag, start a drag
}
public void MouseReleased(MouseEvent e){
// unset a flag, stop a drag
}
});
layeredPane.addMouseMotionListener(new MouseMotionAdapter(){
public void MouseDragged(MouseEvent e){
// drag the card around
// gets called a lot!
// actual code:
if (e.getButton() == MouseEvent.BUTTON1) {
if (!dragging) return; // the flag
int x = e.getX() - 10;
int y = e.getY() - 10;
// snap to grid
x /= GRIDX;
x *= GRIDX;
y /= GRIDY;
y *= GRIDY;
// redraw the card at its new location
draggedCard.setLocation(x, y);
}
}
});
cardsInHandList.addMouseListener(new MouseAdapter(){
public void MousePressed(MouseEvent e){
// set a flag, start a drag
}
public void MouseReleased(MouseEvent e){
// unset a flag, stop a drag
}
});
cardsInHandList.addMouseMotionListener(new MouseMotionAdapter(){
public void MouseDragged(MouseEvent evt){
// check cursor location, drag if within bounds of layeredPane
// gets called a whole lot less!! _Why?_
// actual code:
if (!draggingFromHand) return; // the flag
// check location of cursor with own method (contains() didn't work for me)
if (isCursorAtPointAboveLayeredPane(evt.getLocationOnScreen())) {
// calculate where and snap to grid
int x = (int) (evt.getLocationOnScreen().getX() - layeredPane.getLocationOnScreen().getX())-10;
int y = (int) (evt.getLocationOnScreen().getY() - layeredPane.getLocationOnScreen().getY())-10;
// snap to grid
x /= GRIDX;
x *= GRIDX;
y /= GRIDY;
y *= GRIDY;
if(!draggingFromHandCardPanelAdded){
layeredPane.add(draggingFromHandCardPanel, JLayeredPane.DRAG_LAYER);
draggingFromHandCardPanelAdded = true;
} else {
draggingFromHandCardPanel.setLocation(x,y);
}
}
}
});
}
I'll try to build a short runnable example reproducing the problem and then attach the code somewhere but right now I got to skoot.
Thanks in advance
PS: I am aware that there is another way to drag in Java, involving TransferHandlers and all that but it just seems like too much hassle and it isn't an actual answer to my question of how come the one callback seems to be called more than the other, so please don't tell me to use that instead.
Once you drag outside the list, Java start generating synthetic mouse events for the list, which might be the cause. See the javadoc for JComponent#setAutoscrolls(boolean).
You might get better results using a global event listener, see
http://tips4java.wordpress.com/2009/08/30/global-event-listeners/

Sprite Animation Management Using Threads

I'm in the process of creating my own two-dimensional game. Every object in the game world has a Sprite object, and when the screen is drawn, the object's sprite is drawn in the object's location.
The sprite class can be either a single image or a series of images used to make an animates sprite.
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
public class Sprite implements Runnable{
private int currentImage;
private BufferedImage[] sprites;
private int delayMS;
private Thread animation;
public Sprite(BufferedImage sprite){
sprites = new BufferedImage[1];
sprites[0] = sprite;
}
public Sprite(BufferedImage[] spriteAnimation,int delay){
this.sprites = spriteAnimation;
currentImage = 0;
delayMS = delay;
//start a thread to time animation
animation = new Thread(this);
animation.start();
}
private void next(){
if(currentImage < sprites.length - 1)
currentImage++;
else
currentImage = 0;
}
public void run() {
while (Thread.currentThread() == animation) {
//delay the animation for delayMS
try {
Thread.sleep(delayMS);
} catch (InterruptedException e) {
break;
}
//next image
next();
}
}
public void draw(Graphics2D g,int x,int y){
g.drawImage(sprites[currentImage],null,x,y);
}
}
I couldn't find any reliable information on the subject of using threads to run many animations, and I was wondering whether this was the best approach.
Everything works great until I throw 200ish identical objects into the world. FPS begins to lower and some animated sprites begin to change frames at different times. This would make sense since the threads would start to delay when instantiated.
My questions is whether there is a more efficient way to handle this, allowing me to use more objects without significant FPS loss, and synchronize the thread/threads so that the animations switch frames together.
The usual way of doing this is to have a game loop that knows all the sprites and calls a draw method on them (or preferably only those that needs be redrawn) for each frame. This way, whenever a sprite decides it wants to animate, it will not do so until the next frame. Just do the animating the the draw method, e.i. the next() part.
And of course, in the game loop you put your delay, which will determine the fps.
To get frame rate independent animation you do the same but add an argument to the draw method which says how much time has passed since the last draw, then calculate the amount of animation based on this.
Instead of running every sprite on its own thread, you could put the sprites on a container, and on a rendering thread iterate through all visible sprites and draw them (you could even implement sprite priorities, render background layers before sprites, ...).
if you use more threads for sprites (i.e, one thread for one sprite) then it will eat your cpu and it will hang the best way to implement animation in games is for every run loop of thread do change the animation sequences ( like next image or previous upon key Presses) and paint the sprites at the end.
Here if it is more sprites and paint will take more time then FPS will drop at the time two or three threads max with synchronization

Categories