Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: GUI Problem

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GUI Problem

    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 have it set up to look like the picture just doesnt do anything

    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.

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*; // needed for event handling
     
    public class OpolyPanel extends JPanel implements ActionListener {
     
      JMenuBar b;
      JMenu menu = new JMenu("Actions");
       JLabel counterLabel = new JLabel("Position: 0 ");
       JButton playTurn = new JButton("Play Turn");
       JMenuItem white = new JMenuItem("White");
       JMenuItem red = new JMenuItem("red");
       JMenuItem blue = new JMenuItem("blue");
       JMenuItem pink = new JMenuItem("pink");
       JMenuItem yellow = new JMenuItem("yellow");
       JMenuItem green = new JMenuItem("green");
       JMenuItem orange = new JMenuItem("orange");
       JMenuItem quit = new JMenuItem("Quit");
       JMenuItem newGame = new JMenuItem(" New Game");
       JLabel countLabel = new JLabel("Reward: 0 ");
     
     
      public OpolyPanel(JMenuBar bar) {
    super.Opoly2(25);                [B]how do u get this super to work, when that work methods should work [/B]    setBackground(Color.white);
        this.b = bar;
      b.add(menu);
        this.add(counterLabel);
        this.add(playTurn); // place button in panel
        playTurn.addActionListener(this);// panel is listener for button
        this.add(countLabel);
     
        menu.add(newGame);
        newGame.addActionListener(this);
        menu.add(white);
         white.addActionListener(this);
        menu.add(red);
        red.addActionListener(this);
        menu.add(blue);
        blue.addActionListener(this);
        menu.add(pink);
        pink.addActionListener(this);
        menu.add(yellow);
        yellow.addActionListener(this);
        menu.add(green);
        green.addActionListener(this);
        menu.add(orange);
        orange.addActionListener(this);
        menu.add(quit);
        quit.addActionListener(this);
     
     
     
      }
     
     
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
     
      }
     
      public void actionPerformed(ActionEvent e) { 
     
        if (e.getSource() == quit){
          System.exit(0);}
        if (e.getSource() == red) setBackground(Color.red);
        else
          if (e.getSource() == blue) setBackground(Color.blue);
        else
          if (e.getSource() == pink) setBackground(Color.pink);
        else
          if (e.getSource() == yellow) setBackground(Color.yellow);
        else
          if (e.getSource() == green) setBackground(Color.green);
        else
          if (e.getSource() == orange) setBackground(Color.orange);
        else
          if (e.getSource() == white) setBackground(Color.white);
        if(e.getSource() == playTurn()) reset();         [B]I want the game to reset when i hit new game[/B]
     
     
     
      }
    }


    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 Json; December 11th, 2009 at 03:33 AM. Reason: Please use code tags.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: GUI Problem

    First, your OpolyPanel class extends JPanel, so there is no super.Opoly2(25). Might be better to create an instance variable of Opoly2 in your OpolyPanel class, this way your drawing can easily interact with it for drawing and interaction.

    For drawing the squares similar to the image you provided, you need to override the paintComponent method of the panel you are going to draw to and draw the squares. Its a trivial drawing problem from there (read up on 2D Graphics in java)