Can't update JLabel array - java

I'm writing a thing that has a JLabel array that has something in the first index and then detects right keys that shifts the thing to the right. Basically, [x][][][][][][] starts, then if I press the right key, it shifts to [][x][][][][][]. Currently, I can't get the JLabel to properly update. It shows up correctly at first, but when I hit the right key, the entire thing gets messed up. Ie, it starts like this:
(I have the numbers to gauge the shifting, they'll go away when I figure this problem out)
Which is right, but when I hit the right key, everything disappears (just blank.) However, my test in the console still gives me the correct values though.
The numbers don't even show up :/
Here's the relevant code:
public Template() {
for (int i = 0; i < 7; i++) {
positions[i] = new JLabel();
positions[i].setFont(new Font("Times New Roman", Font.PLAIN, 15));
System.out.println("Yay");
}
// I initialize my JLabel array here instead of below because if I don't do this, there's no change whatsoever if I press the right key. I'm not sure if I did it correctly, though.
initialize(p1, 0);
}
private void initialize(String p, int index) {
frame = new JFrame();
JPanel header = new JPanel();
header.setBackground(Color.WHITE);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
header.setLayout(gbl);
header.setPreferredSize(new Dimension(400, 50));
c.fill = GridBagConstraints.HORIZONTAL;
for (int i = 0; i < 7; i++) {
if (i == index) {
positions[i].setText(p + "");
System.out.println("indexed set");
} else {
positions[i].setText("" + i);
System.out.println(positions[i].getText());
System.out.println("set");
}
c.gridx = i;
c.gridy = 0;
header.add(positions[i], c);
}
frame.add(header, BorderLayout.NORTH);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
playMove(p);
}
public void playMove(String p) {
...
frame.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent keyEvent) {
System.out.println("Game.keyPressed -- event: " + keyEvent);
if (keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("Right key pressed!");
int index = searchHeader();
System.out.println("index: " + index);
if (index != 6) {
int shift = index + 1;
System.out.println("shift: " + shift);
SwingUtilities.updateComponentTreeUI(frame);
initialize(p, shift, t);
}
}
}
#Override
public void keyReleased(KeyEvent keyEvent) {
System.out.println("Game.keyReleased -- event: " + keyEvent);
}
});
}
public int searchHeader() {
for (int i = 0; i < 7; i++) {
String val = positions[i].getText();
if (val == "x" || val == "o") {
System.out.println("i:" + i);
return i;
}
}
return 0;
}
My System.out.println(); tests all return the correct values (the correct index, shift, and numbers in the array) but I'm not sure why the thing isn't showing up :/
If my code needs clarification or missed something (I did cut some irrelevant info off after taking advice from my last post) please tell me

