gpt4 book ai didi

java - JPanel GUI 正在 self 更新

转载 作者:行者123 更新时间:2023-12-01 12:57:23 28 4
gpt4 key购买 nike

我正在制作一个简单的点击器类型的游戏。问题是,我的 JPanel 忽略了我设置为每秒更新的 Swing 计时器,而是每毫秒更新一次,即使我删除了计时器也是如此。除了计时器的监听器之外,不会在任何地方调用 Repaint,因此 JPanel 会在没有我的指示的情况下重新绘制自身,有谁知道为什么会发生这种情况?

    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;
}



}

这是代码(不包括 Action 监听器以及变量和按钮的初始化)。

最佳答案

绘画方法仅用于绘画:

  1. 不要调用诸如 setText(....) 之类的方法
  2. 不要调用 setLocation() 等方法

更改组件的属性将导致组件被重新绘制,这可能会导致无限循环。

不要在drawItems()方法中调用super.paintComponent()。除了在 PaintComponent() 方法中调用 super.paintComponent() 之外,永远不要直接调用绘画方法。

不应从paintComponent() 方法中调用playGame() 方法。这又是你的游戏的逻辑。该代码应该从控制游戏的计时器中调用。

关于java - JPanel GUI 正在 self 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23768368/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com