Swing Timers and Animations in JPanel - java

Im trying to animate 2 boxes to go from the top right to the bottom left of a JPanel. For the animation I'm using a Swing Timer and SwingUtilities.invokeLater(). The problem is that when I click the start button. It only animates and moves the blue box, but not the red one.
Heres the code:
//import neccessary stuff
public class Example extends JFrame {
public static void main(String[] args) {
Example e = new Example();
e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
e.setSize(600, 565);
e.setVisible(true);
}</code>
private JButton startButton = new JButton("Start");
private JPanel theTable = new table();
public Example() {
add(startButton, BorderLayout.SOUTH);
add(theTable, BorderLayout.CENTER);
Handler handler = new Handler();
startButton.addActionListener(handler);
}
public ArrayList<Integer> xPos, yPos;
final int START_POSITION_X = 470;
final int START_POSITION_Y = 10;
final int[] END_POSITION_X = {70, 87};
final int END_POSITION_Y = 160;
private class table extends JPanel {
public table() {
xPos = new ArrayList<Integer>();
yPos = new ArrayList<Integer>();
xPos.add(START_POSITION_X); //default position for box1
yPos.add(START_POSITION_Y);
xPos.add(START_POSITION_X); //default position for box2
yPos.add(START_POSITION_Y);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(new Color(-16756217));
g.setColor(Color.RED);
g.fillRect(xPos.get(1), yPos.get(1), 89, 129);
g.setColor(Color.BLUE);
g.fillRect(xPos.get(0), yPos.get(0), 89, 129);
if (isAnimating) {
animator.start();
} else {
animator.stop();
isAnimating = false;
}
}
}
private class Handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
Runnable r1 = new Runnable() {
#Override
public void run() {
animateCard(0, END_POSITION_X[0], END_POSITION_Y);
}
};
SwingUtilities.invokeLater(r1);
animateCard(1, END_POSITION_X[1], END_POSITION_Y);
}
}
public void animateCard(int card, int xDest, int yDest) {
cardID = card;
xDestination = xDest;
yDestination = yDest;
totalXDistance = Math.abs(xDestination - START_POSITION_X);
totalYDistance = Math.abs(yDestination - START_POSITION_Y);
animator.start();
}
int cardID;
int xDestination, yDestination, totalXDistance, totalYDistance;
boolean isAnimating = false;
Timer animator = new Timer(15, new ActionListener() {
int startVel = 20;
public void actionPerformed(ActionEvent e) {
double xRelDistance, xAbsDistance, xVel;
int xRealVel;
xAbsDistance = xDestination - xPos.get(cardID);
xRelDistance = xAbsDistance / totalXDistance;
xVel = startVel * xRelDistance;
double yRelDistance, yAbsDistance, yVel;
yAbsDistance = yDestination - yPos.get(cardID);
yRelDistance = yAbsDistance / totalYDistance;
yVel = startVel * yRelDistance;
if (xVel > 0) {
xRealVel = (int) java.lang.Math.ceil(xVel);
} else {
xRealVel = (int) java.lang.Math.floor(xVel);
}
xPos.set(cardID, xPos.get(cardID) + xRealVel);
int yRealVel;
if (xVel > 0) {
yRealVel = (int) java.lang.Math.ceil(yVel);
yPos.set(cardID, yPos.get(cardID) + yRealVel);
} else {
yRealVel = (int) java.lang.Math.floor(yVel);
}
yPos.set(cardID, yPos.get(cardID) + yRealVel);
if ((xPos.get(cardID) == xDestination) && (yPos.get(cardID) == yDestination)) {
isAnimating = false;
} else {
isAnimating = true;
}
repaint();
}
});
}

