*** UPDATE *** I have changed the topic slightly from my first post. Start from this post for the updated question:
Hi all,
I’m trying to get two JLabels to update at exactly the same time which I would like to do from my repaint method in my loop. However, the problem is that calling setText and setBackground on the JLables automatically calls the repaint method, therefore these labels aren’t actually getting updated by my repaint method in my loop.
I’m trying to implement a method of double buffering where I update the labels off screen and then in my repaint method I make an Image of the JPanel that holds the JLabels and display that Image. However, I cannot get this to work. The JFrame shows up solid black.
I believe that my problem is getting the Image of the JPanel and I’ve tried a couple different methods, but neither of them worked. I left both methods in my code so you can see the two methods I tried and one of them is commented out.
I did check to make sure everything else in my loop and double buffer were working correctly by removing all of the JPanel Image creating stuff in my paintComponent method and added a line to draw an oval with Graphics…
g.fillOval(x, 200, 20, 20);
Then I incremented “x” in my update method and the oval does move successfully across the screen. So everything else appears to be working.
Any help in getting my JPanel double buffer to work would be greatly appreciated…of if there is a better way I could be going about this, I’m open to suggestions there as well. Thanks!
Here’s my sample code:
TestApplication.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TestApplication {
static TestApplication app;
static Loop loop;
static Canvas canvas;
JPanel mainPanel;
JLabel[] beatDisplay;
boolean isPlaying=true;
int[] count = { 1, 1 };
boolean[] isGreen = { true, true };
JPanel center = new JPanel();
public static void main (String[] args) {
app = new TestApplication();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
app.gui_Create();
app.loop();
}
});
}
public void gui_Create() {
gui_MainPanel();
gui_AddContent();
canvas = new Canvas(app);
}
public void gui_MainPanel() {
mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(400,400));
mainPanel.setLayout(new BorderLayout());
mainPanel.setVisible(true);
}
public void gui_AddContent() {
center.setSize(new Dimension(400,400));
center.setMaximumSize(new Dimension(400,400));
center.setMinimumSize(new Dimension(400,400));
center.setPreferredSize(new Dimension(400,400));
center.setOpaque(true);
center.setVisible(true);
beatDisplay = new JLabel[2];
for (int i=0; i<2; i++){
String buttonName = "~ SOUND " + i + " ~";
beatDisplay[i] = new JLabel(buttonName);
beatDisplay[i].setPreferredSize(new Dimension(400, 50));
beatDisplay[i].setHorizontalAlignment(SwingConstants.CENTER);
beatDisplay[i].setOpaque(true);
beatDisplay[i].setFont(new Font("Serif", Font.BOLD, 40));
center.add(beatDisplay[i]);
}
// canvas.add(center, BorderLayout.CENTER);
}
public void loop(){
loop = new Loop(app, canvas);
loop.start();
}
public void update(){
if (loop.frameCount%30 == 0){
for (int i=0; i<2; i++){
beatDisplay[i].setText(""+count[i]);
if (isGreen[i]){
beatDisplay[i].setBackground(Color.GREEN);
isGreen[i] = false;
}
else{
beatDisplay[i].setBackground(Color.YELLOW);
isGreen[i] = true;
}
count[i]++;
if (count[i] > 4)
count[i] = 1;
}
}
}
}
Loop.java
public class Loop extends Thread {
static TestApplication app;
static Canvas canvas;
int fps = 60;
long frameCount=1;
public Loop(TestApplication appIn, Canvas canvasIn){
app = appIn;
canvas = canvasIn;
}
public void run(){
while (app.isPlaying){
app.update();
canvas.repaint();
try { Thread.sleep(fps); } catch (InterruptedException ie){}
frameCount++;
}
}
}
Canvas.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.BufferedImage;
public class Canvas extends JFrame{
static TestApplication app;
Image dbImage;
Graphics dbg;
public Canvas(TestApplication appIn){
app = appIn;
setTitle("Sound Test");
setPreferredSize(new Dimension(400,400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void setMainPanel(JPanel panel){
setContentPane(panel);
}
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g){
Image temp;
// NEITHER OF THESE WORK FOR CRREATING THE "TEMP" IMAGE
// temp = app.center.createImage(app.center.getWidth(), app.center.getHeight());
temp = createImage(app.center);
g.drawImage(temp, 0, 0, this);
repaint();
}
private static BufferedImage createImage(JPanel panel) {
int w = panel.getWidth();
int h = panel.getHeight();
BufferedImage bi = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
return bi;
}
}