h4. Greetings everyone,
The issue with my program is that
when attempting to alter the program's frame rate on the fly, through changing the swing timer delay using the value of a slider found inside the nested panel, there is no change. I'm unsure whether this is an issue due to how I'm implementing the change listener, or whether it is because I am failing to use nested panels correctly.
I have created this basic program to represent the problem I am having, if you would like to ask any other questions about my actual program then feel free. The NestedPanels class merely opens the applet, you can ignore this seeing as I'm pretty sure it's not the root of my problem. The Nest class is then the main JPanel, which contains the Nested JPanel.
Thanks all,
BoxCat
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.BufferedImage;
public class NestedPanels extends JApplet
{
public static void main(String []args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame window = new JFrame();
window.setDefaultCloseOperation(3);
JApplet applet = new NestedPanels();
applet.init();
window.getContentPane().add(applet);
window.pack();
window.setVisible(true);
}
}
);
}
public void init()
{
JPanel panel = new Nest();
getContentPane().add(panel);
}
}
//The main panel
class Nest extends JPanel implements ActionListener
{
private static Timer timer;
private static Image buffer;
private int _delay = 0; //used to set timer delay
private int _initFrameRate = 35;
private int _frameRate = 0; //will take value from nested panel's slider to update program fps
//these 3 variables are used in paintComponent to calculate frameRate as verification of program run speed
private int _displayFrameRate = 0;
private int _frameCount = 0;
private long startTime = System.currentTimeMillis();
//the panel's nested panel
private Nested nestedPanel = new Nested();
public Nest()
{
setPreferredSize(new Dimension(700, 700));
setBounds(0, 0, 700, 700);
setBackground(Color.BLUE);
setVisible(true);
setOpaque(true);
setLayout(null);
add(nestedPanel);
//irrelevant, only the painting buffer
buffer = new BufferedImage(700, 700, 1);
//setting the programs initial frame rate
_delay = 1000 / _initFrameRate;
timer = new Timer(_delay, this);
timer.setCoalesce(true);
timer.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
//frame rate calculation
_frameCount++;
if (System.currentTimeMillis() > startTime + 1000)
{
startTime = System.currentTimeMillis();
_displayFrameRate = _frameCount;
_frameCount = 0;
}
g2 = (Graphics2D)buffer.getGraphics();
//output of the calculated frame rate
g.setColor(Color.WHITE);
g.drawString("FPS: " + _displayFrameRate, 100, 100);
getGraphics().drawImage(buffer, 0, 0, null);
}
//IMPORTANT - Potential area of failure
//called before repaint, attempts to set frame rate using the nested panel's slider value
void updatePanelFPS()
{
_frameRate = nestedPanel.sliderValue;
_delay = 1000 / _frameRate;
timer.setDelay(_delay);
}
public void actionPerformed(ActionEvent e)
{
updatePanelFPS();
repaint();
}
//IMPORTANT - Potential area of failure
//The nested panel class containing the slider
class Nested extends JPanel implements ChangeListener
{
//This slider will basically function as a way of altering FPS
JSlider slider;
int sliderValue = 0; // initialized at 0 before setting to the altered slider value
public Nested()
{
setBackground(Color.GREEN);
setVisible(true);
setOpaque(true);
setEnabled(true);
setBounds(200, 200, 200, 200);
slider = new JSlider(JSlider.VERTICAL, 0, 60, 30);
slider.setMajorTickSpacing(1);
slider.setMinorTickSpacing(1);
add(slider);
}
public void stateChanged(ChangeEvent event)
{
//if the change was the slider value
if (event.getSource() == slider)
{
//assign the sliderValue
JSlider source = (JSlider)event.getSource();
sliderValue = (int)source.getValue();
}
}
}
}