The Artima Developer Community
Sponsored Link

Java Answers Forum
trying to write a program that's like Minesweeper... stuck on gui

1 reply on 1 page. Most recent reply: Dec 21, 2002 5:05 PM by Calvin Chen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Calvin Chen

Posts: 2
Nickname: chinaman
Registered: Dec, 2002

trying to write a program that's like Minesweeper... stuck on gui Posted: Dec 21, 2002 5:02 PM
Reply to this message Reply
Advertisement
hello, i'm still sort of new to java... anyway, i was bored over this winter break so i decided to try to write a program called "MineSeeker" which... is more or less going to be like Minesweeper. i'm designing the program to simply show three fields where the user can specify how many mines, rows, and cols in the game and a button to get started.

i haven't even gotten to the real functionality of the program yet.. but am getting kicked in the rear end by the gui part of it. i'm not sure if it is my inferior coding or clumsiness on the part of java gui's (my guess is the former). like... i've just been fiddling with the numbers for specifying how many rows and columns and clicking on the button to make sure that the window resizes accordingly. sometimes it works... and sometimes it does not. what perplexes me is that it will work for an unspecified amount of tries, and then it will suddenly not work. however, if i manually maximize the window and then click on the button, it will pack the JFrame the way i imagined i'd coded it to in the first place. if i maximize and then restore... sometimes it will pack the JFrame correctly, and sometimes it won't. even more puzzling, is that if i use different parts of the BorderLayout manager for my textfields, button and mineField (see program code below)... the volatility of the window's inclination to resize changes. i'm not really asking for help on the program as a whole... but i'd really appreciate if someone could tell me if it is indeed something that I'm doing wrong, which is causing my window to not resize correctly after a certain number of times.

thank you very much.


/***********************************************
*MineSeeker v1.0
*by Calvin Chen (cvchen83@yahoo.com)
*
*MineSeeker is basically a watered-down version
*of the Minesweeper game found on MS Windows95+
*platforms. I got bored over the '02-'03 Winter
*break while not in class at UCSD, so I dediced
*to humor myself.
***********************************************/

import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.JTextComponent;

public class MineSeeker extends JFrame
{
private boolean[][] mineContent;
private int numRows;
private int numCols;
private int numMines;
private int height;
private int width;
private JPanel mineField;
private JButton start;
private JLabel mLabel;
private JLabel hLabel;
private JLabel wLabel;
private JTextField mTF;
private JTextField hTF;
private JTextField wTF;

/*Constructor*/
public MineSeeker()
{
int randomR, randomC;
Container window = getContentPane();
JPanel top = new JPanel();
JPanel middle = new JPanel();
JPanel bottom = new JPanel();

mLabel = new JLabel("Mines:");
hLabel = new JLabel(" Height:");
wLabel = new JLabel(" Width:");
mTF = new JTextField("10", 2);
hTF = new JTextField("10", 2);
wTF = new JTextField("10", 2);
start = new JButton("!");

mineField = new JPanel()
{
public void paintComponent(Graphics graphix)
{
super.paintComponent(graphix);
for(int r = 0; r < numRows; r++)
{
for(int c = 0; c < numCols; c++)
{
graphix.drawRect(c * 15, r * 15, 15, 15);
}
}
}
};

start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String mtf = mTF.getText().trim();
String htf = hTF.getText().trim();
String wtf = wTF.getText().trim();

numMines = Integer.valueOf(mtf).intValue();
numRows = Integer.valueOf(htf).intValue();
numCols = Integer.valueOf(wtf).intValue();
height = numRows * 15;
width = numCols * 15;
mineField.setPreferredSize(new Dimension(width, height));
mineField.paintImmediately(0, 0, width, height);
setMaximizedBounds(getMaximizedBounds());
pack();
}
});

setTitle("MineSeeker v1.0 by Calvin Chen");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bottom.add(mLabel);
bottom.add(mTF);
bottom.add(wLabel);
bottom.add(wTF);
bottom.add(hLabel);
bottom.add(hTF);
top.add(start);
middle.add(mineField);
window.add(top, BorderLayout.SOUTH);
window.add(middle, BorderLayout.CENTER);
window.add(bottom, BorderLayout.NORTH);
pack();
}

public static void main(String args[])
{
MineSeeker game = new MineSeeker();
game.setVisible(true);
}
}


Calvin Chen

Posts: 2
Nickname: chinaman
Registered: Dec, 2002

Re: trying to write a program that's like Minesweeper... stuck on gui Posted: Dec 21, 2002 5:05 PM
Reply to this message Reply
by the way... there are some extraneous variables in there that i plan to use, but haven't even gotten around to using. sorry for the confusion.

Flat View: This topic has 1 reply on 1 page
Topic: Attachments Previous Topic   Next Topic Topic: Looping through a multi-dimensional array

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use