aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Mine Sweeper( how to refresh the frame?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Mine Sweeper( how to refresh the frame?)" Watch "Mine Sweeper( how to refresh the frame?)" New topic
Author

Mine Sweeper( how to refresh the frame?)

Ajay Dubedi
Greenhorn

Joined: May 31, 2008
Posts: 3
hi! friends
i m trying to build mineSweeper game (Exact virtual copy of Windows version in our fav JAVA)
PROBLEM: Basic FRAME gets refreshed every time a button is clicked. i want a better way to refresh the Game frame .

Help me out!!!
the code is written below....................


import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
public class Minvers2 implements ActionListener
{
int[] zero=new int[81];
int[] zeroback= new int[81];
int score=0;
JPanel panl,panltop,panlcenter;
JButton[] butn;
JLabel[] lab;
JButton smile,ret,show;
JFrame fram;
JTextField t2;
JMenu Game, help;
JMenuItem n1,n2,n3,n4;
JMenuBar mb;
// IS HEREE<<<<<<<<<<<<<<<<<<
public void initiator(int [] kop)
{
for(int xues=0;xues<=80;xues++){
zero[xues]=-1;
zeroback[xues]=0;}
for(int k=0;k<=9;k++)
{zeroback[kop[k]]=14;}
for(int k=1;k<80;k++){
if(zeroback[k]>=13){
if(k==0){
zeroback[1]++;
zeroback[9]++;
zeroback[10]++;}
else if(k==8){
zeroback[7]++;
zeroback[17]++;
zeroback[16]++;}
else if(k==80){
zeroback[71]++;
zeroback[70]++;
zeroback[79]++;}
else if(k==72){
zeroback[63]++;
zeroback[73]++;
zeroback[64]++;}
else if(k<8&&k>0){
zeroback[k-1]++;
zeroback[k+1]++;
zeroback[k+8]++;
zeroback[k+9]++;
zeroback[k+10]++;}
else if(k>72&&k<80){
zeroback[k-1]++;
zeroback[k+1]++;
zeroback[k-8]++;
zeroback[k-9]++;
zeroback[k-10]++;}
else if((k%9)==0&&((k!=0)||(k!=72))){
zeroback[k-9]++;
zeroback[k+9]++;
zeroback[k+1]++;
zeroback[k-8]++;
zeroback[k+10]++;}
else if((k+1)%9==0&&((k!=8)||(k!=80))){
zeroback[k-9]++;
zeroback[k+9]++;
zeroback[k-1]++;
zeroback[k+8]++;
zeroback[k-10]++;}
else{
zeroback[k+1]++;
zeroback[k-1]++;
zeroback[k+9]++;
zeroback[k-9]++;
zeroback[k+8]++;
zeroback[k+10]++;
zeroback[k-8]++;
zeroback[k-10]++;} } } }


//RESET FUNCTION FOR THE GAME<<<<<<<<<<<<<<<<<
public void resetgame(){
int[] jim={0,0,0,0,0,0,0,0,0,0};
for(int i=0;i<=9;i++)
{jim[i]=(int)(80.00*Math.random());}
score=0;
initiator(jim); }



//CREATION OF ENVIORNMENT<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public void creat(){
//frame creation<<<<<<<<<<<<<<<<<<<<<<<<<
fram=new JFrame("Minesweeper1 : AJAY C.S. 6th SEM ((april2008))");
Toolkit kit =Toolkit.getDefaultToolkit();
Image gg= kit.getImage("ajju.gif");
fram.setIconImage(gg);
//buttons of game are added on this panl<<<<<<<<<<<<
panl= new JPanel();
GridLayout gridl;
gridl=new GridLayout(9,9,3,3);
panl.setLayout(gridl);
panl.setBorder(new CompoundBorder(BorderFactory.createLineBorder(Color.black,1),BorderFactory.createBevelBorder(BevelBorder.RAISED)));
/*a very special but common way
to creat array of objects*/
butn = new JButton[81];
lab =new JLabel[81];
score=81;
for(int y=0;y<=80;y++){
if(zero[y]==-1){
score--;
butn[y]= new JButton(" ");
butn[y].addActionListener(this);
panl.add(butn[y]);}
if(zero[y]==0){
lab[y]=new JLabel(" ");
panl.add(lab[y]);}
if(zero[y]==1){
lab[y]=new JLabel(" 1");
panl.add(lab[y]);}
if(zero[y]==2){
lab[y]=new JLabel(" 2");
panl.add(lab[y]);}
if(zero[y]==3){
lab[y]=new JLabel(" 3");
panl.add(lab[y]);}
if(zero[y]==4){
lab[y]=new JLabel(" 4");
panl.add(lab[y]);}
if(zero[y]==5){
lab[y]=new JLabel(" 5");
panl.add(lab[y]);}
if(zero[y]==6){
lab[y]=new JLabel(" 6");
panl.add(lab[y]);}
if(zero[y]==7){
lab[y]=new JLabel(" 7");
panl.add(lab[y]);}
if(zero[y]==8){
lab[y]=new JLabel(" 8");
panl.add(lab[y]);}
if(zero[y]>=13){
lab[y]=new JLabel(" >x<");
panl.add(lab[y]);} }
//panltop is created here <<<<<<<<<<<<<<<<<<<<<<<<<<<
panltop= new JPanel();
panltop.setBorder(new CompoundBorder(BorderFactory.createLineBorder(Color.black,1),BorderFactory.createBevelBorder(BevelBorder.RAISED)));
t2 = new JTextField(10);
smile = new JButton("SCORE");
ret = new JButton("RESET");
show =new JButton("SHOW");
smile.addActionListener(this);
ret.addActionListener(this);
show.addActionListener(this);
panltop.add(ret);
panltop.add(smile);
panltop.add(t2);
panltop.add(show);
/*panlcenter contains panl(having buttons) and <<<<<<<<<<<<
panltop having textfield (t1,t2)*/
panlcenter= new JPanel();
fram.getContentPane().add(panlcenter);
panlcenter.add(panltop);
panlcenter.add(panl);
//center created

mb = new JMenuBar();
fram.setJMenuBar(mb);
Game= new JMenu("Game");
help= new JMenu("Help");
mb.add(Game);
mb.add(help);
n1=new JMenuItem("New");
n2=new JMenuItem("Beginer");
n3=new JMenuItem("Intermidiate");
n4=new JMenuItem("Expert");
Game.add(n1);
n1.addActionListener(this);
Game.add(n2);
Game.add(n3);
Game.add(n4);
fram.setSize(400,380);
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
fram.setBounds(center.x-400/2,center.y-380/2,400,380);
fram.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fram.setVisible(true); }

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>action event <<<<<<<<<<<<<<<<
public void actionPerformed(ActionEvent akd){
////>>>>>>> GAME GRID <<<<<<< BUTTON LISTENER
for(int c=0;c<=80;c++){
if(akd.getSource()==butn[c]){
if(zeroback[c]>=13){
zero[c]=13;
JOptionPane.showMessageDialog(null,">>>> YOU LOST!!!","",JOptionPane.PLAIN_MESSAGE);
for(int j=0;j<=80;j++)
{if(j!=41){zero[j]=zeroback[j];}}
fram.setVisible(false);
creat();
JOptionPane.showMessageDialog(null,"GAME RESTART","",JOptionPane.PLAIN_MESSAGE);
resetgame();
fram.setVisible(false);
creat();}
zero[c]=zeroback[c];
fram.setVisible(false);
creat(); } }
//>>>>>>> SMILE <<<<<<< BUTTON LISTENER
if(akd.getSource()== smile)
{t2.setText("YOUR SCORE = "+score);}
//>>>>>>> RESET <<<<<<< BUTTON LISTENER
if(akd.getSource()== ret){
resetgame();
fram.setVisible(false);
creat();}
//>>>>>>> SHOW <<<<<<< BUTTON LISTENER/
if(akd.getSource()==show){
for(int j=0;j<=80;j++)
{if(j!=41){zero[j]=zeroback[j];}}
//fram.setVisible(false);
creat();
JOptionPane.showMessageDialog(null,"GAME RESTART","",JOptionPane.PLAIN_MESSAGE);
resetgame();
fram.setVisible(false);
creat();}
//>>>>>>>> HELP <<<<<<<< BUTTON LISTNER/
if(akd.getSource()==n1)
{JOptionPane.showMessageDialog(null,"The game is exactly like window's minesweeper please PLAY N ENJOY","",JOptionPane.PLAIN_MESSAGE);
}
}
public static void main (String gkj[]){
Minvers2 g=new Minvers2();
g.resetgame();
g.creat(); }
Ajay Dubedi
Greenhorn

Joined: May 31, 2008
Posts: 3
i forgot to mention that the problem with refreshing of the frame is that when frame visibility is true and if a button is pressed visibility is set to false. this is creating a flickring effect instead of smooth animation.
i wish to remove this flickring effect.
PLEASE GUYS HELP ME OUT ITS MY SCHOOL PROJECT!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38726
    
  23
Welcome to JavaRanch

Please go back to your original post and use the pencil and paper icon to add code tags by highlighting the class body and clicking the CODE button under the message window.
Please use ctrl-C ctrl-V for posting code. What you have posted will not compile; I think you have forgotten the final }.