So all of those variable declared at the class level are shared... Your first call to animateCard will set them and then your second call to animateCard is going to completely overwrite the previous values. You need to change them from class variables to parameters of the animation.
Create a new class AnimationTask that implements ActionListener and save the variables in that class.
E.g.,
class AnimationTask implements ActionListener {
private int cardID;
private int xDest;
private int yDest;
private int totalXDistance;
private int totalYDistance;
public AnimationTask(int cardID, int xDest, int yDest) {
this.cardID = cardID;
this.xDest = xDest;
this.yDest = yDest;
this.totalXDistance = Math.abs(xDestination - START_POSITION_X);
this.totalYDistance = Math.abs(yDestination - START_POSITION_Y);
}
public void actionPerformed(ActionEvent e) {
// do your animation logic...
}
}
and use that custom class in your "animator"
E.g.,
Timer animator = new Timer(15, new AnimationTask(cardId, xDest, yDest);

put
animateCard(1, END_POSITION_X[1], END_POSITION_Y);
inside your run method:
public void run() {
animateCard(0, END_POSITION_X[0], END_POSITION_Y);
}

Related

why applyLinearImpulse is not working, using libgdx?

I need some help when I try make a game, which is controlled by commands. I need to use textarea and I try to pass the value from textarea to contructer. But my game is not working. I know what mistake is appeared because I call contructer 2 times. I add some code:
public class inputs extends Game implements Disposable {
private Skin skin;
private Viewport viewport;
public Stage stage;
private Table table;
private TextButton button;
public TextArea TF;
private Main game;
public SpriteBatch spriteBatch;
public inputs(final SpriteBatch sb, final Main game){
viewport = new FitViewport(1200, 624, new OrthographicCamera());
stage = new Stage(viewport, sb);
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
button = new TextButton("Send", skin, "default");
TF = new TextArea("", skin);
TF.setWidth(300);
TF.setHeight(570);
TF.setPosition(0,54);
button.setWidth(300);
button.setHeight(54);
button.setPosition(0, 0);
button.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
spriteBatch = new SpriteBatch();
setScreen(new MainCodeProgram(game,TF.getText(),spriteBatch));
TF.setText("");
button.setTouchable(Touchable.disabled);
}
});
stage.addActor(TF);
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
Gdx.input.setInputProcessor(stage);
}
public void setButtonTouchable(){
button.setTouchable(Touchable.enabled);
}
#Override
public void create() {
}
#Override
public void dispose() {
stage.dispose();
}
}
Main.java :
public class Main extends Game {
public SpriteBatch spriteBatch;
public static int Width = 400;
public static int Height = 208;
public static final short objectwhit = 32;
public static final short mariowhit= 2;
public static final short itemwhit = 256;
public static float PPM = 100;
public static final short marioheadwhit = 512;
public static final short blockwhit = 4;
public static final short coinwhit = 8;
public static final short nothing = 0;
public static final short ground = 1;
public static final short destroyedwhit = 16;
public static final short enemywhit = 64;
public static final short enemyheadwhit = 128;
public static inputs inp1;
#Override
public void create () {
spriteBatch = new SpriteBatch();
inp1 = new inputs(spriteBatch,this);
setScreen(new MainCodeProgram(this,null,spriteBatch));
}
#Override
public void dispose() {
super.dispose();
spriteBatch.dispose();
}
}
Pseudocode recognize :
public class MainCodeProgram extends JFrame implements Screen, TextInputListener, Disposable {
static ArrayList<String> firstintkint = new ArrayList<String>();
static ArrayList<Integer> firstintvalue = new ArrayList<Integer>(); //int
private Main game;
private OrthographicCamera orthographicCamera;
private TextureAtlas textureAtlas;
public static int whichlevel=1;
private Mario player;
public Stage stage;
private Hud hud;
private TmxMapLoader maploader;
private TiledMap tilemap;
static int walks = 0;
private OrthogonalTiledMapRenderer renderer;
private Viewport viewport;
private World world;
private Array<Item> items;
private LinkedBlockingQueue<ItemVector> itemstoSpawn;
String text="";
private Box2DDebugRenderer b2dr;
private OtherWorldCreator otherWorldCreator;
public String kodas;
public boolean b =false;
public MainCodeProgram(){
}
public MainCodeProgram(Main game, String text,SpriteBatch spriteBatch) {
if(text == null){
textureAtlas = new TextureAtlas("All.pack");
this.game= game;
orthographicCamera = new OrthographicCamera();
viewport = new FitViewport(Main.Width / Main.PPM, Main.Height / Main.PPM, orthographicCamera);
viewport.apply();
hud = new Hud(game.spriteBatch);
newlevel();
otherWorldCreator = new OtherWorldCreator(this);
player = new Mario( this);
world.setContactListener(new WorldContact());
items = new Array<Item>();
itemstoSpawn = new LinkedBlockingQueue<ItemVector>();
}
if(text != null){
b = true;
textureAtlas = new TextureAtlas("All.pack");
this.game= game;
orthographicCamera = new OrthographicCamera();
viewport = new FitViewport(Main.Width / Main.PPM, Main.Height / Main.PPM, orthographicCamera);
viewport.apply();
hud = new Hud(spriteBatch);
newlevel();
otherWorldCreator = new OtherWorldCreator(this);
player = new Mario( this);
world.setContactListener(new WorldContact());
items = new Array<Item>();
itemstoSpawn = new LinkedBlockingQueue<ItemVector>();
input(text);
}
}
public TextureAtlas getAtlas(){return textureAtlas;}
#Override
public void show() {}
int i = 0, kur = 1;
public void handleInput(float dt) {
if (player.currentstate!= Mario.State.died) {
;
}
}
public void spawnItem(ItemVector idef)
{
itemstoSpawn.add(idef);
}
public void handleSpawningItems(){
if(!itemstoSpawn.isEmpty())
{
ItemVector idef = itemstoSpawn.poll();
if(idef.type == Mushroom.class){items.add(new Mushroom(this, idef.position.x,idef.position.y));}
}
}
public void update(float dt)
{
handleInput(dt);
handleSpawningItems();
world.step(1 / 60f, 6, 2);
player.update(dt);
for (Enemy enemy: otherWorldCreator.getEbemies()) {
enemy.update(dt);
if(enemy.getX() < player.getX() + 224 / Main.PPM){enemy.b2body.setActive(true);}
}
for(Item item : items){item.update(dt);}
hud.refresh(dt);
if(player.currentstate!= Mario.State.died){orthographicCamera.position.x = player.b2body.getPosition().x;}
orthographicCamera.update();
renderer.setView(orthographicCamera);
}
public boolean gameOver()
{
if(player.currentstate== Mario.State.died && palyer.getStateTimer() > 3){return true;}
if(whichlevel== 5){return true;}
else if(player.getY() <0){return true;}
else{return false;}
}
public boolean Youwonn()
{
if(whichlevel==4){return true;}
else{return false;}
}
#Override
public void render(float delta) {
update(delta);
int k = 0;
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, orthographicCamera.combined);
game.spriteBatch.setProjectionMatrix(orthographicCamera.combined);
game.spriteBatch.begin();
game.draw(game.spriteBatch);
for (Enemy enemy: otherWorldCreator.getEbemies())
enemy.draw(game.spriteBatch);
for(Item item : items)
item.draw(game.spriteBatch);
game.spriteBatch.end();
game.spriteBatch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
if(gameOver())
{
whichlevel= 1;
game.setScreen(new GameOverWorld(game));
dispose();
}
if ((player.getX() > 15 && whichlevel< 4)) {
whichlevel++;
if(whichlevel!=4){
game.setScreen(new MainCodeProgram(game,"",""));
dispose();
}
}
if (whichlevel==4)
{
game.setScreen(new WinWindow(game));
dispose();
}
Main.inp1.stage.draw();
}
#Override
public void resize(int width, int height) {viewport.update(width,height);}
public World getWorld(){ return world;}
public TiledMap getMap(){return tilemap;}
#Override
public void pause() {}
#Override
public void resume() {}
#Override
public void hide() {}
public void newlevel()
{
maploader = new TmxMapLoader();
if(whichlevel== 1){tilemap = maploader.load("mario1lvl.tmx");
}
if (whichlevel== 2){tilemap = maploader.load("mario2lvl.tmx");}
if (whichlevel== 3){tilemap = maploader.load("mario3lvl.tmx");}
if (whichlevel== 4){
game.setScreen(new WinWindow(game));}
renderer = new OrthogonalTiledMapRenderer(tilemap, 1 / Main.PPM);
orthographicCamera.position.set(viewport.getWorldWidth() / 2, viewport.getWorldHeight() / 2, 0);
world = new World(new Vector2(0, -10 ), true);
b2dr = new Box2DDebugRenderer();
}
#Override
public void dispose() {
tilemap.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
hud.dispose();
}
public void input(String text) {
ArrayList<String> pseudocode = new ArrayList<String>();
this.text = text;
i = 0;
String str = text;
String split[] = str.split("/");
for (String spacebar : split) {
pseudocode.add(spacebar);
i = 0;
}
int foras = 0;
for (int i = 0;i < pseudocode.size(); i++) {
if(pseudocode.get(i).equals("jump")||pseudocode.get(i).equals("jump;")){jump();}
if(pseudocode.get(i).startsWith("int")){recognizeint(pseudocode.get(i));}
if(pseudocode.get(i).startsWith("for"))
{
recognizefor(pseudocode.get(i));
foras++;
}
if(pseudocode.get(i).startsWith("while")){ recognizewhile(pseudocode.get(i));}
if(pseudocode.get(i).endsWith("--")||pseudocode.get(i).endsWith("--;"))
{
if(foras == 0){minuminus(pseudocode.get(i));}
else{foras--;}
}
if(pseudocode.get(i).endsWith("++")||pseudocode.get(i).endsWith("++;"))
{
if(foras==0){plusplus(pseudocode.get(i));}
else{foras--;}
}
if(istheend){
walks = 0;
istheend = false;
}
}
}
#Override
public void canceled() {
}
public void jump()
{
if(istheend){
try {Thread.sleep(1*walks);
} catch (InterruptedException e) {e.printStackTrace();}}
else{try {Thread.sleep(50*walks);
} catch (InterruptedException e) {e.printStackTrace();}}
walks = 0;
if(player.b2body.getLinearVelocity().y == 0){
if(b == true) {
player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true); //Mistake is here
}
}
else if(player.b2body.getLinearVelocity().y != 0){
try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
}
}
}
Mistake is here:
public void jump()
{
if(istheend){
try {Thread.sleep(1*walks);
} catch (InterruptedException e) {e.printStackTrace();}}
else{try {Thread.sleep(50*walks);
} catch (InterruptedException e) {e.printStackTrace();}}
walks = 0;
if(player.b2body.getLinearVelocity().y == 0){
if(b == true) {
player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true); //Mistake is here
}
}
else if(player.b2body.getLinearVelocity().y != 0){
try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
}
}
Thank you very much for help
I believe your code it is a little confusing, for the ApplyLinearImpulse works in your HandleInput you send a command jump() and inside the Entity you make the jump() method and also to make the animation to join the b2body, with update(dt) method + getState() + getFrame().
Your jump() method should be simple like this
public void jump(){
if(currentState != State.Jumping){
b2body.applyLinearImpulse(new Vector2(0,4f), b2body.getWorldCenter(), true);
currentState=State.JUMPING
}
}
What should be stuck the applyLinearImpulse are the conditions you included. Try to remove it all and leave only the main stuff. Use also LOGS everywhere Gdx.app.log("MESSAGE:", "" + variable); and you can start seeing what is going in and out with your methods and variables, and find your mistake.
Also, if you would like to learn more and don't forget any detail regarding map / viewport / b2bodies, follow this tutorial that I use as my guide for B2Box (Youtube / Github), it is very quick and easy to follow and you are going to love use B2Box for everything :-) and also continue with LibGdx Framework developments.
Click here Youtube Tutorial.
Click here GitHub codes.
I hope it helps you.
Cheers!!

Add MouseListener with reference to other class

I want to add MouseListener to a class which extends JPanel. The Listener references to another class where the JPanel itself is created and added to contentPane. By pressing the mouse on JPanel, it should be removed. It is working well but I cannot call repaint() because my removing methode is static. How I can solve this?
This is the extended class:
public class PanelDraggable extends JPanel {
public PanelDraggable () {
.
.
.
MouseInputAdapter closeMouseAdapter = new CloseMouseHandler();
addMouseListener(closeMouseAdapter);
}
private class CloseMouseHandler extends MouseInputAdapter {
public void mouseClicked(MouseEvent e) {
TblManagement.RemoveTable();
}
}
}
And in this class I want to create the panel and remove it:
public void AddTables() {
String dbShortName = combo.getSelectedItem().toString();
String dbName = prop.get(dbNamesFile, dbShortName);
int x = 50; int y = 150; int width = 150; int height = 220; //set Size and location
int [] indices = tblList.getSelectedIndices();
panel = new PanelDraggable [indices.length];
for (int i = 0; i < indices.length ; i++) {
String tblName = tblList.getModel().getElementAt(i).toString();
String sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE "+
"`TABLE_SCHEMA`='"+dbName+"' AND `TABLE_NAME`='"+tblName+"'";
JList<?> columnslist = new JList<String>();
columnslist.setModel(getListModel(dbName, sql, 1));
JScrollPane sc = new JScrollPane(columnslist);
panel[i] = new PanelDraggable(x, y , width, height, tblName, sc);
MouseInputAdapter mousehandler = new TableMouseHandler();
panel[i].addMouseListener(mousehandler);
mainPanel.add(panel[i]);
x+=15;
y+=20;
}
revalidate();
repaint();
}
private class TableMouseHandler extends MouseInputAdapter {
public void mouseEntered(MouseEvent e) {
removeComp = (JComponent) e.getComponent();
}
}
public static void RemoveTable() {
mainPanel.remove(removeComp);
}
Thank you for help.
You need to take in an instance of the TableManagement class as an argument for each panel, that way you don't have to do it in the static context. Also, I removed the unnecessary sub-classes for your mouseAdapters you were creating and created them in the more accepted way.
public class PanelDraggable extends JPanel {
public PanelDraggable (..., final TableManagement tblManagement) {
.
.
.
addMouseListener(new MouseAdapter() {
#Override
public void (MouseEvent e) {
tblManagement.removeTable();
}
});
}
}
Then in your TableManagement Class
public void AddTables() {
String dbShortName = combo.getSelectedItem().toString();
String dbName = prop.get(dbNamesFile, dbShortName);
int x = 50; int y = 150; int width = 150; int height = 220; //set Size and location
int [] indices = tblList.getSelectedIndices();
panel = new PanelDraggable [indices.length];
for (int i = 0; i < indices.length ; i++) {
String tblName = tblList.getModel().getElementAt(i).toString();
String sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE "+
"`TABLE_SCHEMA`='"+dbName+"' AND `TABLE_NAME`='"+tblName+"'";
JList<?> columnslist = new JList<String>();
columnslist.setModel(getListModel(dbName, sql, 1));
JScrollPane sc = new JScrollPane(columnslist);
// There is an extra argument here. Whatever your
// mainPanel used to be in the static method
panel[i] = new PanelDraggable(x, y , width, height, tblName, sc, mainPanel);
panel[i].addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered() {
removeComp = (JComponent) e.getComponent();
}
});
mainPanel.add(panel[i]);
x+=15;
y+=20;
}
revalidate();
repaint();
}
public void RemoveTable() {
this.remove(removeComp);
}
Side-Note: You should look into Java naming conventions. Only classes have their first character capitalized.

java swing setlocation doesn't work for second time

Frame has mainContentPane with layout manager = null
mainContentPane gamePanel, which holds jLabel Background at z-order 1 and a bunch of backgammon stones at z- order 0, if i set their positions before adding them to game panel, they end up in places i need them to be, but if im trying to set their places in game they all end ap at point 0,0 and calling get location says that its at a different location. Plese help me out on what im doing wrong with swing layout etc. setStones is the void that suppose to do it and getLocation after it ran gives locations I need, but everything is still at 0,0 on the screen
public class MainWindow extends JFrame implements ActionListener, ItemListener,
MouseListener, MouseMotionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private final int NUMBER_OF_STONES = 30;
private JPanel mainContentPane;
private JLayeredPane gamePanel;
private JLabel background;
private JMenuBar menuBar;
private JMenu menuGameTitle, aboutGameTitle;
private JMenuItem newGame, exit, help;
private JLabel[] stones;
private Dimension preferredSize;
private int stoneDragged = NUMBER_OF_STONES;
private int clickX = 0;
private int clickY = 0;
private HashMap<game.Color, String> stoneImage;
private HashMap<Integer, int[]> locations;
private int currStone;
public MainWindow() {
createGUI();
stoneImage = new HashMap<game.Color, String>();
stoneImage.put(game.Color.BLACK, "SilverNSH.jpg");
stoneImage.put(game.Color.WHITE, "GoldNSH.jpg");
locations = new HashMap<Integer, int[]>();
locations.put(0, new int[] {355,509});
locations.put(1, new int[] {655, 509});
.....
currStone = 0;
play();
}
private void createGUI() {
preferredSize = new Dimension(800, 600);
mainContentPane = new JPanel();
mainContentPane.setLayout(null);
mainContentPane.setPreferredSize(preferredSize);
gamePanel = new JLayeredPane();
gamePanel.setBounds(0, 0, 800, 600);
setSize(preferredSize);
background = new JLabel(new ImageIcon("BackGammon.jpg"));
// adding stones;
initStones();
for (int i = 0; i < stones.length; i++) {
gamePanel.add(stones[i], 0);
}
// setting background to 1 Z order
gamePanel.add(background, 1);
// set Z level to 0
for (int i = 0; i < stones.length; i++) {
gamePanel.setComponentZOrder(stones[i], 0);
}
mainContentPane.add(gamePanel);
setContentPane(mainContentPane);
pack();
}
private void initStones() {
stones = new JLabel[NUMBER_OF_STONES];
for (int i = 0; i < stones.length; i++) {
if (i < NUMBER_OF_STONES / 2) {
stones[i] = new JLabel(new ImageIcon("SilverNSH.gif"));
} else {
stones[i] = new JLabel(new ImageIcon("GoldNSH.gif"));
}
stones[i].setName(i + "");
stones[i].setSize(40,40);
stones[i].addMouseListener(this);
stones[i].addMouseMotionListener(this);
}
}
#Override
public void mousePressed(MouseEvent e) {
stoneDragged = Integer.parseInt(e.getComponent().getName());
clickX = e.getX();
clickY = e.getY();
}
#Override
public void mouseReleased(MouseEvent e) {
// no stone is dragged
stoneDragged = NUMBER_OF_STONES;
}
#Override
public void mouseDragged(MouseEvent e) {
// off screen check
int yLoc;
int xLoc;
if (stoneDragged >= 0 && stoneDragged < NUMBER_OF_STONES) {
xLoc = e.getComponent().getX() + e.getX() - clickX;
yLoc = e.getComponent().getY() + e.getY() - clickY;
stones[stoneDragged].setLocation(xLoc, yLoc);
if (stones[stoneDragged].getX() <= 50)
stones[stoneDragged].setLocation(51, 51);
if (stones[stoneDragged].getY() <= 50)
stones[stoneDragged].setLocation(51, 51);
if (stones[stoneDragged].getX() >= 660)
stones[stoneDragged].setLocation(659, 509);
if (stones[stoneDragged].getY() >= 510)
stones[stoneDragged].setLocation(659, 509);
}
}
private void play() {
setStones(board.getAmountArray(), board.getColorArray());
}
private void setStones(int[] amountArray, game.Color[] colorArray) {
currStone = 0;
for (int i = 0; i < Board.NO_OF_PLAYABLE_FIELDS;i++){
if (amountArray[i] != 0) {
placeStones(i, amountArray[i], stoneImage.get(colorArray[i]));
}
}
}
private void placeStones(int startPos, int amount, String picture) {
for (int i = 0; i < amount;i++){
stones[currStone].setIcon(new ImageIcon(picture));;
if (startPos < 13) {
stones[currStone].setLocation(locations.get(startPos)[0], locations.get(startPos)[1] - i*40);
} else {
stones[currStone].setLocation(locations.get(startPos)[0], locations.get(startPos)[1] + i*40);
}
stones[currStone].setSize(40, 40);
currStone++;
}
}
}

Fixing method latency in Java

Another problem, same program:
The following is MainGUI.java
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class MainGUI extends JFrame implements ActionListener
{
private static final long serialVersionUID = 4149825008429377286L;
private final double version = 8;
public static int rows;
public static int columns;
private int totalCells;
private MainCell[] cell;
public static Color userColor;
JTextField speed = new JTextField("250");
Timer timer = new Timer(250,this);
String generationText = "Generation: 0";
JLabel generationLabel = new JLabel(generationText);
int generation = 0;
public MainGUI(String title, int r, int c)
{
rows = r;
columns = c;
totalCells = r*c;
System.out.println(totalCells);
cell = new MainCell[totalCells];
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Timer set up
timer.setInitialDelay(Integer.parseInt(speed.getText()));
timer.setActionCommand("timer");
//set up menu bar
JMenuBar menuBar = new JMenuBar();
JMenu optionsMenu = new JMenu("Options");
JMenu aboutMenu = new JMenu("About");
menuBar.add(optionsMenu);
menuBar.add(aboutMenu);
JMenuItem helpButton = new JMenuItem("Help!");
helpButton.addActionListener(this);
helpButton.setActionCommand("help");
aboutMenu.add(helpButton);
JMenuItem aboutButton = new JMenuItem("About");
aboutButton.addActionListener(this);
aboutButton.setActionCommand("about");
aboutMenu.add(aboutButton);
JMenuItem colorSelect = new JMenuItem("Select a Custom Color");
colorSelect.addActionListener(this);
colorSelect.setActionCommand("colorSelect");
optionsMenu.add(colorSelect);
JMenuItem sizeChooser = new JMenuItem("Define a Custom Size");
sizeChooser.addActionListener(this);
sizeChooser.setActionCommand("sizeChooser");
optionsMenu.add(sizeChooser);
//Create text field to adjust speed and its label
JPanel speedContainer = new JPanel();
JLabel speedLabel = new JLabel("Enter the speed of a life cycle (in ms):");
speedContainer.add(speedLabel);
speedContainer.add(speed);
speedContainer.add(generationLabel);
Dimension speedDim = new Dimension(100,25);
speed.setPreferredSize(speedDim);
//Create various buttons
JPanel buttonContainer = new JPanel();
JButton randomizerButton = new JButton("Randomize");
randomizerButton.addActionListener(this);
randomizerButton.setActionCommand("randomize");
buttonContainer.add(randomizerButton);
JButton nextButton = new JButton("Next"); //forces a cycle to occur
nextButton.addActionListener(this);
nextButton.setActionCommand("check");
buttonContainer.add(nextButton);
JButton startButton = new JButton("Start");
startButton.addActionListener(this);
startButton.setActionCommand("start");
buttonContainer.add(startButton);
JButton stopButton = new JButton("Stop");
stopButton.addActionListener(this);
stopButton.setActionCommand("stop");
buttonContainer.add(stopButton);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(this);
clearButton.setActionCommand("clear");
buttonContainer.add(clearButton);
//holds the speed container and button container, keeps it neat
JPanel functionContainer = new JPanel();
BoxLayout functionLayout = new BoxLayout(functionContainer, BoxLayout.PAGE_AXIS);
functionContainer.setLayout(functionLayout);
functionContainer.add(speedContainer);
speedContainer.setAlignmentX(CENTER_ALIGNMENT);
functionContainer.add(buttonContainer);
buttonContainer.setAlignmentX(CENTER_ALIGNMENT);
//finish up with the cell container
GridLayout cellLayout = new GridLayout(rows,columns);
JPanel cellContainer = new JPanel(cellLayout);
cellContainer.setBackground(Color.black);
int posX = 0;
int posY = 0;
for(int i=0;i<totalCells;i++)
{
MainCell childCell = new MainCell();
cell[i] = childCell;
childCell.setName(String.valueOf(i));
childCell.setPosX(posX);
posX++;
childCell.setPosY(posY);
if(posX==columns)
{
posX = 0;
posY++;
}
cellContainer.add(childCell);
childCell.deactivate();
Graphics g = childCell.getGraphics();
childCell.paint(g);
}
//make a default color
userColor = Color.yellow;
//change icon
URL imgURL = getClass().getResource("images/gol.gif");
ImageIcon icon = new ImageIcon(imgURL);
System.out.println(icon);
setIconImage(icon.getImage());
//add it all up and pack
JPanel container = new JPanel();
BoxLayout containerLayout = new BoxLayout(container, BoxLayout.PAGE_AXIS);
container.setLayout(containerLayout);
container.add(cellContainer);
container.add(functionContainer);
add(menuBar);
setJMenuBar(menuBar);
add(container);
pack();
}
private void checkCells()
{
//perform check for every cell
for(int i=0;i<totalCells;i++)
{
cell[i].setNeighbors(checkNeighbors(i));
}
//use value from check to determine life
for(int i=0;i<totalCells;i++)
{
int neighbors = cell[i].getNeighbors();
if(cell[i].isActivated())
{
System.out.println(cell[i].getName()+" "+neighbors);
if(neighbors==0||neighbors==1||neighbors>3)
{
cell[i].deactivate();
}
}
if(cell[i].isActivated()==false)
{
if(neighbors==3)
{
cell[i].activate();
}
}
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("randomize"))
{
Random rn = new Random();
for(int i=0;i<totalCells;i++)
{
cell[i].deactivate();
if(rn.nextInt(6)==0)
{
cell[i].activate();
}
}
}
//help button, self-explanatory
if(e.getActionCommand().equals("help"))
{
JOptionPane.showMessageDialog(this, "The game is governed by four rules:\nFor a space that is 'populated':"
+ "\n Each cell with one or no neighbors dies, as if by loneliness."
+ "\n Each cell with four or more neighbors dies, as if by overpopulation."
+ "\n Each cell with two or three neighbors survives."
+ "\nFor a space that is 'empty' or 'unpopulated':"
+ "\n Each cell with three neighbors becomes populated."
+ "\nLeft click populates cells. Right click depopulates cells.","Rules:",JOptionPane.PLAIN_MESSAGE);
}
//shameless self promotion
if(e.getActionCommand().equals("about"))
{
JOptionPane.showMessageDialog(this, "Game made and owned by *****!"
+ "\nFree usage as see fit, but give credit where credit is due!\nVERSION: "+version,"About:",JOptionPane.PLAIN_MESSAGE);
}
//clears all the cells
if(e.getActionCommand().equals("clear"))
{
timer.stop();
generation = 0;
generationText = "Generation: "+generation;
generationLabel.setText(generationText);
for(int i=0;i<totalCells;i++)
{
cell[i].deactivate();
}
}
//starts timer
if(e.getActionCommand().equals("start"))
{
if(Integer.parseInt(speed.getText())>0)
{
timer.setDelay(Integer.parseInt(speed.getText()));
timer.restart();
}
else
{
JOptionPane.showMessageDialog(this, "Please use a value greater than 0!","Rules:",JOptionPane.ERROR_MESSAGE);
}
}
//stops timer
if(e.getActionCommand().equals("stop"))
{
timer.stop();
}
//run when timer
if(e.getActionCommand().equals("timer"))
{
generation++;
generationText = "Generation: "+generation;
generationLabel.setText(generationText);
timer.stop();
checkCells();
timer.setInitialDelay(Integer.parseInt(speed.getText()));
timer.restart();
}
//see checkCells()
if(e.getActionCommand().equals("check"))
{
generation++;
generationText = "Generation: "+generation;
generationLabel.setText(generationText);
checkCells();
}
//color select gui
if(e.getActionCommand().equals("colorSelect"))
{
userColor = JColorChooser.showDialog(this, "Choose a color:", userColor);
if(userColor==null)
{
userColor = Color.yellow;
}
}
//size chooser!
if(e.getActionCommand().equals("sizeChooser"))
{
SizeChooser size = new SizeChooser();
size.setLocationRelativeTo(null);
size.setVisible(true);
}
}
private int checkNeighbors(int c)
{
//if a LIVE neighbor is found, add one
int neighbors = 0;
if(cell[c].getPosX()!=0&&cell[c].getPosY()!=0)
{
if(c-columns-1>=0)
{
if(cell[c-columns-1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c-columns-1].getName());
neighbors++;
}
}
}
if(cell[c].getPosY()!=0)
{
if(c-columns>=0)
{
if(cell[c-columns].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c-columns].getName());
neighbors++;
}
}
}
if(cell[c].getPosX()!=columns-1&&cell[c].getPosY()!=0)
{
if(c-columns+1>=0)
{
if(cell[c-columns+1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c-columns+1].getName());
neighbors++;
}
}
}
if(cell[c].getPosX()!=0)
{
if(c-1>=0)
{
if(cell[c-1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c-1].getName());
neighbors++;
}
}
}
if(cell[c].getPosX()!=columns-1)
{
if(c+1<totalCells)
{
if(cell[c+1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c+1].getName());
neighbors++;
}
}
}
if(cell[c].getPosX()!=0&&cell[c].getPosY()!=rows-1)
{
if(c+columns-1<totalCells)
{
if(cell[c+columns-1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c+columns-1].getName());
neighbors++;
}
}
}
if(cell[c].getPosY()!=rows-1&&cell[c].getPosY()!=rows-1)
{
if(c+columns<totalCells)
{
if(cell[c+columns].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c+columns].getName());
neighbors++;
}
}
}
if(cell[c].getPosX()!=columns-1&&cell[c].getPosY()!=rows-1)
{
if(c+columns+1<totalCells)
{
if(cell[c+columns+1].isActivated())
{
System.out.println(cell[c].getName()+" found "+cell[c+columns+1].getName());
neighbors++;
}
}
}
return neighbors;
}
}
The following is MainCell.java:
public class MainCell extends JPanel implements MouseListener
{
//everything here should be self-explanatory
private static final long serialVersionUID = 1761933778208900172L;
private boolean activated = false;
public static boolean leftMousePressed;
public static boolean rightMousePressed;
private int posX = 0;
private int posY = 0;
private int neighbors = 0;
private URL cellImgURL_1 = getClass().getResource("images/cellImage_1.gif");
private ImageIcon cellImageIcon_1 = new ImageIcon(cellImgURL_1);
private Image cellImage_1 = cellImageIcon_1.getImage();
private URL cellImgURL_2 = getClass().getResource("images/cellImage_2.gif");
private ImageIcon cellImageIcon_2 = new ImageIcon(cellImgURL_2);
private Image cellImage_2 = cellImageIcon_2.getImage();
private URL cellImgURL_3 = getClass().getResource("images/cellImage_3.gif");
private ImageIcon cellImageIcon_3 = new ImageIcon(cellImgURL_3);
private Image cellImage_3 = cellImageIcon_3.getImage();
public MainCell()
{
Dimension dim = new Dimension(17, 17);
setPreferredSize(dim);
addMouseListener(this);
}
public void activate()
{
setBackground(MainGUI.userColor);
System.out.println(getName()+" "+posX+","+posY+" activated");
setActivated(true);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if(getPosX()==MainGUI.columns-1&&getPosY()==0)
{
//do nothing
}
else if(getPosY()!=0&&getPosX()!=MainGUI.columns-1)
{
g.drawImage(cellImage_1,0,0,null);
}
else if(getPosY()==0)
{
g.drawImage(cellImage_2,0,0,null);
}
else if(getPosX()==MainGUI.columns-1)
{
g.drawImage(cellImage_3,0,0,null);
}
}
public void setActivated(boolean b)
{
activated = b;
}
public void deactivate()
{
setBackground(Color.gray);
System.out.println(getName()+" "+posX+","+posY+" deactivated");
setActivated(false);
}
public boolean isActivated()
{
return activated;
}
public void setNeighbors(int i)
{
neighbors = i;
}
public int getNeighbors()
{
return neighbors;
}
public int getPosX()
{
return posX;
}
public void setPosX(int x)
{
posX = x;
}
public int getPosY()
{
return posY;
}
public void setPosY(int y)
{
posY = y;
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
if(leftMousePressed&&SwingUtilities.isLeftMouseButton(e))
{
activate();
}
if(rightMousePressed&&SwingUtilities.isRightMouseButton(e))
{
deactivate();
}
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
if(SwingUtilities.isRightMouseButton(e)&&!leftMousePressed)
{
deactivate();
rightMousePressed = true;
}
if(SwingUtilities.isLeftMouseButton(e)&&!rightMousePressed)
{
activate();
leftMousePressed = true;
}
}
public void mouseReleased(MouseEvent e)
{
if(SwingUtilities.isRightMouseButton(e))
{
rightMousePressed = false;
}
if(SwingUtilities.isLeftMouseButton(e))
{
leftMousePressed = false;
}
}
}
The following is SizeChooser.java:
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
public class SizeChooser extends JFrame
{
private static final long serialVersionUID = -6431709376438241788L;
public static MainGUI GUI;
private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JTextField rowsTextField = new JTextField(String.valueOf((screenSize.height/17)-15));
JTextField columnsTextField = new JTextField(String.valueOf((screenSize.width/17)-10));
private static int rows = screenSize.height/17-15;
private static int columns = screenSize.width/17-10;
public SizeChooser()
{
setResizable(false);
setTitle("Select a size!");
JPanel container = new JPanel();
BoxLayout containerLayout = new BoxLayout(container, BoxLayout.PAGE_AXIS);
container.setLayout(containerLayout);
add(container);
JLabel rowsLabel = new JLabel("Rows:");
container.add(rowsLabel);
container.add(rowsTextField);
JLabel columnsLabel = new JLabel("Columns:");
container.add(columnsLabel);
container.add(columnsTextField);
JButton confirmSize = new JButton("Confirm");
confirmSize.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
GUI.setVisible(false);
GUI = null;
if(Integer.parseInt(rowsTextField.getText())>0)
{
rows = Integer.parseInt(rowsTextField.getText());
}
else
{
JOptionPane.showMessageDialog(rootPane, "Please use a value greater than 0!","Rules:",JOptionPane.ERROR_MESSAGE);
}
if(Integer.parseInt(columnsTextField.getText())>0)
{
columns = Integer.parseInt(columnsTextField.getText());
}
else
{
JOptionPane.showMessageDialog(rootPane, "Please use a value greater than 0!","Rules:",JOptionPane.ERROR_MESSAGE);
}
GUI = new MainGUI("The Game of Life!", rows, columns);
GUI.setLocationRelativeTo(null);
GUI.setVisible(true);
setVisible(false);
}
});
container.add(confirmSize);
URL imgURL = getClass().getResource("images/gol.gif");
ImageIcon icon = new ImageIcon(imgURL);
System.out.println(icon);
setIconImage(icon.getImage());
pack();
}
public static void main(String[]args)
{
GUI = new MainGUI("The Game of Life!", rows, columns);
GUI.setLocationRelativeTo(null);
GUI.setVisible(true);
}
}
So the problem now is, when the randomize button is pressed, or a large number of cells exist and then the timer is started, the cells aren't as snappy as they would be with less active cells. For example, with 100 columns and 50 rows, when the randomize button is pressed, one cell activates, then the next, then another, and so forth. Can I have them all activate at exactly the same time? Is this just a problem with too many things calculated at once? Would concurrency help?
QUICK EDIT: Is the swing timer the best idea for this project?
I havent read through your code completely but I am guessing that your listener methods are fairly computationally intensive and hence the lag in updating the display.
This simple test reveals that your randomize operation should be fairly quick:
public static void main(String args[]) {
Random rn = new Random();
boolean b[] = new boolean[1000000];
long timer = System.nanoTime();
for (int i = 0; i < b.length; i++) {
b[i] = rn.nextInt(6) == 0;
}
timer = System.nanoTime() - timer;
System.out.println(timer + "ns / " + (timer / 1000000) + "ms");
}
The output for me is:
17580267ns / 17ms
So this leads me into thinking activate() or deactivate() is causing your UI to be redrawn.
I am unable to run this because I don't have your graphical assets, but I would try those changes to see if it works:
In MainGUI#actionPerformed, change:
if(e.getActionCommand().equals("randomize"))
{
Random rn = new Random();
for(int i=0;i<totalCells;i++)
{
cell[i].deactivate();
if(rn.nextInt(6)==0)
{
cell[i].activate();
}
}
}
to:
if(e.getActionCommand().equals("randomize"))
{
Random rn = new Random();
for(int i=0;i<totalCells;i++)
{
// This will not cause the object to be redrawn and should
// be a fairly cheap operation
cell[i].setActivated(rn.nextInt(6)==0);
}
// Cause the UI to repaint
repaint();
}
Add this to MainCell
// You can specify those colors however you like
public static final Color COLOR_ACTIVATED = Color.RED;
public static final Color COLOR_DEACTIVATED = Color.GRAY;
And change:
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if(getPosX()==MainGUI.columns-1&&getPosY()==0)
{
//do nothing
}
to:
protected void paintComponent(Graphics g)
{
// We now make UI changes only when the component is painted
setBackground(activated ? COLOR_ACTIVATED : COLOR_DEACTIVATED);
super.paintComponent(g);
if(getPosX()==MainGUI.columns-1&&getPosY()==0)
{
//do nothing
}

Trying to learn the ins and outs of javax.swing

So I've been trying to get the handle of javax.swing and am having some trouble. I am attempting to implement the '8 puzzle', where there are 8 tiles and one open spot set up in a 3x3 grid, when a tile adjacent to the open spot is clicked it trades spaces with the open spot. My structure consists of a JFrame, which contains a JPanel, and the JPanel contains the 9 tiles as JComponents, however only the JPanel renders, and the tiles are nowhere to be found. Any help with this problem would be greatly appreciated.
import javax.swing.*;
public class MainFrame{
public static void main(String[] args){
JFrame frame = new JFrame("8 Puzzle");
frame.setVisible(true);
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PieceManager pm = new PieceManager();
frame.add(pm);
}
}
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import javax.swing.*;
Second class
public class PieceManager extends JPanel{
int[] possmoves;
GameTile[] pieces;
int openSpot;
public PieceManager(){
this.setSize(600,600);
this.setBackground(new Color(255,255,255));
this.setLayout(new GridLayout(3,3));
pieces = new GameTile[9];
this.init();
this.addMouseListener(new ClickAction());
}
public void init(){
ArrayList<Integer> nums = new ArrayList<Integer>();
Random rand = new Random();
for(int i=0;i<9;i++){
nums.add(i);
}
for(int i=0,j=8;i<8;i++,j--){
int p = rand.nextInt(j);
GameTile x = new GameTile(i,nums.remove(p));
pieces[i]=x;
nums.removeAll(Collections.singleton(null));
}
GameTile z = new GameTile(8,nums.get(0));
pieces[8]=z;
possmoves = new int[4];
boolean found = false;
for(int i=0;i<9||found;i++){
if(pieces[i].getID()==0){
openSpot = pieces[i].getPos();
}
}
setOpenSpot();
paint();
}
public void paint(){
this.removeAll();
for(int i=0;i<9;i++){
this.add(pieces[i]);
pieces[i].setVisible(true);
}
}
public void setOpenSpot(){
Arrays.fill(possmoves,-1);
if(openSpot==0){
possmoves[0]=1;
possmoves[1]=3;
} else if(openSpot==1){
possmoves[0]=0;
possmoves[1]=2;
possmoves[3]=4;
} else if(openSpot==2){
possmoves[0]=1;
possmoves[1]=5;
} else if(openSpot==3){
possmoves[0]=0;
possmoves[1]=4;
possmoves[2]=6;
} else if(openSpot==4){
possmoves[0]=1;
possmoves[1]=3;
possmoves[2]=5;
possmoves[3]=7;
} else if(openSpot==5){
possmoves[0]=2;
possmoves[1]=4;
possmoves[3]=8;
} else if(openSpot==6){
possmoves[0]=3;
possmoves[1]=7;
} else if(openSpot==7){
possmoves[0]=6;
possmoves[1]=4;
possmoves[2]=8;
} else if(openSpot==8){
possmoves[0]=5;
possmoves[1]=7;
}
}
public void checkCorrect(){
}
public class ClickAction implements MouseListener{
#Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int pX=(int)Math.floor(x/200);
int pY=(int)Math.floor(y/200);
int piecepressed=(pY*3)+pX;
boolean moveable = false;
int toBeMoved = -1;
for(int i=0;i<4;i++){
if(piecepressed==possmoves[i]){
moveable=true;
toBeMoved=possmoves[i];
}
}
if(moveable){
GameTile saved=pieces[openSpot];
pieces[openSpot]=pieces[toBeMoved];
pieces[toBeMoved]=saved;
openSpot=toBeMoved;
setOpenSpot();
paint();
checkCorrect();
}
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
}
Tile Class (3rd and final)
import java.awt.*;
import javax.swing.JComponent;
public class GameTile extends JComponent{
private int id;
private int position;
public GameTile(int id, int initpos){
if(id==0){
this.id=id;
this.position=initpos;
} else{
this.id=id;
this.position = initpos;
String label = Integer.toString(id);
setSize(200,200);
setBackground(new Color(0,0,0));
Label l = new Label(label,Label.CENTER);
this.add(l);
l.setVisible(true);
}
}
public void setPos(int position){
this.position=position;
}
public int getPos(){
return position;
}
public int getID(){
return id;
}
}
Use of a JComponent is fine, but you'll probably want to set it to be opaque.
Also
Don't mix AWT (i.e., Label) with Swing components.
Don't setSize(...). Instead deal with preferredSize. Best to override getPreferredSize() and return an appropriate Dimension. I've used setPreferredSize(...) in a pinch, but at the risk of the wrath of kleopatra.
Don't override a JComponent or any of its children's (JPanel included) paint method without good reason (you don't have one). Instead override paintComponent. Edit: I see that your paint method is not a true override, so this is OK -- sorry for the misunderstanding on my part.
You almost never call paint or paintComponent directly. Instead you call repaint() and let the JVM call the painting methods for you. Edit: ditto for this as this was my misunderstanding of your code. You will want to call 'revalidate()andrepaint()` after removing and replacing components in your JPanel.
You can see that your JComponents are present and accounted for if you give them borders.
Edit 2: You will need to give your JComponents a layout if you want to have components added to them easily placed and visualized. In this example I gave mine a BorderLayout.
I've combined a bunch of classes into one file for ease with compilation.
I've indicated key changes with // !! comments.
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import javax.swing.*;
public class MainFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("8 Puzzle");
frame.setVisible(true);
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PieceManager pm = new PieceManager();
frame.add(pm);
}
}
class PieceManager extends JPanel {
int[] possmoves;
GameTile[] pieces;
int openSpot;
public PieceManager() {
this.setSize(600, 600);
this.setBackground(new Color(255, 255, 255));
this.setLayout(new GridLayout(3, 3));
pieces = new GameTile[9];
this.init();
this.addMouseListener(new ClickAction());
}
public void init() {
ArrayList<Integer> nums = new ArrayList<Integer>();
Random rand = new Random();
for (int i = 0; i < 9; i++) {
nums.add(i);
}
for (int i = 0, j = 8; i < 8; i++, j--) {
int p = rand.nextInt(j);
GameTile x = new GameTile(i, nums.remove(p));
pieces[i] = x;
nums.removeAll(Collections.singleton(null));
}
GameTile z = new GameTile(8, nums.get(0));
pieces[8] = z;
possmoves = new int[4];
boolean found = false;
for (int i = 0; i < 9 || found; i++) {
if (pieces[i].getID() == 0) {
openSpot = pieces[i].getPos();
}
}
setOpenSpot();
paint();
}
public void paint() {
this.removeAll();
for (int i = 0; i < 9; i++) {
this.add(pieces[i]);
pieces[i].setVisible(true);
}
revalidate(); // !!
repaint(); // !!
}
public void setOpenSpot() {
Arrays.fill(possmoves, -1);
if (openSpot == 0) {
possmoves[0] = 1;
possmoves[1] = 3;
} else if (openSpot == 1) {
possmoves[0] = 0;
possmoves[1] = 2;
possmoves[3] = 4;
} else if (openSpot == 2) {
possmoves[0] = 1;
possmoves[1] = 5;
} else if (openSpot == 3) {
possmoves[0] = 0;
possmoves[1] = 4;
possmoves[2] = 6;
} else if (openSpot == 4) {
possmoves[0] = 1;
possmoves[1] = 3;
possmoves[2] = 5;
possmoves[3] = 7;
} else if (openSpot == 5) {
possmoves[0] = 2;
possmoves[1] = 4;
possmoves[3] = 8;
} else if (openSpot == 6) {
possmoves[0] = 3;
possmoves[1] = 7;
} else if (openSpot == 7) {
possmoves[0] = 6;
possmoves[1] = 4;
possmoves[2] = 8;
} else if (openSpot == 8) {
possmoves[0] = 5;
possmoves[1] = 7;
}
}
public void checkCorrect() {
}
public class ClickAction implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int pX = (int) Math.floor(x / 200);
int pY = (int) Math.floor(y / 200);
int piecepressed = (pY * 3) + pX;
boolean moveable = false;
int toBeMoved = -1;
for (int i = 0; i < 4; i++) {
if (piecepressed == possmoves[i]) {
moveable = true;
toBeMoved = possmoves[i];
}
}
if (moveable) {
GameTile saved = pieces[openSpot];
pieces[openSpot] = pieces[toBeMoved];
pieces[toBeMoved] = saved;
openSpot = toBeMoved;
setOpenSpot();
paint();
checkCorrect();
}
}
#Override
public void mouseEntered(MouseEvent arg0) {
}
#Override
public void mouseExited(MouseEvent arg0) {
}
#Override
public void mousePressed(MouseEvent arg0) {
}
#Override
public void mouseReleased(MouseEvent arg0) {
}
}
}
class GameTile extends JComponent {
private int id;
private int position;
public GameTile(int id, int initpos) {
setBorder(BorderFactory.createTitledBorder("" + id)); // !!
setLayout(new BorderLayout()); // !! so the added JLabel will show
if (id == 0) {
this.id = id;
this.position = initpos;
} else {
this.id = id;
this.position = initpos;
String label = Integer.toString(id);
// !! setSize(200, 200);
setOpaque(true); // !!
setPreferredSize(new Dimension(200, 200)); // !!
setBackground(new Color(0, 0, 0));
// !! Label l = new Label(label, Label.CENTER);
JLabel l = new JLabel(label, SwingConstants.CENTER); // !!
this.add(l);
l.setVisible(true);
}
}
public void setPos(int position) {
this.position = position;
}
public int getPos() {
return position;
}
public int getID() {
return id;
}
}
There's probably a lot more to say, but this is all I've seen so far.

Categories