Since you didn't provide a minimal runnable example that we could copy into our IDE, run, and debug, I came up with this example GUI that shows an X shifting to the right.
I used the right arrow key to perform the shift.
Here's the GUI when starting the application.
Here's the GUI after shifting the X three positions.
Here's the GUI after shifting the X four positions.
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Netbeans section.
The first thing I did was start the Swing application by calling the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
Next, I created a JFrame. The JFrame methods must be called in a specific order. This is the order I use for my Swing applications.
I created a main JPanel to hold the array of eight JLabels. I also created an int to hold the position of the X. I used a FlowLayout to lay out the array of JLabels. I( used the Box class to put some space between the JLabels.
As you're creating a more complicated game, create an application model using plain Java getter/setter classes.
I used Key Bindings to bind the right arrow key to the Action class. I used AbstractAction so I'd only have to code the actionPerformed method.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class ShiftRightExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new ShiftRightExample());
}
private int position;
private JLabel[] spotLabel;
public ShiftRightExample() {
this.position = 0;
}
#Override
public void run() {
JFrame frame = new JFrame("Shift Right Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = createMainPanel();
frame.add(mainPanel, BorderLayout.CENTER);
setKeyBindings(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.spotLabel = new JLabel[8];
for (int index = 0; index < spotLabel.length; index++) {
spotLabel[index] = new JLabel(" ");
panel.add(spotLabel[index]);
if (index < (spotLabel.length - 1)) {
panel.add(Box.createHorizontalStrut(20));
}
}
updateMainPanel(position, "X");
return panel;
}
private void setKeyBindings(JPanel panel) {
panel.getInputMap().put(
KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "shiftRight");
panel.getActionMap().put("shiftRight", new ShiftRightAction());
}
public void updateMainPanel(int position, String value) {
spotLabel[position].setText(value);
}
public class ShiftRightAction extends AbstractAction {
private static final long serialVersionUID = 1L;
#Override
public void actionPerformed(ActionEvent event) {
updateMainPanel(position, " ");
position++;
position = position % spotLabel.length;
updateMainPanel(position, "X");
}
}
}

Related

Buggy JCombobox - KeyPressed

With a JComboBox containing some elements beginning with repeated letters, typing in the character twice will return the character typed, followed by the first character of its type. For example, typing "bb" in a list containing ba, bb, and bc will return ba. However, if this list also contains bbd, continuing to press a "d" will return the bbd option. This is the same with numbers: Typing "33" returns 30, while typing "334" returns 334.
Is there a way to fix this so that a double keyPress really returns what is typed in?
Quick sample program:
String[] range = new String[401];
for (int i = 0; i <= 400; i++) {
range[i] = "" + i;
}
private javax.swing.JComboBox<String> jComboBox1;
jComboBox1 = new javax.swing.JComboBox<>();
getContentPane().setLayout(new java.awt.GridLayout());
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(range));
getContentPane().add(jComboBox1);
pack();
Item selection via keyboard is controlled by the JComboBox.KeySelectionManager, which, surprisingly, you can actually implement and change 😱
The one thing that the default implementation does is, it will try and find items which match the key stroke based on the first character of the item (when converted to a String via toString). The "neat" thing about this is, it will search from the currently selected item (if not null), if it can't find another matching item, it will start at the beginning of the model instead.
What this means is, if you type 3 repeatedly, it will, progressively move through all the items that start with 3. (30, 31, 32 ... 39, 300 ...)
Buuuut, this isn't what you apparently want, so, instead, you're going to have to supply your own algorithm.
One, very important consideration is, what happens when the user stops typing? If the user has typed 33, stops, then types 3 again, what should happen? Should it select 3 or 333?
The following is a VERY basic example, based on the DefaultKeySelectionManager used by JComoBox by default. It uses a StringBuilder to keep track of the key strokes and a Swing Timer which provides a 250 millisecond timeout, which will clear the StringBuilder after 250 milliseconds of inactivity (you could pass this value into the constructor to define your own)
When called by the JComboBox, it will simply do a linear search of the model for a item whose toString result starts with what's been typed.
This example is a case insensitivity, but you can modify it to meet your particular needs
public class MyKeySelectionManager implements KeySelectionManager {
private Timer timeout;
private StringBuilder pattern = new StringBuilder(32);
public MyKeySelectionManager() {
timeout = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pattern.delete(0, pattern.length());
}
});
timeout.setRepeats(false);
}
#Override
public int selectionForKey(char aKey, ComboBoxModel<?> model) {
timeout.stop();
pattern.append(Character.toLowerCase(aKey));
String match = pattern.toString();
for (int index = 0; index < model.getSize(); index++) {
String text = model.getElementAt(index).toString().toLowerCase();
if (text.startsWith(match)) {
timeout.start();
return index;
}
}
timeout.start();
return -1;
}
}
Runnable example
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JComboBox.KeySelectionManager;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
for (int index = 0; index < 50; index++) {
model.addElement(Integer.toString(index));
}
JComboBox cb = new JComboBox(model);
cb.setKeySelectionManager(new MyKeySelectionManager());
JFrame frame = new JFrame();
JPanel content = new JPanel(new GridBagLayout());
content.setBorder(new EmptyBorder(32, 32, 32, 32));
content.add(cb);
frame.setContentPane(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MyKeySelectionManager implements KeySelectionManager {
private Timer timeout;
private StringBuilder pattern = new StringBuilder(32);
public MyKeySelectionManager() {
timeout = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pattern.delete(0, pattern.length());
}
});
timeout.setRepeats(false);
}
protected int indexOf(Object item, ComboBoxModel<?> model) {
for (int index = 0; index < model.getSize(); index++) {
if (model.getElementAt(index) == item) {
return index;
}
}
return -1;
}
#Override
public int selectionForKey(char aKey, ComboBoxModel<?> model) {
timeout.stop();
pattern.append(Character.toLowerCase(aKey));
String match = pattern.toString();
for (int index = 0; index < model.getSize(); index++) {
String text = model.getElementAt(index).toString().toLowerCase();
if (text.startsWith(match)) {
timeout.start();
return index;
}
}
timeout.start();
return -1;
}
}
}
Don't forget to take a look at JComboBox#setKeySelectionManager for more details

