Results 1 to 2 of 2

Thread: Gui problems

  1. #1
    bulldog is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default Gui problems

    i417.photobucket.com/albums/pp260/spawnacreature/Opoly.jpg

    i need to know how to make the blue boxes, i need to set the size of board to 21.

    also how would i get the postions to cycle through and when it reaches the end to say game over in red.
    anyhelp would be apreciated
    this is what i have so far it works i just dont know what to add, to get the numbers to show up next to the rewards and postion or also the blue squares

    and help would be very apreciated.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*; // needed for event handling
    
    public class OpolyPanel extends JPanel implements ActionListener {
       JLabel counterLabel = new JLabel("Position:  ");
       JButton playTurn = new JButton("Play Turn");
       JLabel countLabel = new JLabel("Reward: ");
       JMenuBar b;
       JMenu menu = new JMenu("Actions");
       JMenuItem game = new JMenuItem("New Game");
       JMenuItem quitter = new JMenuItem("Quit");
      
    public OpolyPanel(JMenuBar bar){
        setBackground(Color.green);
        
        this.add(counterLabel);
        this.add(playTurn); // place button in panel
        playTurn.addActionListener(this);// panel is listener for button
        this.add(countLabel);
    
        this.b = bar; // menu bar passed down from JFrame 
       b.add(menu); // attach menu to the bar
       menu.add(game); // a menu choice
       menu.add(quitter); // a menu choice
       game.addActionListener(this);//panel: listener for menu items
       quitter.addActionListener(this);
      }
      
       
       
      public void paintComponent(Graphics g){
        super.paintComponent(g);
           
      }
      
      
    
      public void actionPerformed(ActionEvent e) { 
       if (e.getSource() == playTurn)
         repaint();
    
    
      }
      }
     
    
    
    
    import java.awt.*;
    import javax.swing.*;
    
     public  class GopolyDriver{
        
    public static void main(String[] args){
      DisplayWindow d = new DisplayWindow();
        JMenuBar menuBar = new JMenuBar();
        d.setJMenuBar(menuBar);
        OpolyPanel p = new OpolyPanel(menuBar);
        d.addPanel(p);
        d.showFrame();
    }
     }
    
    The game cant change this file...
    
    public class Opoly2{
     private int boardSize;
     private int pos = 0;
    private int steps = 0;
    private int reward = 10;
    
     public Opoly2(int boardSize){
       this.boardSize=boardSize;
     }
     
     public int getPos(){return pos;}
     public int getSteps(){return steps;}
     public int getReward(){return reward;}
     public int getBoardSize(){return boardSize;}
     
     public void reset(){
       // resets game for another start
       pos = 0;
       steps = 0;
       reward = 10;
     }
    
     public int spin(){
       // gives a random spin 1-5
       int p=(1+(int)(Math.random()*5));
       return p;
     }
    
     public void move(int p){
       // rules for advancing board piece
       steps++;
       if  (pos + p > boardSize) return;
       if(pos+p == boardSize-1){
         reward=reward/3;
         pos=0;
       }
       else if (pos + p == boardSize)
       {pos = pos + p; return;}
       else{
         pos = pos + p;
         if(pos%4==0) reward = 2*reward;
       }
     }
     
     public boolean isGameOver(){
       // game over when piece equals board size exactly
       return (pos==boardSize);
     }
    
     public void spinAndMove(){
       // does a spin, then move
       int p=spin();
       move(p);
     }
    
     public void drawBoard(){
       // a crude drawing of the board; shows current reward
       for(int j=0; j<boardSize; j++){
         if(j!=pos){
           System.out.print("*");}
         else{
           System.out.print("o");}
       }
       if (pos == boardSize)System.out.print("o");
       System.out.println(" " + reward);
     }
    
     public void playGame(){
       // top level play of game; play continues until game over (duh!)
       System.out.println("Board Size: " + boardSize);
         drawBoard();
       while (isGameOver()==false){
        spinAndMove();
        drawBoard();
       }
       System.out.println("game over");
       System.out.println("rounds of play: " + steps);
       System.out.println("final reward: " + reward);
              }
    }
    
    Display window
    
    import java.awt.*;
    import javax.swing.*;
    public  class DisplayWindow extends JFrame{
    
      private Container c;
    
      public DisplayWindow(){
        super("Display");
        c = this.getContentPane();
      }
    
      public void addPanel(JPanel p){
        p.setPreferredSize(new Dimension(500,400));
        c.add(p);
      }
    
      public void showFrame(){
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    }
    Last edited by Fubarable; 12-11-2009 at 12:34 PM. Reason: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Problems, problems, problems!
    By Empower5315 in forum New To Java
    Replies: 2
    Last Post: 08-28-2009, 04:27 AM
  2. having problems
    By pinkdiamondgail in forum New To Java
    Replies: 6
    Last Post: 02-06-2009, 11:35 PM
  3. GUI problems.
    By saytri in forum New To Java
    Replies: 1
    Last Post: 12-16-2007, 10:27 PM
  4. gui problems
    By bluebirdjc in forum Advanced Java
    Replies: 2
    Last Post: 07-23-2007, 05:38 PM
  5. a few problems
    By gary in forum AWT / Swing
    Replies: 0
    Last Post: 07-11-2007, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •