使用PaintComponent更新值

时间:2014-03-06 00:10:35

标签: java swing paintcomponent

我一直在制作一个配色游戏,我决定制作另一个窗口来跟踪用户在重置之前剩余的移动次数,以及用于跟踪a的计时器最终获得高分。

这是我第一次使用Graphics的经历,我似乎无法获得我的计数器值在窗口中更新,它保持在它的初始值4移动。我认为计时器将同样存在保持初始值的问题。

在Gridworld内部,计数器更新它应该如何。有人可以解释为什么计数器不会在我的分数窗口内更新?我确定这是显而易见的事情,但我在过去一小时内一直主演这段代码而无法理解。

TileGame Class

import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;
import info.gridworld.world.World;
import java.awt.Color;
import java.util.ArrayList;

public class TileGame extends World<TileCritter> {
public TileGame() {
    Color[] colors = { Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY,
            Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
            Color.PINK, Color.RED };
    for (Color color : colors) {
        add(getRandomEmptyLocation(), new TileCritter(color));
        add(getRandomEmptyLocation(), new TileCritter(color));
    }
    setMessage("Click on the first tile to reveal its color. Click on a second to try to find a match. \n"
            + "If you don't find a match within 3 turns, all matches will be reset and the board shuffled.");
    first = true;
}

public boolean locationClicked(Location loc) {
    Grid<TileCritter> gr = getGrid();
    TileCritter t = gr.get(loc);
    if (t != null) {
        t.flip();
        if (first) {
            if (firstTile != null) {
                firstTile.flip();
                secondTile.flip();
            }
            firstTile = t;
            setMessage("Turns until reset: " + (counter - 1));
            first = false;
        } else {
            if (firstTile.getColor().equals(t.getColor())) {
                firstTile = secondTile = null;
                counter = 4;
            } else {
                secondTile = t;
            }
            setMessage("Turns until reset: " + (counter - 1));
            first = true;
            counter--;
            shuffle(gr, t);
        }
    }
    return true;
}

public void shuffle(Grid<TileCritter> gr, TileCritter t) {
    if (counter == 0) {
        ArrayList<Location> locs = gr.getOccupiedLocations();
        for (Location l : locs) {
            gr.remove(l);

        }
        Color[] colors = { Color.BLUE, Color.CYAN, Color.DARK_GRAY,
                Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA,
                Color.ORANGE, Color.PINK, Color.RED };
        for (Color color : colors) {
            add(getRandomEmptyLocation(), new TileCritter(color));
            add(getRandomEmptyLocation(), new TileCritter(color));
        }
        counter = 4;
    }
}

public static void main(String[] args) {
    new Score();
    new TileGame().show();
    Score main = new Score();
    main.display();
}

private TileCritter firstTile;
private TileCritter secondTile;
private boolean first;
public static int counter = 4;

}

得分等级

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Date;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Score extends JFrame {

private static final long serialVersionUID = 1L;


public Score() {
    setTitle("Score");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void display() {
    add(new CustomComponent());
    pack();        
    setMinimumSize(getSize());
    setVisible(true);
    setResizable(false);
}
}

class CustomComponent extends JComponent {

private static final long serialVersionUID = 1L;
long startTime = System.currentTimeMillis();
long elapsedTime = 0L;

@Override
public Dimension getMinimumSize() {
    return new Dimension(100, 100);
}

@Override
public Dimension getPreferredSize() {
    int width = 450;
    int height = 150;
    return new Dimension(width, height);
}

public long timer() {
     while (elapsedTime < 2*60*1000) {
            elapsedTime = (new Date()).getTime() - startTime;
        }
     return elapsedTime;
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setFont(new Font("SansSerif", Font.BOLD, 16 ));
    g.drawString("Time", 75, 50);
    g.drawString("Moves Remaining", 250, 50);
    String paintCounter = String.valueOf(TileGame.counter);
    g.drawString(paintCounter, 310, 75);
}
}

0 个答案:

没有答案