JFrame not wanting to repaint()
Hi, I am wondering if anyone can help me figure out why I cannot get repaint() to execute. Here's my code, it's for a stock simulator program.
package Game;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.AbstractButton;
import MarketPlay.Market;
public class Window extends JFrame implements ActionListener{
Market m = new Market();
Window w;
Grid g1;
JButton buy, sell, nextDay;
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
public Window(){
JFrame frame = new JFrame("Stocks 2.0");
frame.setLayout(new BorderLayout());
frame.setSize(1024, 768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buy = new JButton("Buy");
sell = new JButton("Sell");
nextDay = new JButton("Next Day");
//Listen for actions on buttons 1 and 3.
buy.addActionListener(this);
sell.addActionListener(this);
nextDay.addActionListener(this);
//nextDay.setToolTipText("Click this button to disable the middle button.");
g1 = new Grid();
frame.setJMenuBar(createMenuBar());
frame.add(buy, BorderLayout.WEST);
frame.add(sell, BorderLayout.EAST);
frame.add(nextDay, BorderLayout.NORTH);
frame.add(g1, BorderLayout.CENTER);
frame.setVisible(true);
g1.repaint();
}
public JMenuBar createMenuBar() {
JMenuBar menuBar;
JMenu fileMenu, settingsMenu, helpMenu, durationSubmenu, submenu;
JMenuItem newGameItem, loadGameItem, saveGameItem, saveGameAsItem,
exitItem;
JRadioButtonMenuItem year1rbItem, year5rbItem, year10rbItem, year25rbItem,
year50rbItem;
//Create the menu bar.
menuBar = new JMenuBar();
//Build 'File' Menubar
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(fileMenu);
//Contents of 'File'
newGameItem = new JMenuItem("New Game", KeyEvent.VK_N);
//menuItem.setAccelerator(KeyStroke.getKeyStroke(
//KeyEvent.VK_1, ActionEvent.ALT_MASK));
newGameItem.addActionListener(this);
fileMenu.add(newGameItem);
loadGameItem = new JMenuItem("Load Game", KeyEvent.VK_L);
//menuItem.setAccelerator(KeyStroke.getKeyStroke(
//KeyEvent.VK_1, ActionEvent.ALT_MASK));
loadGameItem.addActionListener(this);
fileMenu.add(loadGameItem);
fileMenu.addSeparator();
saveGameItem = new JMenuItem("Save Game", KeyEvent.VK_1);
//menuItem.setAccelerator(KeyStroke.getKeyStroke(
//KeyEvent.VK_1, ActionEvent.ALT_MASK));
saveGameItem.addActionListener(this);
fileMenu.add(saveGameItem);
saveGameAsItem = new JMenuItem("Save Game As...", KeyEvent.VK_2);
//menuItem.setAccelerator(KeyStroke.getKeyStroke(
//KeyEvent.VK_1, ActionEvent.ALT_MASK));
saveGameAsItem.addActionListener(this);
fileMenu.add(saveGameAsItem);
fileMenu.addSeparator();
exitItem = new JMenuItem("Exit", KeyEvent.VK_2);
//menuItem.setAccelerator(KeyStroke.getKeyStroke(
//KeyEvent.VK_1, ActionEvent.ALT_MASK));
exitItem.addActionListener(this);
fileMenu.add(exitItem);
//Build 'Settings' Menubar
settingsMenu = new JMenu("Settings");
settingsMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(settingsMenu);
durationSubmenu = new JMenu("Duration");
//menuItem.setAccelerator(KeyStroke.getKeyStroke(
//KeyEvent.VK_1, ActionEvent.ALT_MASK));
durationSubmenu.addActionListener(this);
settingsMenu.add(durationSubmenu);
ButtonGroup group = new ButtonGroup();
year1rbItem = new JRadioButtonMenuItem("1 Year");
year1rbItem.setSelected(true);
year1rbItem.setMnemonic(KeyEvent.VK_R);
group.add(year1rbItem);
durationSubmenu.add(year1rbItem);
year5rbItem = new JRadioButtonMenuItem("5 Years");
year5rbItem.setMnemonic(KeyEvent.VK_R);
group.add(year5rbItem);
durationSubmenu.add(year5rbItem);
year10rbItem = new JRadioButtonMenuItem("10 Years");
year10rbItem.setMnemonic(KeyEvent.VK_R);
group.add(year10rbItem);
durationSubmenu.add(year10rbItem);
year25rbItem = new JRadioButtonMenuItem("25 Years");
year25rbItem.setMnemonic(KeyEvent.VK_R);
group.add(year25rbItem);
durationSubmenu.add(year25rbItem);
year50rbItem = new JRadioButtonMenuItem("50 Years");
year50rbItem.setMnemonic(KeyEvent.VK_R);
group.add(year50rbItem);
durationSubmenu.add(year50rbItem);
//menu.getAccessibleContext().setAccessibleDescription(
//"This menu does nothing");
/*
menu.addSeparator();
ButtonGroup group = new ButtonGroup();
rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
rbMenuItem.setSelected(true);
rbMenuItem.setMnemonic(KeyEvent.VK_R);
group.add(rbMenuItem);
menu.add(rbMenuItem);
rbMenuItem = new JRadioButtonMenuItem("Another one");
rbMenuItem.setMnemonic(KeyEvent.VK_O);
group.add(rbMenuItem);
menu.add(rbMenuItem);
//a submenu
menu.addSeparator();
submenu = new JMenu("A submenu");
submenu.setMnemonic(KeyEvent.VK_S);
menuItem = new JMenuItem("An item in the submenu");
menuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_2, ActionEvent.ALT_MASK));
submenu.add(menuItem);
menuItem = new JMenuItem("Another item");
submenu.add(menuItem);
menu.add(submenu);
//Build 'Edit' Menubar
menu = new JMenu("Another Menu");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"This menu does nothing");
menuBar.add(menu);
*/
//Build 'Help' Menubar
helpMenu = new JMenu("Help");
helpMenu.setMnemonic(KeyEvent.VK_F);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
return menuBar;
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if(str.equals("New Game")){
}
else if(str.equals("Load Game")){
}
else if(str.equals("Save Game")){
}
else if(str.equals("Save Game As...")){
}
else if(str.equals("Exit")){
}
else if(str.equals("1 Year")){
}
else if(str.equals("5 Years")){
}
else if(str.equals("10 Years")){
}
else if(str.equals("25 Years")){
}
else if(str.equals("50 Years")){
}
else if(str.equals("Buy")){
}
else if(str.equals("Sell")){
}
else if (str.equals("Next Day")){
m.day();
System.out.print(m.weekCurrent);
}
g1.repaint();
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Window();
}
});
}
/*class Buttons extends JPanel implements ActionListener{
Market m = new Market();
Grid g1 = new Grid();
Window w = new Window();
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
}
}*/
}
class Grid extends JPanel{
Market m = new Market();
Thread t;
int i = 30;
int a = 10;
int b = a + 150;
int c = b + 120;
int d = c + 150;
int e = d + 200;
int count;
Grid(){
setBorder(null);
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("Cash:", a, i);
g.drawString("Net Worth:", b, i);
g.drawString("DOWJONES:", c, i);
g.drawString("NASDAQ:", d, i);
g.drawString("Week:", e, i);
g.drawString("" + m.cash, a, i * 2);
g.drawString("" + m.dowjones(), b, i * 2);
g.drawString("" + m.nasdaq(), c, i * 2);
g.drawString("" + m.NetWorth(), d, i * 2);
g.drawString("" + m.weekCurrent, e, i * 2);
g.drawString("Symbol", a, i * 3);
g.drawString(m.stocks[0].symbol, a, i * 4);
g.drawString(m.stocks[1].symbol, a, i * 5);
g.drawString(m.stocks[2].symbol, a, i * 6);
g.drawString(m.stocks[3].symbol, a, i * 7);
g.drawString(m.stocks[4].symbol, a, i * 8);
g.drawString(m.stocks[5].symbol, a, i * 9);
g.drawString(m.stocks[6].symbol, a, i * 10);
g.drawString(m.stocks[7].symbol, a, i * 11);
g.drawString(m.stocks[8].symbol, a, i * 12);
g.drawString(m.stocks[9].symbol, a, i * 13);
g.drawString(m.funds[0].symbol, a, i * 14);
g.drawString(m.funds[1].symbol, a, i * 15);
g.drawString(m.funds[2].symbol, a, i * 16);
g.drawString(m.funds[3].symbol, a, i * 17);
g.drawString(m.funds[4].symbol, a, i * 18);
g.drawString(m.funds[5].symbol, a, i * 19);
g.drawString("Shares", b, i * 3);
g.drawString("" + m.stocks[0].shares, b, i * 4);
g.drawString("" + m.stocks[1].shares, b, i * 5);
g.drawString("" + m.stocks[2].shares, b, i * 6);
g.drawString("" + m.stocks[3].shares, b, i * 7);
g.drawString("" + m.stocks[4].shares, b, i * 8);
g.drawString("" + m.stocks[5].shares, b, i * 9);
g.drawString("" + m.stocks[6].shares, b, i * 10);
g.drawString("" + m.stocks[7].shares, b, i * 11);
g.drawString("" + m.stocks[8].shares, b, i * 12);
g.drawString("" + m.stocks[9].shares, b, i * 13);
g.drawString("" + m.funds[0].shares, b, i * 14);
g.drawString("" + m.funds[1].shares, b, i * 15);
g.drawString("" + m.funds[2].shares, b, i * 16);
g.drawString("" + m.funds[3].shares, b, i * 17);
g.drawString("" + m.funds[4].shares, b, i * 18);
g.drawString("" + m.funds[5].shares, b, i * 19);
g.drawString("Change", c, i * 3);
g.drawString("" + m.stocks[0].change(), c, i * 4);
g.drawString("" + m.stocks[1].change(), c, i * 5);
g.drawString("" + m.stocks[2].change(), c, i * 6);
g.drawString("" + m.stocks[3].change(), c, i * 7);
g.drawString("" + m.stocks[4].change(), c, i * 8);
g.drawString("" + m.stocks[5].change(), c, i * 9);
g.drawString("" + m.stocks[6].change(), c, i * 10);
g.drawString("" + m.stocks[7].change(), c, i * 11);
g.drawString("" + m.stocks[8].change(), c, i * 12);
g.drawString("" + m.stocks[9].change(), c, i * 13);
g.drawString("" + m.funds[0].change(), c, i * 14);
g.drawString("" + m.funds[1].change(), c, i * 15);
g.drawString("" + m.funds[2].change(), c, i * 16);
g.drawString("" + m.funds[3].change(), c, i * 17);
g.drawString("" + m.funds[4].change(), c, i * 18);
g.drawString("" + m.funds[5].change(), c, i * 19);
g.drawString("Price", d, i * 3);
g.drawString("" + m.stocks[0].price, d, i * 4);
g.drawString("" + m.stocks[1].price, d, i * 5);
g.drawString("" + m.stocks[2].price, d, i * 6);
g.drawString("" + m.stocks[3].price, d, i * 7);
g.drawString("" + m.stocks[4].price, d, i * 8);
g.drawString("" + m.stocks[5].price, d, i * 9);
g.drawString("" + m.stocks[6].price, d, i * 10);
g.drawString("" + m.stocks[7].price, d, i * 11);
g.drawString("" + m.stocks[8].price, d, i * 12);
g.drawString("" + m.stocks[9].price, d, i * 13);
g.drawString("" + m.funds[0].price(), d, i * 14);
g.drawString("" + m.funds[1].price(), d, i * 15);
g.drawString("" + m.funds[2].price(), d, i * 16);
g.drawString("" + m.funds[3].price(), d, i * 17);
g.drawString("" + m.funds[4].price(), d, i * 18);
g.drawString("" + m.funds[5].price(), d, i * 19);
g.drawString("News", e, i * 3);
g.drawString(m.stocks[0].news, e, i * 4);
g.drawString(m.stocks[1].news, e, i * 5);
g.drawString(m.stocks[2].news, e, i * 6);
g.drawString(m.stocks[3].news, e, i * 7);
g.drawString(m.stocks[4].news, e, i * 8);
g.drawString(m.stocks[5].news, e, i * 9);
g.drawString(m.stocks[6].news, e, i * 10);
g.drawString(m.stocks[7].news, e, i * 11);
g.drawString(m.stocks[8].news, e, i * 12);
g.drawString(m.stocks[9].news, e, i * 13);
g.drawString("Market Trend", e, i * 14);
g.drawString("" + m.marketTrend, e, i * 15);
g.drawString("News", e, i * 16);
g.drawString("News", e, i * 17);
g.drawString("News", e, i * 18);
g.drawString("News", e, i * 19);
t = new Thread();
//count = 0;
t.start();
}
public void run() {
while (true) {
repaint();
}
}
}
If anyone can help I will greatly appreciate it.