You have posted an illegible monolithic mass of code.
The initiator method (don't call it a function) has far too many if-elses in. That ought to have been implemented with nested loops.
And you are testing for things like (k != 0 || k != 72) which is a tautology: why?

The actionPerformed() method is a good example of what I cursed roundly here and in the earlier posts I mentioned. It is a total mess. Each button ought to have its own instance of an ActionListener class which is tailored to the Minesweeper game. Again it looks like a job for nested loops.
You are using setVisible(false) and nesting creat() calls in the creat() method which is liable to set up an infinite recursion.

Sorry I have found so much to complain about, but you will have to redesign that class totally. Use the repaint() method for refreshing the display.
Jack Crow
Greenhorn

Joined: May 31, 2008
Posts: 2
I've taken a pretty close look at the code (takes about 10 min in Eclipse to turn that unformatted mess into something readable) which is still too long for most people.

Your methods and variables are poorly named. I would start with that. Maybe you arent a native English speaker. If so, my apologies.
ret = reset
panl = panel
panltop = topPanel

Secondly, the flickering comes from the JFrame constantly being hidden, recreated and shown. The flickering is literally because you hide the JFrame and the time it takes to remake and reshow the frame is long enough that it's visible.

You have run into the basic complaint people have with Swing. It's slow to render. Other than the flickering the design you have is useable. Your "resetGame()" initializes then throws down random mines. One problem (I think) is that you are also trying to put down labels/values in another array indicating how many adjacent mines there are. It would take a better person than me to get that to work and only worsens your rendering problem as it redraws all those too.

Generally you should restart your project like so...

Make CreateDisplay() that creates your basic JFrame and controls in a fixed size.
Test It.
Make a 2, 2 dimensional arrays. Label[][] MineFieldLabels; Button[][] MineFieldButtons;
Make a setUpMineField() that adds event listeners to all the buttons then adds the labels to the display panel and then the buttons to the display panel.
Test it.
Make a clearMineField() that removes all the labels and buttons.
Test it.
Make setUpMineField(int skill) so you can create minefields of different sizes
Test it.
Make a boolean checkForMine(int,int).
Test it.
Make your ActionPerformed(event). If it's a click event and source button, for(int i=0;i< button[].length; i++){for(int j=0;j<button.length;j++){ if(source == button[i][j]){ if(!checkForMine(i,j)){ button[i][j].setVisible(false); }}}
Test it.
etc.

Dont try to implement everything at once. Only the things you need until it works. The ONLY reason to hide that JFrame is to keep the user from interacting with it.
Ajay Dubedi
Greenhorn

Joined: May 31, 2008
Posts: 3
thank you! Mr. Jack Crow
i tried the instructions you gave regarding the redesigning of the project.
its better to solve in the steps you have mentioned.
i still have a question that how the method repaint will help me in redrawing of the frame ? i am unable to understand how to implement repaint if possible please state an example.

please suggest..
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Ajay Dubedi:
i still have a question that how the method repaint will help me in redrawing of the frame ? i am unable to understand how to implement repaint if possible please state an example.

There are some great lessons and sample codes in the Sun Java Swing tutorials, and I strongly suggest that you spend quite a bit of time reviewing these as graphics programming can be counterintuitive if you've never done it before.

Also, it seems to me as if you are trying to tackle this beast all in one huge mega-class. If so, you will save yourself a lot of grief later if you break your problem down into smaller more manageable classes, each one responsible for one and only one issue. For instance it may be helpful to have a MineSweeperButton class that encapsulates all that this button has to do. For instance, if you look at the Standard MineSweeper app, you'll see that this "button" is only initially a button, but later after being pressed, changes into what in Java would be a JLabel -- empty space with text (a number) on it. One way this can be emulated is having this "button" use the CardLayout. Shoot, it would probably even be a good idea to give this MineSweeperButton a model to encapsulate its non-GUI logic, though this is not 100% necessary.

Anyway, I'm digressing too much into detail. The bottom line is divide into classes, test each class individually, and this way conquer the big problem.

Good luck!

Pete
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mine Sweeper( how to refresh the frame?)