Null Pointer with JButton Array

So far I have created a frame at adds my ControlPanel class, the class takes an int, and that int is used to fill an array with JButton objects which are the added to a Panel and displayed.
However I am getting a Null Pointer error when I try to add JButtons to the array. I'm not sure why (since I thought null pointers were from when you try to reference something that isn't in the spot in the array?)
Any help on this would be great appreciated.
Error Message:
Exception in thread "main" java.lang.NullPointerException
at elevator.ControlPanel.<init>(ControlPanel.java:22)
at elevator.Elevator_Simulator.main(Elevator_Simulator.java:27)
Main Class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Elevator_Simulator extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame ("Elevator Simulation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ControlPanel(8));
frame.pack();
frame.setVisible(true);
}
}
Control Panel Class:
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
public class ControlPanel extends JPanel implements ActionListener {
public JButton[] floorButton; // an array of floor buttons.
public int[] floorIn; // state of button. 0 == not click >= 1 means clicked (how many times).
public ControlPanel(int x) {
JPanel floors = new JPanel();
for (int i = 0; i < x; i++) { // interates through user input and creates that many JButtons.
floorButton[i].add(new JButton("F" + Integer.toString(i))); // adds a button to button array. Sets text to F(floorNo).
floors.add(floorButton[i]); // adds buttons to the floor panel.
floorButton[i].addActionListener(this); // adds action listener to the buttons.
}
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < floorButton.length; i++) {
if (e.getSource() == floorButton[i]) { // checks which button the event occured on.
floorButton[i].setBackground(Color.red); // sets button background to red.
floorButton[i].setText(floorButton[i].getText() + "(1)"); // marks button as clicked.
}
}
}
}
floorButton is not initalised to anything...
public JButton[] floorButton; // I'm null
public ControlPanel(int x) {
//...
for (int i = 0; i < x; i++) { // in
// Still null
floorButton[i].add(new JButton("F" + Integer.toString(i)));
Initalise the array to reflect what you need, also, don't use floorButton[i].add, you don't want to add the button to (what is currently a null element) button, you want to assign it to the position of the array...
public ControlPanel(int x) {
//...
floorButton = new JButton[x];
for (int i = 0; i < x; i++) { // in
floorButton[i] = new JButton("F" + Integer.toString(i));
I'm guessing you'll want to do the same with floorIn...
You have to initialize your floorButton.
floorButton = new JButton [yourCount];
You created the array of JButton, but you didn't create every element in the array. To do this:
Replace this:
floorButton[i].add(new JButton("F" + Integer.toString(i)));
by this:
floorButton[i] = new JButton("F" + Integer.toString(i));

Jbutton event handler not working properly

rollDice.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//generate roll
int roll = dice.getRoll();
addGameFeedMessage(players[1].getName() + " rolled " + roll);
//store the player's position before the roll
int currentPlayerPos = players[1].getPosition();
//update player's position
players[1].movePlayer(roll);
//move the player icon to the player's position
tiles[players[1].getPosition()].addIconCurrentPlayersPanel(players[1].getPlayerIcon());
//revalidate components
//tiles[currentPlayerPos].getCurrentPlayersPanel().revalidate();
//tiles[players[1].getPosition()].getCurrentPlayersPanel().revalidate();
//this loop was put in to see if doing revalidate() on all panels would make a difference but the problem is still there
for(int i = 0; i < tiles.length; i++)
{
tiles[i].getCurrentPlayersPanel().revalidate();
}
}
});
Can anyone explain why the for loop in the actionListener for roll dice doesn't seem to work yet the for loop just above it which is the exact same thing works? There's no actual error as such it just doesn't do anything, however it does get inside the actionListener and executes as we've put print statements etc in it.
Guessing its something strange about actionListeners but I'm not sure what.
Any assistance would be appreciated, thankyou.
You should call validate() after the for loop as follows:
for(int i = 0; i < getNumOfPlayers(); i++)
{
currentPlayersOnTile[5].add(players[i].getPlayerIcon());
}
validate();
For Example consider the code given below:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class DemoPanels extends JFrame
{
public void createAndShowGUI()
{
final JPanel panel = new JPanel();
getContentPane().add(panel);
JButton button = new JButton("Click");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
for (int i = 0 ; i < 10 ; i++ )
{
JPanel panel1 = new JPanel();
panel1.add(new JButton(String.valueOf(i)));
panel.add(panel1);
}
validate();//comment this line and then compile and execute the code to see the effect
}
});
getContentPane().add(button,BorderLayout.SOUTH);
setVisible(true);
setSize(300,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
DemoPanels dp = new DemoPanels();
dp.createAndShowGUI();
}
});
}
}
NOTE: comment the code validate() and then compile and execute it to see the effect.

autoscroll JScrollPane disturbing

I'm working on a text editor, the main panel is composed of 3 JTextPanes, 2 on the side which show the number of line and common syntax errors, and 1 in the middle for the main edition. The whole stuff is packed in a JScrollPane.
The autoscroll issue appears when the users jump a line (press ENTER), the KeyListeners attached add a new entry in the 2 sides JTextPanes (num and syntax error for the line),
in reaction, the JScrollPane autoscroll in the bottom of the docs, probably to show the new text inserted in the 2 sides JTextPanes.
I partially fix the problem by setting the JScrollBar's position for each new line (added by the user) in my KeyListeners. Using scrollRectToVisible for example, or better by selecting a proper part of text in one of the 2 sides JTextPanes.
However, the final effect is not so great, for each new line the vertical scrollbar oscillates, and we can easily crash the app by pressing ENTER for a few seconds. I've been looking for solutions with a lot of methods of the JScrollPane class and trying AdjustmentListener but unsuccessfully. Would you help me?
PS: Sorry for my English. I am French, our forums suck.
SSCCE great source of inspiration, as this one worked well (couldn't see my problem when running it) it seems that my method actually works, but was not running in the right listener in my real code.
Thanks anyway!
There is the SSCCE, its a simple JScrollPane composed of one central JTextPane for edition and one lateral for the lines number. The placeScroll() method place the scrollbar so the caret in the main JTextPane is in the middle (vertically) when the paneLigne try to push it down.
Bye
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
public class SSCCE extends JFrame {
private JTextPane paneLigne, main;
private String tempchain;
public SSCCE() {
this.setSize(500,500);
this.setTitle("S");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
paneLigne = new JTextPane();
paneLigne.setEditable(false);
tempchain = "";
for(int j=1; j<40; j++)
tempchain+=" " + j + " \n";
paneLigne.setText(tempchain);
main = new JTextPane();
main.addKeyListener(new KeyListener() {
private int lastnline=0;
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode()==10) {
String tecste=main.getText();
int n=nbLignes(tecste);
if(n>38) {
if(lastnline<n) {
paneLigne.setText(paneLigne.getText()+" " + (n+1) + " \n");
} else {
this.retablirLignes(tecste);
}
} else {
paneLigne.setText(tempchain);
}
lastnline=n;
this.placeScroll();
}
}
#Override
public void keyReleased(KeyEvent arg0) { }
#Override
public void keyTyped(KeyEvent arg0) { }
private int nbLignes(String str) {
int ret=1;
for(int n=0, cpt=0; (n=str.indexOf('\n', cpt))!=-1; cpt=n+1)
ret++;
return ret;
}
public void retablirLignes(String stret) {
int n=this.nbLignes(stret);
String retoure="";
if(n>=40) {
for(int j=1; j<n+2; j++)
retoure+=" " + j + " \n";
paneLigne.setText(retoure);
}
lastnline=n;
}
public void placeScroll() {
// TODO Auto-generated method stub
if(paneLigne!=null) {
int n=this.nbLignesBuen(main.getText().substring(0, main.getCaretPosition()));
if(n!=-1) {
paneLigne.select(paneLigne.getText().indexOf(""+n), n+1);
} else {
paneLigne.select(0,1);
}
}
}
private int nbLignesBuen(String str) { //return the index of the last 20th line
int ret=0;
for(int n, cpt=0; (n=str.indexOf('\n', cpt))!=-1; cpt=n+1)
ret++;
if(ret>20)
ret-=20;
else
ret=-1;
return ret;
}
});
JPanel contentpane=new JPanel(new BorderLayout());
contentpane.add(paneLigne, BorderLayout.WEST);
contentpane.add(main, BorderLayout.CENTER);
this.setContentPane(new JScrollPane(contentpane));
this.setVisible(true);
}
public static void main(String[] args) {
SSCCE fen = new SSCCE();
}
}

