So I am trying to pass a variable from a button press in one class to another class, and can't quite figure it out. The button press creates a random number to simulate a dice roll, adds it to a variable which then is suppose to be passed to the board class which has the game board built on it and will then use said variable to determine which space on the board the player is on. Thank you in advance.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Game extends JPanel{
private JLabel lblP1Name, lblP2Name, lblRules, lblDiceRoll;
private JTextField txtP1Name, txtP2Name;
private JButton diceRoll;
private JRadioButton rdP1, rdP2;
private int dice;
private static int countP1;
private int countP2;
private JPanel panelNorth;
private void groupButton( ) {
ButtonGroup bg1 = new ButtonGroup( );
bg1.add(rdP1);
bg1.add(rdP2);
}
public Game() throws FileNotFoundException {
setLayout (new BorderLayout());
rdP1 = new JRadioButton("Player 1");
rdP2 = new JRadioButton("Player 2");
ButtonListener listener = new ButtonListener();
Player1 player1 = new Player1(countP1);
Player2 player2 = new Player2(countP2);
Board board = new Board();
Rules rules = new Rules();
JButton diceRoll = new JButton("Roll the dice!");
panelNorth = new JPanel();
panelNorth.setLayout(new GridLayout(1,3));
lblRules = new JLabel(rules.toString());
add(panelNorth, BorderLayout.NORTH);
panelNorth.add(rdP1);
panelNorth.add(diceRoll);
panelNorth.add(rdP2);
Card card = new Card();
add(player1, BorderLayout.WEST);
add(player2, BorderLayout.EAST);
add(lblRules, BorderLayout.SOUTH);
add(board, BorderLayout.CENTER);
diceRoll.addActionListener(listener);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent diceRoll){
Random random = new Random();
dice = random.nextInt(6)+1;
if(rdP1.isSelected()){
countP1 = countP1+dice;
if(countP1>48){
countP1=countP1-48;
}
}else if(rdP2.isSelected()){
countP2 = countP2+dice;
if(countP2>48){
countP2=countP2-48;
}
}
}
}
}
It's simple; just use references.
Instance your classes and pass the reference:
Board board = new Board();
YourClass yourClass = new YourClass(board);
This way you can set Board attributes from the class YourClass.
It's really easy, you could have learned how to do this just by reading basic Java books.
Related
I want to be able to call the Introduction.Intro() method into my main file code, but it tells me I am unable to call a non-static method intro from a static context. Since I am still fairly new to coding I'm not entirely sure what the problem is. I've added my codes down below. I've tried countless online methods but sadly none have seemed to work.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Start extends JFrame implements ActionListener
{
private JFrame Main;
private JPanel PanelA, PanelB, PanelC;
private JLabel Text, ImageL;
private JButton Button;
private ImageIcon Image;
public Start ()
{
//Button
Button = new JButton("Start");
Button.addActionListener(new ButtonListener());
//Text
Text = new JLabel("Welcome To The Game"); //ADD NAME OF THE GAME
//Image
Image = new ImageIcon(getClass().getResource("download.jfif")); //ADD THE IMAGE FOR WELCOME
ImageL = new JLabel(Image);
//Top Panel (PanelA) - Image
PanelA = new JPanel();
PanelA.setBorder(BorderFactory.createEmptyBorder(0,200,150,200));
PanelA.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelA.add(ImageL);
//Middle Panel (PanelB) - Text
PanelB = new JPanel();
PanelB.setBorder(BorderFactory.createEmptyBorder(50,200,10,200));
PanelB.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelB.add(Text);
//Bottom Panel (PanelC) - Buttons
PanelC = new JPanel();
PanelC.setBorder(BorderFactory.createEmptyBorder(0,200,20,200));
PanelC.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelC.add(Button);
//Main Frame
Main = new JFrame ();
Main.add(PanelA, BorderLayout.NORTH);
Main.add(PanelB, BorderLayout.CENTER);
Main.add(PanelC, BorderLayout.SOUTH);
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Main.setTitle("GAME TITLE"); //ADD THIS LATER
Main.pack();
Main.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent ae)
{
}
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == Button)
{
Introduction.Intro1(); //THESE LINE RIGHT HERE
return null; //THESE LINE RIGHT HERE
}
}
}
public static void main(String[] args)
{
new Start();
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Introduction
{
private JFrame Main;
private JPanel PanelD;
private JLabel Text, ImageL;
private JButton Button;
private ImageIcon Image;
public void Intro()
{
Image = new ImageIcon(getClass().getResource("guy.jfif"));
ImageL = new JLabel(Image);
PanelD = new JPanel();
PanelD.setBorder(BorderFactory.createEmptyBorder(0,100,10,100));
PanelD.setLayout(new FlowLayout(FlowLayout.CENTER));
PanelD.add(ImageL);
PanelD.setVisible(true);
Main.add(PanelD, BorderLayout.NORTH);
}
}
EDIT: So I made another method in the Introduction class where I added this line of code, it managed to fix the error, however, the panel isn't being saved and my JFrame is outputting blank.
public static JFrame Intro1()
{
Introduction M = new Introduction();
return M;
}
If you are looking to initialize the Introduction class in main method of Start class, You can add belo code in main method after Start()
Introduction M = new Introduction();
You main method becomes :
public static void main(String[] args)
{
new Start();
Introduction M = new Introduction();
m.Intro
}
Looking at this set of code, It looks like there is incompatible issue, as you have declare JFrame as return type, while you are returning instance of Introduction.
public static JFrame Intro1()
{
Introduction M = new Introduction();
return M;
}
I am learning JRadioButtons and I do not know why it is working in the tutorial I am watching and not in my code. Could someone please take a look at it?
Main Class:
import java.awt.*;
import javax.swing.*;
public class Calculator extends JPanel{
private static final long serialVersionUID = 1L;
public static void main(String[] args){
Screen screen = new Screen();
screen.setVisible(true);
}
}
Here is the Screen Class:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
public class Screen extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JRadioButton b1, b2;
ButtonGroup group;
JTextArea tb;
public Screen(){
super("First GUI");
setSize(600,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel p1 = new JPanel();
JTextArea tb = new JTextArea("Text Area");
group = new ButtonGroup();
group.add(b1);
group.add(b2);
b1 = new JRadioButton("Hello");
b1.setActionCommand("HELLO!");
b1.addActionListener(this);
b2 = new JRadioButton("Goodbye");
b2.setActionCommand("Goodbye! =)");
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
p1.add(tb);
add(p1);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tb.setText(e.getActionCommand());
}
}
In my new Java head this should work perfectly. I initialize the buttons, initialize the group. I am getting an error after I click one of the buttons: AWT-EventQueue-0. I do not know what that means so I do not know how to fix this issue.
You have declared twice the same variable. If you declare the same variable (JTextArea tb) in the global and local scope, it will be a separate object. Remove the declaration in the local scope from the Screen() constructor so it will work.
Try this
tb = new JTextArea("Text Area");
instead of
JTextArea tb = new JTextArea("Text Area");
Because of your current code, tb is still not initialized in the global scope.
I want to know is it possible to re factor my code in such a way to remove duplication of almost identical statements, bar the variable name and what it is initialized to by using methods. Here is the code in question:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class Menu extends JFrame {
private JButton jbtChoc1 = new JButton("cross.gif");
private JButton jbtChoc2 = new JButton("nought.gif");
private JButton jbtChoc3 = new JButton("cross.gif");
private JButton jbtChoc4 = new JButton("nought.gif");
private JButton jbtChoc5 = new JButton("cross.gif");
private JButton jbtChoc6 = new JButton("nought.gif");
private JLabel foodLabelChoice = new JLabel("Main Dishes");
private ImageIcon food1Image = new ImageIcon("cross.gif");
private ImageIcon food2Image = new ImageIcon("nought.gif");
private ImageIcon food3Image = new ImageIcon("cross.gif");
private ImageIcon food4Image = new ImageIcon("nought.gif");
private ImageIcon food5Image = new ImageIcon("cross.gif");
private ImageIcon food6Image = new ImageIcon("nought.gif");
/**
* Constructor for the Menu.
*/
public Menu() {
Container cont = getContentPane();
cont.setLayout(new BorderLayout(5, 5));;
cont.setBackground(Color.white);
cont.add(foodLabelChoice, BorderLayout.NORTH);
JPanel girdSetup = new JPanel(new GridLayout(2, 3, 5, 5));
jbtChoc1.setIcon(food1Image);
girdSetup.add(jbtChoc1);
jbtChoc1.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc1.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc2.setIcon(food2Image);
girdSetup.add(jbtChoc2);
jbtChoc2.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc2.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc3.setIcon(food3Image);
girdSetup.add(jbtChoc3);
jbtChoc3.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc3.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc4.setIcon(food4Image);
girdSetup.add(jbtChoc4);
jbtChoc4.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc4.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc5.setIcon(food5Image);
girdSetup.add(jbtChoc5);
jbtChoc5.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc5.setHorizontalTextPosition(AbstractButton.CENTER);
jbtChoc6.setIcon(food6Image);
girdSetup.add(jbtChoc6);
jbtChoc6.setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChoc6.setHorizontalTextPosition(AbstractButton.CENTER);
cont.add(girdSetup, BorderLayout.CENTER);
}
/**
* Main method for test.
*
* #param args Initial setup.
*/
public static void main(String[] args) {
Menu frame = new Menu();
frame.setTitle("Menu");
frame.setSize(950, 400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I want to remove the duplication of the JButton and ImageIcon declaration and their settings within the constructor so that I simply call a single method instead of copy/paste of code.
You can do that by wrapping them in arrays, like this:
private static final String[] imageNames = {"cross.gif",
"nought.gif",
"cross.gif",
"nought.gif",
"cross.gif",
"nought.gif"};
private JButton[] jbtChocs = new JButton[imageNames.length];
private ImageIcon[] foodImages = new ImageIcon[imageNames.length];
public Menu() {
/* ... */
for(int i = 0; i < imageNames.length; i++){
jbtChocs[i] = new JButton(imageNames[i]);
foodImages[i] = new ImageIcon(imageNames[i]);
jbtChocs[i].setIcon(foodImages[i]);
girdSetup.add(jbtChocs[i]);
jbtChocs[i].setVerticalTextPosition(AbstractButton.BOTTOM);
jbtChocs[i].setHorizontalTextPosition(AbstractButton.CENTER);
}
}
Since the behavior is the same for all of them, this lets you simply iterate over each object, applying the same actions to each one.
I am learning the basics of Java and decided to practice working on objects, by making a simple program which I called a 'PlayerAction Counter'. It does nothing more that counting an 'action' which is fired by clicking '+1' button and additionally reset the counter, when the 'Reset' button is clicked. For this case I've made a Application.java which holds the main method and launches the program, Interface.java for GUI, EventHandling.java for events and Person.java which holds interface components and methods to change their state. It looks like this:
My idea was to create the window in Interface class and set it's layout, then create two Person objects called PlayerOne and Player Two. The Person class creates two labels and two buttons and set a label of one by a constructor parameter.
I've managed to add these two objects' elements to the GUI by getters and it looks just fine. I added ActionListener to both buttons in Person class and it also works just fine.
I am stuck on getting back to Person fields and methods from EventHandling class. I've created a setValueLabel() which increases by one a value of 'action' and sets it on the label, but I have to idea how to launch the method from another class. I can not make another object inside (throws StackOverFlow error), but should it not somehow refer to those two made in Interface class?
Here is the code:
Application.java
import java.awt.EventQueue;
public class Application {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Interface().setVisible(true);
}
});
}
}
Interface.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
public class Interface extends JFrame {
private JLabel titleLabel;
private JPanel mainPanel;
private JPanel leftPanel, rightPanel;
private JSeparator separator;
Person PlayerOne = new Person("PlayerOne");
Person PlayerTwo = new Person("PlayerTwo");
public Interface() {
//Basic init
setTitle("PlayerAction Counter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//Layout
mainPanel = new JPanel();
add(mainPanel);
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
leftPanel = new JPanel();
rightPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
leftPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
mainPanel.add(leftPanel, BorderLayout.WEST);
separator = new JSeparator(SwingConstants.VERTICAL);
mainPanel.add(separator, BorderLayout.CENTER);
titleLabel = new JLabel("PlayerAction Counter");
mainPanel.add(titleLabel, BorderLayout.PAGE_START);
//Components init
PlayerTwo.getPersonLabel().setAlignmentX(Component.CENTER_ALIGNMENT);
leftPanel.add(PlayerTwo.getPersonLabel());
PlayerTwo.getValueLabel().setAlignmentX(Component.CENTER_ALIGNMENT);
leftPanel.add(PlayerTwo.getValueLabel());
PlayerTwo.getValueUpButton().setAlignmentX(Component.CENTER_ALIGNMENT);
leftPanel.add(PlayerTwo.getValueUpButton());
PlayerTwo.getValueResetButton().setAlignmentX(Component.CENTER_ALIGNMENT);
leftPanel.add(PlayerTwo.getValueResetButton());
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
mainPanel.add(rightPanel, BorderLayout.EAST);
PlayerOne.getPersonLabel().setAlignmentX(Component.CENTER_ALIGNMENT);
rightPanel.add(PlayerOne.getPersonLabel());
PlayerOne.getValueLabel().setAlignmentX(Component.CENTER_ALIGNMENT);
rightPanel.add(PlayerOne.getValueLabel());
PlayerOne.getValueUpButton().setAlignmentX(Component.CENTER_ALIGNMENT);
rightPanel.add(PlayerOne.getValueUpButton());
PlayerOne.getValueResetButton().setAlignmentX(Component.CENTER_ALIGNMENT);
rightPanel.add(PlayerOne.getValueResetButton());
pack();
}
}
Person.java
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JLabel;
public class Person {
private JLabel personLabel;
private JLabel valueLabel;
private JButton valueUpButton;
private JButton valueResetButton;
private int actionValue = 0;
public Person(String s) {
personLabel = new JLabel(s);
setComponents();
}
private void setComponents() {
valueUpButton = new JButton("+1");
valueResetButton = new JButton("Reset");
valueLabel = new JLabel(""+actionValue);
EventHandling eventHand = new EventHandling(valueUpButton, valueResetButton);
valueResetButton.addActionListener(eventHand);
valueUpButton.addActionListener(eventHand);
}
public void setValueLabel() {
actionValue++;
valueLabel.setText("" +actionValue);
}
public JLabel getPersonLabel() {
return personLabel;
}
public JLabel getValueLabel() {
return valueLabel;
}
public JButton getValueUpButton() {
return valueUpButton;
}
public JButton getValueResetButton() {
return valueResetButton;
}
}
EventHandling.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
public class EventHandling implements ActionListener {
private JButton valueUpButton;
private JButton valueResetButton;
public EventHandling(JButton valueUpButton, JButton valueResetButton) {
this.valueUpButton = valueUpButton;
this.valueResetButton = valueResetButton;
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == valueUpButton) {
//no idea how to launch Person.setValueLabel();
//creating new Person object throws StackOverFlow error and I doubt that's the way
}
else if (event.getSource() == valueResetButton) {
}
}
}
I do hope my issue is understandable. The question is: how should it be done?
Please keep in mind that I am a complete rookie and try to learn OOP, but am very confused by it. Please point my mistakes in thinking and the code.
Any feedback will be greatly appraciated.
Thank you.
I'm writing a clone of Risk in Java, and am having some trouble with my code. When I create a new game, I create a JPanel with a JTextField (for the player name) and a JComboBox(for the player color), one panel for each player the user wants to create. Instances of this panel are created dynamically based on a second JComboBox which lets the user select a number of players from three to eight.
My problem is that when I want to create the player objects from the data entered into the player creation panel, each player object retrieves the data from the most-recently-created player creation panel. I have a functional solution, but I can't seem to figure out why what seems to be the 'proper' solution () won't work.
This is the code that I have working right now:
Creation panel class:
package risk;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PlayerCreatePanel extends JPanel{
public static ImageIcon[] playerColors = {Resources.red,Resources.green,Resources.blue,Resources.cyan,Resources.magenta,Resources.yellow,Resources.orange,Resources.gray};
public JComboBox playerColor;
public JTextField playerName;
public PlayerCreatePanel(int index){
this.setPreferredSize(new Dimension(360, 30));
JLabel numberLabel = new JLabel ("Player " + index + ": ");
JLabel nameLabel = new JLabel("Name: ");
JLabel colorLabel = new JLabel(" Color: ");
playerName = new JTextField("");
playerName.setColumns(13);
playerColor = new JComboBox(playerColors);
this.add(numberLabel);
this.add(nameLabel);
this.add(playerName);
this.add(colorLabel);
this.add(playerColor);
}
}
And for the new game class, the part that creates players:
package risk;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
public class NewGame {
private static PlayerCreatePanel [] panels = { new PlayerCreatePanel(1), new PlayerCreatePanel(2),
new PlayerCreatePanel(3), null, null, null, null, null};
private static int playerCount = 3;
public static void createPlayers(){
Resources.players = new Player[playerCount];
switch(playerCount){
case 8: Resources.players[7] = new Player (panels[7].playerName.getText(), Resources.colors[panels[7].playerColor.getSelectedIndex()], 8);
case 7: Resources.players[6] = new Player (panels[6].playerName.getText(), Resources.colors[panels[6].playerColor.getSelectedIndex()], 7);
case 6: Resources.players[5] = new Player (panels[5].playerName.getText(), Resources.colors[panels[5].playerColor.getSelectedIndex()], 6);
case 5: Resources.players[4] = new Player (panels[4].playerName.getText(), Resources.colors[panels[4].playerColor.getSelectedIndex()], 5);
case 4: Resources.players[3] = new Player (panels[3].playerName.getText(), Resources.colors[panels[3].playerColor.getSelectedIndex()], 4);
case 3: Resources.players[2] = new Player (panels[2].playerName.getText(), Resources.colors[panels[2].playerColor.getSelectedIndex()], 3);
Resources.players[1] = new Player (panels[1].playerName.getText(), Resources.colors[panels[1].playerColor.getSelectedIndex()], 2);
Resources.players[0] = new Player (panels[0].playerName.getText(), Resources.colors[panels[0].playerColor.getSelectedIndex()], 1); break;
default: break;
}
}
}
Now, what I've learned is the proper way to do this is to make my JTextField and JComboBox private, and write accessors, like so:
private JComboBox playerColor;
private JTextField playerName;
//...same method as above
public static String getName(){
return playerName.getText();
}
public static int getColorIndex(){
return playerColor.getSelectedIndex();
}
and change the creation lines in the new game method to read something like this:
Resources.players [0] = new Player (panels[0].getName(), panels[0].getColorIndex(), 1);