You know it’s already true by default on a JPanel? (Any other constructor)
I’ll assume you haven’t read the thread because he had a threading issue, not a buffering one, and the discussion has evolved to a different place anyway.
You know it’s already true by default on a JPanel? (Any other constructor)
I’ll assume you haven’t read the thread because he had a threading issue, not a buffering one, and the discussion has evolved to a different place anyway.
Sorry, I just read the title, and when scrolling down, I saw long codes, so I made that post. Sorry.
SHC,
D’oh! Sorry about that, that was my bad! I didn’t want to create a bunch of new topics on the forum over this so I posted my updated question on the same topic. I just edited the original post to notify you all about the slightly changed topic.
BurntPizza,
Holy smokes, that is awesome! ;D Wow, I have a lot to learn on this stuff…I spent days trying to get those two stupid dots to move back and forth and you cranked out that awesome animation within the hour. lol
When I do get to making the swinging metronome animation, I would like to use images however rather than drawing with graphics so I can make the animation look “pretty” in photoshop. It’ll probably end up looking something like this…although I’ll probably make my own “less realistic” looking version.
Here is a mobile metronome program that is similar to the kind of animation style that I’m shooting for:
I think I could get something like this to work with the “setIcon” method that I used above, but I don’t know if that’s a good method to use for animation as far as speed, smoothness, etc…I’ve never seen that used in any of the animation tutorials I’ve looked at, so I’m thinking this probably isn’t a recommended method.
Thanks again!
It’s possible to use ImageIcons, but yeah, I wouldn’t recommend it.
I’d use ImageIO to read in the images into a List or something, which is the animation for half a swing (from one side to the other) and ping-pong through the list like so:
boolean direction = true;
int currentImageIndex = 0;
// a simple form of state machine, sweeps back and forth on the range [0, images.size() - 1]
void update() {
currentImageUpdate += direction ? 1 : -1;
if (currentImageIndex == 0) {
direction = true;
}
if (currentImageIndex == images.size() - 1) {
direction = false;
}
}
public void paintComponent(Graphics g) {
g.drawImage(background, 0, 0, null);
g.drawImage(images.get(currentImageIndex), 0, 0, null);
update();
}
You can add event hooks into update() if needed, like if (currentImageIndex == 0) // fire event listeners
to detect one end of the swing.
Thanks BurntPizza!
I just tried re-working my original single sprite sheet example (using a BufferedImage) so I could post the code I was trying and I actually got it working this time!!!
Before I was trying to do it all in a single file and I calling repaint() from within the EDT, but it wasn’t working. So instead I tried creating a separate animation JPanel class and this method works now. I’m not sure exactly what I was doing wrong before…but for whatever reason, starting over almost from scratch I finally got it!
Would this method be ok to use for animating more frames?
Thanks again!
[EDIT] CODE UPDATED! SEE NEXT POST…
BurntPizza,
Here’s an updated version using multiple frames and the method you suggested for switching directions.
I also tweaked it so it’s more like an “game loop” with an update and edtPaint methods. One thing I’ve noticed now is that I’m resetting the background color to the same color for the labels as well as redrawing the same image on the frames where the visuals don’t change. Would it be more efficient to set a flag and check to see if it these items need to be updated before setting them again or is it ok to do this kind of constant updating on each frame on the edt?
I just want to make sure I’m not setting myself up for problems down the road as I started adding more stuff to it.
Thanks again!
Test.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.IOException;
public class Test extends JFrame{
JPanel panel = new JPanel();
final JLabel label1 = new JLabel("Label 1");
final JLabel label2 = new JLabel("Label 2");
AnimationPanel animationPanel;
final Color[] colors = new Color[] { Color.green, Color.yellow };
final int[] position = { 100, 300 };
int delay = 4;
int count = 0;
int labelIndex = 0;
private BufferedImage animation;
boolean rightDirection = true;
int currentImageIndex = 2;
public static void main(String[] a) throws InterruptedException {
Test test = new Test();
}
public Test() throws InterruptedException {
try { animation = ImageIO.read(getClass().getResource("dot.jpg")); } catch (IOException ioe) { ioe.printStackTrace(); }
animationPanel = new AnimationPanel(animation);
animationPanel.setMinimumSize(new Dimension(400,100));
animationPanel.setPreferredSize(new Dimension(400,100));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(410, 300);
setLocationRelativeTo(null);
panel.setSize(new Dimension(400, 400));
add(panel);
label1.setMinimumSize(new Dimension(100,100));
label1.setPreferredSize(new Dimension(100,100));
label1.setOpaque(true);
label2.setMinimumSize(new Dimension(100,100));
label2.setPreferredSize(new Dimension(100,100));
label2.setOpaque(true);
panel.add(label1);
panel.add(label2);
panel.add(animationPanel);
setVisible(true);
loop();
}
public void loop() throws InterruptedException {
while (true) {
update();
edtPaint();
Thread.sleep(16);
}
}
public void update(){
if (count % (int)(delay/4) == 0){
animationPanel.setSubImage(currentImageIndex);
currentImageIndex += rightDirection ? 1 : -1;
if (currentImageIndex == 0) {
rightDirection = true;
}
if (currentImageIndex == 4) {
rightDirection = false;
}
}
if (count % delay == 0){
labelIndex++;
}
count++;
}
public void edtPaint(){
final int li = labelIndex % 2;
SwingUtilities.invokeLater(new Runnable(){
public void run(){
label1.setBackground(colors[li]);
label2.setBackground(colors[li]);
animationPanel.paintSubImage();
}
});
}
}
AnimationPanel.java
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class AnimationPanel extends JPanel{
BufferedImage img;
BufferedImage subImg;
public AnimationPanel(BufferedImage imgIn){
img = imgIn;
setLayout(null);
}
public void setSubImage(int frameIn){
subImg = img.getSubimage(400*frameIn, 0, 400, 100);
}
public void paintSubImage(){
repaint();
}
public void paintComponent(Graphics g){
g.drawImage(subImg, 0, 0, null);
}
}
dot.jpg
Yeah it doesn’t really matter if you set the background color frequently, it’s not an expensive operation. If you want to add the logic to do it only when necessary for peace of mind, do it.
Only things really left for me to complain about are things you get with experience, like a developed sense of code conventions/style/formatting. Although Eclipse does have an on-save auto-formatter, imports organizer, and some other stuff I recommend you check out.
Alright, thanks a lot BurntPizza!
I’m currently using a very simple Java IDE called jGrasp. When I start working on my actual program I’ll be using NetBeans since that was recommended for use with Codename One, does NetBeans have a similar kind of auto-formatting feature? And just so I’m aware of them, what kinds of formatting issues have you noticed in my sample code?
Thanks again for your help!
Eh, just some (mostly) little things:
Random things I could think of looking at it, goes from quite nit-picky to obvious to fairly advanced. I don’t expect most of this from a simple demo/test program, sure, and some of it like I said you only gain from experience.
Refactoring is your friend! Try to develop good architectural and style habits.
Ok, thanks a lot BurntPizza! Those are great things to be on the look out for. I will say that most of them were neglected only because this was a quick example to post on the forum and not things I intend to do in my real program. I did a lot of hacking, pasting, and moving things around so sometimes I didn’t correct every detail just for the sake of time.
Thank you again for your help! I’m really glad I found this forum, you all are awesome!