dimanche 3 mai 2015

Updating images with progress bars and creating array lists for a pop up box

public class MainGUI extends JPanel {

public JLabel characterName;
public JLabel statusBar1;
public JLabel energyBar1;
public JLabel actionBar1;
public JLabel characterPicture;
public JLabel characterStatus;
private JLabel crimeDetails;

public JButton fightCrimeButton;
public JButton commitCrimeButton;
public JButton sleepButton;
public JButton saveGameButton;
public JButton exitGameButton;
private JButton generateCrimeButton;


public JPanel actionPanel;
public JPanel energyPanel;
public JPanel statusPanel;
public JPanel characterPicture1;


private JProgressBar actionBar;
private JProgressBar energyBar;
private JProgressBar statusBar;



private Timer timer;



GridBagConstraints constraints= new GridBagConstraints();

public MainGUI(){

    setLayout(new GridBagLayout());
    constraints.insets= new Insets(0, 0, 10, 10);

    //characterName = new JLabel("Character name");
    //constraints.gridheight= 1;
    //constraints.gridwidth= 1;
    //constraints.gridx= 9;
    //constraints.gridy= 9;
    //characterName.setForeground(Color.blue);
    //add(characterName, constraints);

    generateCrimeButton = new JButton("Generate Crime");
    constraints.gridheight= 1;
    constraints.gridwidth=1;
    constraints.gridx= 1;



    add(generateCrimeButton, constraints);

    sleepButton = new JButton("Sleep");
    constraints.gridheight= 1;
    constraints.gridwidth= 1;
    constraints.gridx= 2;
    constraints.gridy= 0;

    sleepButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            int value = energyBar.getValue();
             int value1= actionBar.getValue();
            if(value <100 && value >0){
                energyBar.setValue(energyBar.getValue()+ 10);
            }
            if(value1 <100 && value1 > 0){
                actionBar.setValue(actionBar.getValue()+ 5);
            }
        }
    });
    add(sleepButton, constraints);

    fightCrimeButton = new JButton("Fight Crime");
    constraints.gridheight= 1;
    constraints.gridwidth= 1;
    constraints.gridx= 3;
    constraints.gridy= 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor= GridBagConstraints.WEST;

    fightCrimeButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            int value = energyBar.getValue();
            int value1= statusBar.getValue();
             int value2= actionBar.getValue();
            if(value >0){
                energyBar.setValue(energyBar.getValue()- 5);
            }
                if(value1 <100 && value1 >0){
                    statusBar.setValue(statusBar.getValue()+ 5);
                }
                if(value2 <100 && value2 > 0){
                    actionBar.setValue(actionBar.getValue()+ 5);
                }
            }

    });
    add(fightCrimeButton, constraints);

    commitCrimeButton = new JButton("Commit Crime");
    constraints.gridheight= 1;
    constraints.gridwidth= 1;
    constraints.gridx= 4;
    constraints.gridy= 0;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor= GridBagConstraints.WEST;

    commitCrimeButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            int value = energyBar.getValue();
            int value1= statusBar.getValue();
             int value2= actionBar.getValue();
            if(value >0){
                energyBar.setValue(energyBar.getValue()-5);
            }
            if(value1 <100 && value1 >0){
                statusBar.setValue(statusBar.getValue()-5);
            }
            if(value2 <100 && value2 > 0){
                actionBar.setValue(actionBar.getValue()+5);
            }
        }
    });
    add(commitCrimeButton, constraints);

    exitGameButton = new JButton("Exit Game");
    constraints.gridheight= 1;
    constraints.gridwidth= 1;
    constraints.gridx= 5;
    constraints.gridy= 0;


    exitGameButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            //remember to make an exit feature here
            System.exit(0);

        }
    });
    add(exitGameButton, constraints);

    actionPanel = new JPanel();
    constraints.gridheight= 3;
    constraints.gridwidth= 2;
    constraints.gridx=3;
    constraints.gridy= 2;
    actionPanel.setLayout(new BoxLayout(actionPanel, BoxLayout.Y_AXIS));
    constraints.fill= GridBagConstraints.HORIZONTAL;
    add(actionPanel, constraints);

    actionBar1= new JLabel("Action Bar");
    actionPanel.add(actionBar1);

    actionBar = new JProgressBar();
    actionBar.setStringPainted(true);
    actionBar.setValue(100);
    actionPanel.add(actionBar);

    energyPanel = new JPanel();
    constraints.gridheight= 2;
    constraints.gridwidth= 2;
    constraints.gridx=2;
    constraints.gridy= 2;
    energyPanel.setLayout(new BoxLayout(energyPanel, BoxLayout.Y_AXIS));
    constraints.fill= GridBagConstraints.HORIZONTAL;
    add(energyPanel, constraints);

    energyBar1= new JLabel("Energy Bar");
    energyPanel.add(energyBar1);

    energyBar = new JProgressBar();
    energyBar.setStringPainted(true);
    energyBar.setValue(100);
    energyPanel.add(energyBar);




    statusPanel = new JPanel();
    constraints.gridheight= 1;
    constraints.gridwidth= 2;
    constraints.gridx=4;
    constraints.gridy= 2;
    statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.Y_AXIS));
    constraints.fill= GridBagConstraints.HORIZONTAL;
    add(statusPanel, constraints);

    statusBar1= new JLabel("Status Bar");
    statusPanel.add(statusBar1);

    statusBar = new JProgressBar();
    statusBar.setStringPainted(true);
    statusBar.setValue(50);
    statusPanel.add(statusBar);




    characterPicture = new JLabel();
    if
    (statusBar.getValue()<10){
        ImageIcon image = new ImageIcon("images/supervillain.jpg");
        statusBar.setString("SUPERVILLAIN");
        characterPicture.setIcon(image);

    }
     if
     (statusBar.getValue()>=10 && statusBar.getValue()<40){
        ImageIcon image = new ImageIcon("images/villain.jpg");
        statusBar.setString("VILLAIN");
        characterPicture.setIcon(image);

    }
     if
     (statusBar.getValue()>=40 && statusBar.getValue()<60 ){
        ImageIcon image = new ImageIcon("images/human.jpg");
        statusBar.setString("HUMAN");
        characterPicture.setIcon(image);

    }
     if
     (statusBar.getValue()>=60 && statusBar.getValue()<80){
        ImageIcon image = new ImageIcon("images/Hero.jpg");
        statusBar.setString("HERO");
        characterPicture.setIcon(image);

    }
     if
     (statusBar.getValue()>=80){
        ImageIcon image = new ImageIcon("images/superhero.jpg");
        statusBar.setString("SUPERHERO");
        characterPicture.setIcon(image);
    }   
     constraints.gridheight= 3;
     constraints.gridwidth= 2;
     constraints.gridx= 0;
     constraints.gridy= 1;
     add(characterPicture, constraints);
     validate();


    ActionListener listener = new ActionListener(){
        public void actionPerformed(ActionEvent event){
            energyBar.setValue(energyBar.getValue()-1);
        }}; 
    ActionListener listener1 = new ActionListener(){
        public void actionPerformed(ActionEvent event){
            statusBar.setValue(statusBar.getValue()-2);
    }
        };
        ActionListener listener2 = new ActionListener(){
            public void actionPerformed(ActionEvent event){
                actionBar.setValue(actionBar.getValue()-3);
        }
            };  

    timer = new Timer(1000, listener);
    timer.start();
    energyPanel.add(energyBar);

    timer = new Timer(1000, listener1);
    timer.start();
    statusPanel.add(statusBar);

    timer = new Timer(1000, listener2);
    timer.start();
    actionPanel.add(actionBar);





}


}

From the code I have above I want the images to change depending on what level the status bar is on. for some reason it doesn't work for me. Also With the generateCrimeButton, I want to create a pop up window with three buttons, fight, commit and ignore crime.

Depending on which one you click in the pop up window the progress bars will increase/decrease. Also in the pop up window I want to have a random string appear, I know I have to create an ArrayList in a different class, but can someone help me out by telling me how to do all this?

Or by even giving me an example which has similar components so I can work around with the code and try to create these components in my game.

Aucun commentaire:

Enregistrer un commentaire