Reputation: 13
I am making a simple clicker type game. The problem is, my JPanel is ignoring the Swing Timer I have set to it to update every second and is instead updating every millisecond on it's own, even if I remove the timer. Repaint is not called anywhere except the timer's listener, so the JPanel is repainting itself without my instruction, does anyone know why this would be happening?
public GameGUI() {
sectors.add(new Sector());
add(recourseMonitor);
recourseMonitor.setOpaque(false);
recourseMonitor.setFocusable(false);
add(buyHShip);
buyHShip.setToolTipText("Extracts Hydrogren directly from the core of a star.");
buyHShip.addActionListener(this);
add(buyCShip);
buyCShip.setToolTipText("Extracts Carbon from indigenous lifeforms.");
buyCShip.addActionListener(this);
add(buyIShip);
add(buyUShip);
buyUShip.setToolTipText("Extracts Uranium from nuclear waste leftovers.");
buyUShip.addActionListener(this);
buyIShip.setToolTipText("Strip mines nearby planets for Iron.");
buyIShip.addActionListener(this);
add(buyAShip);
buyAShip.setToolTipText("Collects Antimatter from the fabric of the universe.");
buyAShip.addActionListener(this);
add(buyCookieShip);
buyCookieShip.setToolTipText("Rips holes in the space-time continuum to "
+ "bring back cookies from an ancient civilization.");
buyCookieShip.addActionListener(this);
add(buyTShip);
buyTShip.addActionListener(this);
add(cashButton);
cashButton.addActionListener(this);
add(sellHplus);
sellHplus.addActionListener(this);
add(sellCplus);
sellCplus.addActionListener(this);
add(sellIplus);
sellIplus.addActionListener(this);
add(sellUplus);
sellUplus.addActionListener(this);
add(sellAplus);
sellAplus.addActionListener(this);
add(sellCookieplus);
sellCookieplus.addActionListener(this);
add(sellHminus);
sellHminus.addActionListener(this);
add(sellCminus);
sellCminus.addActionListener(this);
add(sellIminus);
sellIminus.addActionListener(this);
add(sellUminus);
sellUminus.addActionListener(this);
add(sellAminus);
sellAminus.addActionListener(this);
add(sellCookieminus);
sellCookieminus.addActionListener(this);
add(hBeingSold);
add(cBeingSold);
add(iBeingSold);
add(uBeingSold);
add(aBeingSold);
add(cookiesBeingSold);
if (debug == true) {
add(testButton);
testButton.addActionListener(this);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
buyHShip.setText("[" + sectors.get(0).getStation().gethShip() + "] "
+ "Buy Hydrogen Extractor: " + hPrice);
buyCShip.setText("[" + sectors.get(0).getStation().getcShip() + "] "
+ "Buy Carbon Miner: " + cPrice);
buyUShip.setText("[" + sectors.get(0).getStation().getuShip() + "] "
+ "Buy Uranium Miner: " + uPrice);
buyIShip.setText("[" + sectors.get(0).getStation().getiShip() + "] "
+ "Buy Iron Miner: " + iPrice);
buyAShip.setText("[" + sectors.get(0).getStation().getaShip() + "] "
+ "Buy Antimatter Collector: " + aPrice);
buyCookieShip.setText("[" + sectors.get(0).getStation().getcChip() + "] "
+ "Buy Cookie Drone: " + cookiePrice);
buyTShip.setText("[" + sectors.get(0).getStation().gettShip() + "] "
+ "Buy Trading Ship: " + tradePrice);
hBeingSold.setText("Hydrogen being sold: " + tradeH);
cBeingSold.setText("Carbon being sold: " + tradeC);
iBeingSold.setText("Iron being sold: " + tradeI);
uBeingSold.setText("Uranium being sold: " + tradeU);
aBeingSold.setText("Antimatter being sold: " + tradeA);
cookiesBeingSold.setText("Cookies being sold: " + tradeCookie);
recourseMonitor.setText("Space Cash: " + spaceCash
+ "\nEnergy: " + energy
+ "\nHydrogen: " + hydrogen
+ "\nCarbon: " + carbon
+ "\nUranium: " + uranium
+ "\nIron: " + iron
+ "\nAntimatter: " + antimatter
+ "\nCookies: " + cookies);
drawItems(g);
playGame();
}
public void drawItems(Graphics g) {
super.paintComponent(g);
recourseMonitor.setLocation(5, 0);
buyHShip.setLocation(getWidth() - buyHShip.getWidth(), 0);
buyCShip.setLocation(getWidth() - buyCShip.getWidth(), buyCShip.getHeight() * 2);
buyIShip.setLocation(getWidth() - buyIShip.getWidth(), buyIShip.getHeight() * 4);
buyUShip.setLocation(getWidth() - buyUShip.getWidth(), buyUShip.getHeight() * 6);
buyAShip.setLocation(getWidth() - buyAShip.getWidth(), buyAShip.getHeight() * 8);
buyCookieShip.setLocation(getWidth() - buyCookieShip.getWidth(), buyCookieShip.getHeight() * 10);
buyTShip.setLocation(getWidth() - buyTShip.getWidth(), buyTShip.getHeight() * 12);
cashButton.setLocation(getWidth() / 2 - cashButton.getWidth() / 2, 10);
testButton.setLocation(getWidth() / 2 - testButton.getWidth() / 2, testButton.getHeight() + 20);
sellHplus.setLocation(5, 200);
hBeingSold.setLocation(sellHplus.getWidth() + 20, 200);
sellHminus.setLocation(sellHplus.getWidth() + hBeingSold.getWidth() + 40, 200);
sellCplus.setLocation(5, 250);
cBeingSold.setLocation(sellCplus.getWidth() + 20, 250);
sellCminus.setLocation(sellCplus.getWidth() + cBeingSold.getWidth() + 40, 250);
sellIplus.setLocation(5, 300);
iBeingSold.setLocation(sellIplus.getWidth() + 20, 300);
sellIminus.setLocation(sellIplus.getWidth() + iBeingSold.getWidth() + 40, 300);
sellUplus.setLocation(5, 350);
uBeingSold.setLocation(sellUplus.getWidth() + 20, 350);
sellUminus.setLocation(sellUplus.getWidth() + uBeingSold.getWidth() + 40, 350);
sellAplus.setLocation(5, 400);
aBeingSold.setLocation(sellAplus.getWidth() + 20, 400);
sellAminus.setLocation(sellAplus.getWidth() + aBeingSold.getWidth() + 40, 400);
sellCookieplus.setLocation(5, 450);
cookiesBeingSold.setLocation(sellCookieplus.getWidth() + 20, 450);
sellCookieminus.setLocation(sellCookieplus.getWidth() + cookiesBeingSold.getWidth() + 40, 450);
}
public void playGame() {
for (int i = 0; i < sectors.size(); i++) {
hIncome = sectors.get(i).getStation().gethShip() - tradeH;
cIncome = sectors.get(i).getStation().getcShip() - tradeC;
iIncome = sectors.get(i).getStation().getiShip() - tradeI;
uIncome = sectors.get(i).getStation().getuShip() - tradeU;
aIncome = sectors.get(i).getStation().getaShip() - tradeA;
cookieIncome = sectors.get(i).getStation().getcChip() - tradeCookie;
cashIncome = (tradeH * 5) + (tradeC * 20) + (tradeI * 100)
+ (tradeU * 500) + (tradeA * 1500) + (tradeCookie * 5000);
}
for (int i = 0; i < sectors.size(); i++) {
spaceCash += cashIncome;
energy += energyIncome;
hydrogen += hIncome;
carbon += cIncome;
iron += iIncome;
uranium += uIncome;
antimatter += aIncome;
cookies += cookieIncome;
}
}
There's the code (excluding the action listener and initialization of variables and buttons).
Upvotes: 0
Views: 44
Reputation: 324147
Painting methods are for painting only:
setText(....)
setLocation()
Changing properties on a component will cause the component to be repainted which may cause an infinite loop.
Don't invoke super.paintComponent() in the drawItems() method. You should never invoke a painting method directly except to invoke super.paintComponent() in the paintComponent() method.
The playGame() method should not be invoked from the paintComponent() method. Again that is logic for your game. That code should be invoked from your Timer that controls the game.
Upvotes: 1