Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: GUI help

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

    Default

    I couldn't help tinkering with my darn code...

    DoorPanel.java
    Java Code:
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    
    public class DoorPanel
    {
      private static final String DOOR_BUTTON = "Door Button";
      private static final String WIN_LOSE_LABEL = "Label";
    
      private JPanel mainPanel = new JPanel();
      private JLabel winLoseLabel = new JLabel();
      private ImageIcon winIcon;
      private ImageIcon loseIcon;
      private JButton doorButton;
      private CardLayout cardlayout = new CardLayout();
      private boolean winner;
      private List<ActionListener> listenerList = new ArrayList<ActionListener>();
    
      public DoorPanel(ImageIcon doorIcon, ImageIcon winIcon, ImageIcon loseIcon, boolean winner)
      {
        this.winIcon = winIcon;
        this.loseIcon = loseIcon;
        winLoseLabel.setHorizontalAlignment(SwingConstants.CENTER);
        winLoseLabel.setBackground(Color.darkGray);
        setWinLoseIcon(winner);
        
        doorButton = new JButton(doorIcon);
        doorButton.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            // swap card to show label
            cardlayout.show(mainPanel, WIN_LOSE_LABEL);
            String actionCommand = DoorPanel.this.winner ? "Winner" : "Loser";
            ActionEvent ae = new ActionEvent(DoorPanel.this, e.getID(), actionCommand);
            for (ActionListener al : listenerList)
            {
              al.actionPerformed(ae);
            }
          }
        });
    
        mainPanel.setLayout(cardlayout); // set layout of jpanel to cardlayout
        mainPanel.add(doorButton, DOOR_BUTTON); // add button first with BUTTON string
        mainPanel.add(winLoseLabel, WIN_LOSE_LABEL); // add label second with LABEL const string    
      }
      
      private void setWinLoseIcon(boolean winner)
      {
        this.winner = winner;
        if (winner)
        {
          winLoseLabel.setIcon(winIcon);
        }
        else
        {
          winLoseLabel.setIcon(loseIcon);
        }    
      }
      
      public void setButtonEnabled(boolean enabled)
      {
        doorButton.setEnabled(enabled);
      }
      
      public void addActionListener(ActionListener al)
      {
        listenerList.add(al);
      }
      
      public boolean isWinner()
      {
        return winner;
      }
      
      public void reset(boolean winner)
      {
        cardlayout.show(mainPanel, DOOR_BUTTON);
        setWinLoseIcon(winner);
        doorButton.setEnabled(true);
      }
    
      public JComponent getPanel()
      {
        return mainPanel;
      }
    }
    DoorArray.java
    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    
    public class DoorArray
    {
      private static final int DOOR_COUNT = 3;
      private static final String IMAGE_PATH = "src/dy08/m12/b/images/";
      private static final String DOOR_IMAGE = "Backdoor.jpg";
      private static final String WIN_IMAGE = "happy face.jpg";
      private static final String LOSE_IMAGE = "sad face.jpg";
    
      private JPanel mainPanel = new JPanel();
      private DoorPanel[] doors = new DoorPanel[DOOR_COUNT];
      private Random random = new Random();
      
      public DoorArray()
      {
        ImageIcon doorIcon = null;
        ImageIcon winIcon = null;
        ImageIcon loseIcon = null;
        try
        {
          doorIcon = new ImageIcon(ImageIO.read(new File(IMAGE_PATH + DOOR_IMAGE)));
          winIcon = new ImageIcon(ImageIO.read(new File(IMAGE_PATH + WIN_IMAGE)));
          loseIcon = new ImageIcon(ImageIO.read(new File(IMAGE_PATH + LOSE_IMAGE)));
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
    
        int gap = 5;
        JPanel doorsPanel = new JPanel(new GridLayout(1, 0, gap, 0));
        int randomInt = random.nextInt(DOOR_COUNT);
        for (int i = 0; i < doors.length; i++)
        {
          doors[i] = new DoorPanel(doorIcon, winIcon, loseIcon, (randomInt == i));
          doorsPanel.add(doors[i].getPanel());
          doors[i].addActionListener(new ActionListener()
          {
            public void actionPerformed(ActionEvent e)
            {
              doorsAction(e);
            }
          });
        }
    
        mainPanel.add(doorsPanel);
      }
      
      public void reset()
      {
        int randomInt = random.nextInt(DOOR_COUNT);
        for (int i = 0; i < doors.length; i++)
        {
          doors[i].reset(i == randomInt);
        }
      }
    
      private void doorsAction(ActionEvent e)
      {
        DoorPanel doorpanel = (DoorPanel)e.getSource();
        System.out.println("We have a " + e.getActionCommand() + "!");
        for (int i = 0; i < doors.length; i++)
        {
          if (doors[i] != doorpanel)
          {
            doors[i].setButtonEnabled(false);
          }
        }
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    }
    DoorArrayTest.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class DoorArrayTest
    {
      private static void createAndShowUI()
      {
        final DoorArray doorArray = new DoorArray();
        JButton resetBtn = new JButton("Reset");
        resetBtn.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            doorArray.reset();
          }
        });
        JPanel resetPanel = new JPanel();
        resetPanel.add(resetBtn);
        
        JFrame frame = new JFrame("DoorArray");
        frame.getContentPane().add(doorArray.getComponent());
        frame.getContentPane().add(resetPanel, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    Last edited by Fubarable; 12-10-2008 at 06:15 AM.

  2. #22
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    Just another cross poster.
    New To Java - help with GUI

    db

  3. #23
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Just another cross poster.
    New To Java - help with GUI

    db
    yea i kno im not supposed to do that but i was havin a hard time gettin this figured out. however i finally did get and its ready to hand in. thanks alot for your help fubarable

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

    Default

    You're welcome, but I have to re-iterate Darryl's concerns. Your cross-posting will frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. For this reason, many volunteers here and at the other sites refuse to help repeat offenders.

Page 2 of 2 FirstFirst 12

Posting Permissions

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