Reputation: 97
I have a variable that is marked final(number_of_cubes). I need to use it in few places in my code. But I need to change it to slider. What would you suggest? This is what I've already done:
class GrimMain{
static JFrame frame = new JFrame();
public static void main(String[] args) {
final DrawingComponent fps = new DrawingComponent();
final int number_of_cubes = 10;
final Rect[] r1 = new Rect[number_of_cubes];
final JButton button1= new JButton("Start");
button1.setLocation(700,600);
button1.setSize(100,30);
fps.add(button1);
final JButton button2= new JButton("Stop");
button2.setLocation(700,640);
button2.setSize(100,30);
fps.add(button2);
button1.setEnabled(true);
button2.setEnabled(true);
final JSlider slider = new JSlider(JSlider.HORIZONTAL,3,10,10);
slider.setLocation(300,600);
slider.setSize(290, 70);
slider.setMajorTickSpacing(330);
slider.setMinorTickSpacing(115);
slider.setPaintTicks(false);
slider.setPaintLabels(true);
fps.add(slider);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent changeEvent) {
//I want to change it here but I cant
number_of_cubes = Integer.valueOf(slider.getValue())
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
for(int i=0;i<number_of_cubes;i++){
fps.remRect(r1[i]);
frame.getContentPane().repaint();
}
button1.setEnabled(true);
button2.setEnabled(false);
}
});
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("yes");
Random rn = new Random();
Random rand = new Random();
for(int i=0; i <number_of_cubes;i++){
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r, g, b);
r1[i] = new Rect(rn.nextInt(600), rn.nextInt(400), 15, 15, randomColor);
}
for(int i=0; i < number_of_cubes;i++){
fps.addRect(r1[i]);
}
fps.animate();
button1.setEnabled(false);
button2.setEnabled(true);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fps.setPreferredSize(new Dimension(900, 700));
frame.getContentPane().add(fps);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
This is the full code of my class
Upvotes: 0
Views: 81
Reputation: 14259
Final is more like a design decision and a hint to the compiler than anything else. You can define something as final, so the compiler will tell you if you accidentally changed it somewhere else. In addition the compiler can make various optimizations if it knows that the variable will never be changed.
So if your number of cubes can change over time, this is just not a candidate for final
.
Your code however has a different problem if you actually remove the final: if number_of_cubes change, the size of the array does not change with it, so later on you will get an ArrayIndexOutOfBoundsException to notify that you violated the array constrains. It might be a good idea to write a setter for the number of cubes instead, that adjusts everything else that needs adjusting as well. Example:
void setNumberOfCubes(int cubes) {
number_of_cubes = cubes;
r1 = Arrays.copyOf(r1, cubes);
}
Upvotes: 0
Reputation: 24998
You cannot declare something as it will not change
and then change it later. The docs state:
A variable can be declared final. A final variable may only be assigned to once. Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.
Since the value of cubes will change, you cannot declare it as final
. Remember that final
values are constants and constants do not change.
Upvotes: 2
Reputation: 179
if you need to change it then you can't have it as a final, this may give you some insight on it, http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4
Your number of cubes is a primative and not an object so modifying the value violates it's final state.
Upvotes: 1