0

I have a Battle class and a RightPanel class. What i'm lacking is the code to connect the two classes. Basically, what I want to do is to say, Okay select your weapon and then after clicking the button, I will set the piece's weapon and then the code will proceed to my if else statement, which I already finished.

public class Battle {

private MovePiece mpref;

//constructors
public Battle(MovePiece mpref){
    this.mpref =mpref;
}

//the battle system
public Piece winner (Piece piecetwo, Piece pieceone){
    while(pieceone.getRemlife()>0||piecetwo.getRemlife()>0){

       //what i want to code is Click wind, fire or earth button then
       pieceone.setAttack("wind");

       //then...

       if (pieceone.getAttack().equals("fire")){

             if(piecetwo.getAttack().equals("wind")){
                    int life = piecetwo.getLife();
                    life=life-1;
                    piecetwo.setLife(life);
                    System.out.println("Player 1 wins");
                    }
             else if(piecetwo.getAttack().equals("earth")){
                    int life = pieceone.getLife();
                    life=life-1;
                    pieceone.setLife(life);
                    System.out.println("Player 2 wins");
                    }
          //and so forth

}

My Right Panel class which has all the buttons looks like this...

public class RightPanel extends JPanel {

    public RightPanel(){
    this.setPreferredSize(dimension);
    this.setLayout(new GridBagLayout());

    JLabel option = new JLabel("Choose your weapon: ");
    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.insets = new Insets(0, 20, 0, 20);
    wpanel.add(option, gbc);

    //----Attack Buttons----//
    JPanel wabuttons = new JPanel(new GridLayout(0, 3, 3, 3));
    wabuttons.setEnabled(false);
    gbc.gridx = 0;
    gbc.gridy = 6;
    gbc.fill = GridBagConstraints.BOTH; 
    gbc.insets = new Insets(0, 20, 10, 20);
    wpanel.add(wabuttons, gbc);

     //Wind
        ImageIcon icnWind = new ImageIcon(imageFolderPath + "button/wind.png");
        Image imgWind = icnWater.getImage() ;  
        Image newWind = imgWater.getScaledInstance(25, 25, SCALE_SMOOTH ) ;  
        icnWind = new ImageIcon(newWind);
        btnWind = new JButton("",icnWind);
        btnWind.setEnabled(false);
        btnWind.setForeground(Color.BLUE);
        btnWind.setFont(new Font("Lucida Sans Unicode", Font.ITALIC, 15));
        btnWind.addActionListener(new actWind());
        wabuttons.add(btnWind);
       //Fire//
        ImageIcon icnFire = new ImageIcon(imageFolderPath + "button/fire.png");
        Image imgFire = icnFire.getImage() ;  
        Image newFire = imgFire.getScaledInstance(25, 25, SCALE_SMOOTH ) ;  
        icnFire = new ImageIcon(newFire);
        btnFire = new JButton("",icnFire);
        btnFire.setEnabled(false);
        btnFire.setForeground(Color.RED);
        btnFire.setFont(new Font("Lucida Sans Unicode", Font.ITALIC, 15));
        btnFire.addActionListener(new actFire());
        wabuttons.add(btnFire);
       //Earth//
        ImageIcon icnEarth = new ImageIcon(imageFolderPath + "button/earth.png");
        Image imgEarth = icnEarth.getImage() ;  
        Image newEarth = imgEarth.getScaledInstance(25, 25, SCALE_SMOOTH ) ;  
        icnEarth = new ImageIcon(newEarth);
        btnEarth = new JButton("",icnEarth);
        btnEarth.setEnabled(false);
        btnEarth.setForeground(Color.GREEN);
        btnEarth.setFont(new Font("Lucida Sans Unicode", Font.ITALIC, 15));
        btnEarth.addActionListener(new actEarth());
        wabuttons.add(btnEarth);

    }

    class actWind implements ActionListener {
    public void actionPerformed (ActionEvent e) {

    }


    class actFire implements ActionListener {
    public void actionPerformed (ActionEvent e) {

    }

    class actEarth implements ActionListener {
    public void actionPerformed (ActionEvent e) {

    }





}
user3266210
  • 299
  • 4
  • 15

1 Answers1

3

You could use an MVC or Model-View-Control pattern where the view is the GUI, the model is your Battle class, and the Control connects the two. For example:

import java.awt.event.*;
import javax.swing.*;

public class TestRightPanel {
   private static void createAndShowGui() {
      // create your pieces
      View view = new View();
      Model model = new Model();

      // and let the Control tie them all together
      Control control = new Control(model, view);

      JFrame frame = new JFrame("TestRightPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(view);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class View extends JPanel {
   private Control control;

   public View(Control control) {
      this();
      this.control = control;
   }

   public View() {
      JButton btnFire = new JButton(new FireAction("Fire"));
      add(btnFire);
   }


   public void setControl(Control control) {
      this.control = control;
   }


   private class FireAction extends AbstractAction {
      public FireAction(String name) {
         super(name);
         putValue(MNEMONIC_KEY, KeyEvent.VK_F);
      }

      public void actionPerformed(ActionEvent e) {
         if (control != null) {
           control.fire();  // communicate user input to control
         }
      }

   }
}

class Control {

   private Model model;
   private View view;

   public Control(Model model, View view) {
      this.model = model;
      this.view = view;
      view.setControl(this);
   }

   public void fire() {
      model.fire();
   }

}

class Model {

   public void fire() {
      // TODO finish this code          
   }

}

Also your code will likely need to take threading into consideration since your Battle class appears to have some code that is long running. If so, it cannot run on the Swing event thread, but rather in a background thread.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373