You are viewing our Forum Archives. To view or take place in current topics click here.
Help With a RadioButton ( Java )
Posted:

Help With a RadioButton ( Java )Posted:

yoseaweed
  • Prospect
Status: Offline
Joined: Aug 04, 201014Year Member
Posts: 608
Reputation Power: 25
Status: Offline
Joined: Aug 04, 201014Year Member
Posts: 608
Reputation Power: 25
Ok so I'm making a game like minesweeper, but you have to find the flag, and when you click a box, a mine appears. I want to have a radiobutton on the lower right hand side so when I check it off, it changes the color of the mine when I click again. Hope you understand. Or a radiobutton at the end of the game so I can restart. Which ever is easier.

GAME CODE:

// Mitch

import java.awt.Graphics;
import java.awt.Color;
import java.util.Random;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.applet.*;
import java.net.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



import java.applet.AudioClip;


public class MinesweeperGUI
{
   public static int DEFAULT_GAME_SIZE = 500;
   public static int SIDE = 50;
   
   private int gameSize, xCtr, yCtr, state = 0;
   private String status = "";
   private boolean hit;
   private JLabel labelImage;
   private JRadioButton play;
   
   private JLabel label;
   

   
   public MinesweeperGUI( int newGameSize )
   {
      if ( newGameSize > SIDE )
         gameSize = newGameSize;
      else
         gameSize = DEFAULT_GAME_SIZE;
         
      Random random = new Random();
      xCtr = SIDE / 2 + random.nextInt( gameSize - SIDE );
      yCtr = SIDE / 2 + random.nextInt( gameSize - SIDE );
      hit = false;
   }
   

   
   
   public String getStatus()
   {
      return status;
   }
   
   public int getGameSize()
   {
      return gameSize;
   }
   
   public boolean isHit()
   {
      return hit;
   }
   
   
   
   public void play( int x, int y )
   {
      if (Math.abs( x - xCtr ) < SIDE / 2
         && Math.abs( y - yCtr ) < SIDE / 2 )
      {
         status = "You hit the flag!";
         hit = true;
      }
      
      else if ( Math.abs( x - xCtr ) < 2 * SIDE
         && Math.abs( y - yCtr ) < 2 * SIDE )
         status = "Your close to the flag!";
         
      else
         status = "Your move is valid!";
   }
   
   public void setState(int s)
   {
      this.state = s;
   }
   
   public void draw( Graphics g, int x, int y )
   {
   
   
      if(state == 0)
      {
         BufferedImage img2 = null;
         try
         {
                     
            img2 = ImageIO.read(new File("background4.png"));
            g.drawImage( img2, 0, 0, null);
            
            
         } catch(Exception e) {
            System.out.println("Image not found.");
         }
            
         ++state;
         
         BufferedImage img4 = null;
         try
         {
                     
            img4 = ImageIO.read(new File("top2.png"));
            g.drawImage( img4, 50, 0, null);
            
            
         } catch(Exception e) {
            System.out.println("Image not found.");
         }
         ++state;

         
      }
      
      if ( status.equals( "You hit the flag!" ))
      {
         
         BufferedImage img3 = null;
         try
         {
            img3 = ImageIO.read(new File("flag2.png"));
            g.drawImage(img3, x - SIDE/2, y - SIDE/2, null);
                     
         } catch(Exception e) {
            System.out.println("Image not found.");
         }
               
      }
      else
      {
         BufferedImage img = null;
         try
         {
            img = ImageIO.read(new File("mine.png"));
            g.drawImage(img, x - SIDE/2, y - SIDE/2, null);
         } catch(Exception e) {
            System.out.println("Image not found.");
         }
      }
   }
}


Game Client Code:
//Mitch

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MinesweeperGUIClient extends JFrame
{
   private int x;
   private int y;
   private MinesweeperGUI sub;
   private boolean gameStarted = false;
   
   private MouseHandler mh;
   
   public MinesweeperGUIClient()
   {
      super( "Can you find the flag?" );
      
      sub = new MinesweeperGUI( MinesweeperGUI.DEFAULT_GAME_SIZE );
      
      mh = new MouseHandler();
      addMouseListener( mh );
      
      setSize( sub.getGameSize(), sub.getGameSize() );
      setVisible( true );
   }
   
   private class MouseHandler extends MouseAdapter
   {
      public void mousePressed ( MouseEvent me )
      {
         x = me.getX();
         y = me.getY();
         sub.play(x,y);
         setTitle( sub.getStatus());
         repaint();
      }
   }
   
   public void paint ( Graphics g )
   {
      if (!gameStarted )
      {
         super.paint(g);
         gameStarted = true;
         sub.setState(0);
      }
      sub.draw( g, x, y );
      if ( sub.isHit())
         removeMouseListener ( mh );
         
   }
   
   public static void main( String [] args )
   {
      MinesweeperGUIClient subH = new MinesweeperGUIClient();
      subH.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }
}



P.S. Why does it paint the mine when I first run it. Please help !! Thanks !!! You might need the pics to run, but hopefully you can read the code!! THANKS!!!
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.