By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,024 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,024 IT Pros & Developers. It's quick & easy.

alot of help needed....asap....anyone interested?

P: 1
Hi,

I don't even know if this is the right place to post this but i will, because I need help. I'm trying to finish this basic game. but I am having troubles. If anyone wants to help me out it would be great. I will admit this might take a long time, i am new to java and i suck at it(yes i will also admit that). by anyones standards this program probably really sucks, but its the best that i can do.

my objectives:
- when a button is pressed down, switch it to the opposite button ( on goes to off, off goes to on) but at the same time, switch the buttons that are left, right, up and down from it.
Before:

http://i15.photobucket.com/albums/a385/t_dimock/Picture3-2.png
After:

http://i15.photobucket.com/albums/a385/t_dimock/Picture2-9.png

graphics

http://i15.photobucket.com/albums/a385/t_dimock/On.png

http://i15.photobucket.com/albums/a385/t_dimock/Off.png
Anyone have any ideas?
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5.  
  6.  
  7. /**
  8.  * Class l0gixGame - write a description of the class here
  9.  * 
  10.  * @author (your name) 
  11.  * @version (a version number)
  12.  */
  13. public class logixGame2 extends JApplet {
  14.     JButton btn;
  15.     private static final ImageIcon image[]={
  16.         new ImageIcon("On.png"),
  17.         new ImageIcon("Off.png")};
  18.    ImageIcon on = new ImageIcon(getImage(getCodeBase(),
  19.        "On.png"));
  20.  
  21.    ImageIcon off = new ImageIcon(getImage(getCodeBase(),
  22.        "Off.png"));
  23.  
  24.    public void init(){
  25.  
  26.        /** creates the control panel, sets the size,locaiton and the background
  27.         * colour
  28.         */
  29.        JPanel controlPanel = new JPanel();
  30.        controlPanel.setSize(new Dimension(400,275));
  31.        controlPanel.setLocation(10,100);
  32.        controlPanel.setBackground(Color.gray);
  33.  
  34.        /** creates the background panel, sets the size,locaiton and the
  35.        * background color
  36.        */
  37.        JPanel backGround = new JPanel();
  38.        backGround.setSize(new Dimension(400,400));
  39.        backGround.setLocation(0,0);
  40.        backGround.setBackground(Color.gray);
  41.  
  42.        /** creates the play panel, sets the size,locaiton and the
  43.        * background color
  44.        */
  45.        JPanel playPanel = new JPanel();
  46.        playPanel.setSize(new Dimension(400,275));
  47.        playPanel.setLocation(500,200);
  48.        playPanel.setBackground(Color.orange);
  49.  
  50.        /** creates the layout for the play panel, and the control panel
  51.         * creates the content pane, and the three panels to the content pane
  52.         * and sets the dimensions for the content pane
  53.         */
  54.        controlPanel.setLayout(new GridLayout(8,12));
  55.        playPanel.setLayout(new GridLayout(8,12));
  56.        Container cp = getContentPane();
  57.        cp.setSize(new Dimension(5000,5000));
  58.        cp.add(controlPanel);
  59.        cp.add(playPanel);
  60.        cp.add(backGround);
  61.  
  62.        /**initialize the onbutton and the offbutton with the related graphics*/
  63.        ImageIcon on = new ImageIcon(getImage(getCodeBase(),
  64.        "On.png"));
  65.  
  66.        ImageIcon off = new ImageIcon(getImage(getCodeBase(),
  67.        "Off.png"));
  68.  
  69.        ArrayList<String> control = new ArrayList<String>();
  70.        ArrayList<String> play = new ArrayList<String>();
  71.  
  72.        /** Randimization of control panel and play panel*/
  73.        /**1=off 2=on*/
  74.        int row,column,i,x,y;
  75.          for( column = 0; column <= 8; column = column+1)
  76.          {
  77.                 for (row = 0; row <= 14; row = row+1)
  78.                 {
  79.                     i = 1;
  80.                     y = row * 50;
  81.                     x = column * 50;
  82.                     if ( (column % 2) == (row % 2) )
  83.                     {
  84.  
  85.                         Random r = new Random();
  86.                         int n = r.nextInt();
  87.                         if(n % 2 == 0){
  88.                              JLabel btn2 = new JLabel(off);
  89.                             controlPanel.add(btn2);
  90.                             control.add("1");
  91.                             JButton btn = new JButton(on);
  92.                             btn.setBackground(Color.orange);
  93.                             playPanel.add(btn);
  94.                             btn.addActionListener(new bob());
  95.                             play.add("2");
  96.  
  97.  
  98.  
  99.                         }
  100.                         else{
  101.                             JLabel btn2 = new JLabel(on);
  102.                             controlPanel.add(btn2);
  103.                             control.add("2");
  104.                             JButton btn = new JButton(off);
  105.                             btn.setBackground(Color.orange);
  106.                             playPanel.add(btn);
  107.                             btn.addActionListener(new bob());
  108.                             play.add("1");
  109.                         }
  110.  
  111.                     }
  112.                     else
  113.                     {
  114.                         Random r = new Random();
  115.                         int n = r.nextInt();
  116.                         if(n % 3 == 0){
  117.                             JLabel btn2 = new JLabel(on);
  118.                             controlPanel.add(btn2);
  119.                             control.add("2");
  120.                             JButton btn = new JButton(on);
  121.                             btn.setBackground(Color.orange);
  122.                             playPanel.add(btn);
  123.                             btn.addActionListener(new bob());
  124.                             play.add("2");
  125.  
  126.                     }
  127.  
  128.                     }
  129.  
  130.  
  131.                 }
  132.             }
  133.  
  134.             String controlString =""+ control;
  135.             String playString =""+ play;
  136.  
  137.             if(controlString.compareTo(playString)==0){
  138.                 JLabel winner = new JLabel("Winner!");
  139.                 backGround.add(winner);
  140.             }
  141.         }
  142. public class bob implements ActionListener{
  143.     public void actionPerformed (ActionEvent event){
  144.         if(btn.getIcon().equals(on))
  145.         {
  146.             btn.setIcon(off);
  147.         }
  148.         else
  149.         {
  150.             btn.setIcon(on);
  151.  
  152.         }
  153. }
  154. }
  155.  
  156. }
Dec 8 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.