Best way of Changing the background of JButtons

Right now i change the background color of a button by using
button.setBackground(Color.WHITE);
That being an example.
But when i have a massive grid out of jbuttons (1000+), just running a for loop to change every buttons background is very, very slow. You can see the grid slowly turning white, box by box. I really don't want this
Is there a better way of changing every JButton on the grid to the same color at the same time?
This is how i am making the grid, the numbers used are only for example...
grid = new JPanel(new GridLayout(64, 64, 0, 0));
That's 4096 buttons, takes about 30+ seconds to change every button to the same color.
Edit 1: I need the buttons to be clickable, like when i click a button it turns blue for example. when all of the buttons are clicked, change the color of every button to white. Right now i have that working fine, but it is just slow to change the color of every button.
Edit 2: this is how i am changing the buttons:
new javax.swing.Timer(300, new ActionListener() {
int counter = 0;
public void actionPerformed(ActionEvent e) {
if (counter >= counterMax) {
((Timer) e.getSource()).stop();
}
Color bckgrndColor = (counter % 2 == 0) ? flashColor : Color.white;
for (JButton button : gridButton) {
button.setBackground(bckgrndColor);
}
counter++;
}
}).start();
The fact that you see the boxes being repainted individually indicates that either double buffering is turned off, or that the paint code in the button UI makes use of paintImmediately().
I tested your setup with 64x64 JButtons, an made sure that all UI operations were executed in the EDT (Event Dispatch Thread). I can confirm the effect you saw, changing the background of all buttons took about 1200 ms, with every box repainted immediately.
You can bypass the immediate repaints by setting the grid to non-visible before, and to visible after you changed the backgrounds:
grid.setVisible(false);
for (Component comp : grid.getComponents()) {
comp.setBackground(color);
}
grid.setVisible(true);
This caused the grid to do only one repaint, and reduced the time to ~300ms (factor 4).
This is still too slow for frequent updates, so you're better off with a custom component which draws the grid, or a flyweight container (what trashgod suggested in the comment to your question) if you want allow the grid cells to be arbitrary components.
You can get a considerable benefit if only visible buttons need to be repainted. In the MVC approach shown below, each button listens to a model that defines it's current state. Updating the model is quite fast compared to repainting. Although startup takes a few seconds, I see updates taking < 10 ms. in the steady-state. It's not as scalable as the flyweight pattern used by JTable, illustrated here, but it may serve.
import java.awt.*;
import java.awt.event.*;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import javax.swing.*;
/** #see https://stackoverflow.com/questions/6117908 */
public class UpdateTest {
private static final int ROW = 64;
private static final int COL = 64;
private static final int MAX = COL * ROW;
private final DataModel model = new DataModel(MAX);
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new UpdateTest().create();
}
});
}
void create() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(ROW, COL));
for (int i = 0; i < MAX; i++) {
panel.add(new ViewPanel(model, i));
}
Timer timer = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
long start = System.nanoTime();
model.update();
System.out.println(
(System.nanoTime() - start) / (1000 * 1000));
}
});
JFrame f = new JFrame("JTextTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(panel), BorderLayout.CENTER);
f.setPreferredSize(new Dimension(800, 600));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
timer.start();
}
private static class ViewPanel extends JPanel implements Observer {
private final JButton item = new JButton();
private DataModel model;
private int index;
public ViewPanel(DataModel model, int i) {
this.model = model;
this.index = i;
this.add(item);
item.setText(String.valueOf(i));
item.setOpaque(true);
item.setBackground(new Color(model.get(index)));
model.addObserver(this);
}
#Override
public void update(Observable o, Object arg) {
int value = model.get(index);
item.setBackground(new Color(value));
}
}
private static class DataModel extends Observable {
private final Random rnd = new Random();
private final int[] data;
public DataModel(int n) {
data = new int[n];
fillData();
}
public void update() {
fillData();
this.setChanged();
this.notifyObservers();
}
public int get(int i) {
return data[i];
}
private void fillData() {
for (int i = 0; i < data.length; i++) {
data[i] = rnd.nextInt();
}
}
}
}

Categories