I’m working on a project and I need to pass a variable from the JavaReactor class to the GameGraphicsAndDigitalOperations class.
But when I try to do this to me in the GameGraphicsAndDigitalOperations class it GameGraphicsAndDigitalOperations 0.
Code:
import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.DefaultComboBoxModel; import javax.swing.JComboBox; import javax.swing.JLabel; import java.awt.Color; import java.awt.Font; import javax.swing.JButton; import javax.swing.ImageIcon; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.ActionEvent; import javax.swing.JTextField; import javax.swing.Timer; public class JavaReactor extends JFrame { private JPanel contentPane; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JavaReactor frame = new JavaReactor(); frame.setVisible(true); frame.setResizable(false); } catch (Exception e) { e.printStackTrace(); } } }); } public int i; private JTextField textField; private JTextField textField_1; public int Heat_reactor = 10; public int Energy_reactor = 0; public JComboBox comboBox; public int getIndex; public JavaReactor() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 0, 1200, 878); contentPane = new GameGraphicsAndDigitalOperations(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); GameGraphicsAndDigitalOperations GGADO = new GameGraphicsAndDigitalOperations(); comboBox = new JComboBox(); comboBox.setFont(new Font("standard 09_66", Font.PLAIN, 11)); comboBox.setForeground(Color.BLACK); comboBox.setBackground(Color.LIGHT_GRAY); comboBox.setBounds(934, 26, 240, 20); comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { getIndex = comboBox.getSelectedIndex(); System.out.println(getIndex+" "+e); }}); comboBox.setModel(new DefaultComboBoxModel(GGADO.aBuy)); contentPane.add(comboBox); JLabel lblBay = new JLabel("Buy:"); lblBay.setIcon(new ImageIcon("C:\\Users\\\u0412\u043B\u0430\u0434\u0438\u043A\\Desktop\\Make reactor java IMG\\Buy.png")); lblBay.setFont(new Font("standard 09_66", Font.PLAIN, 11)); lblBay.setBounds(849, 13, 130, 46); contentPane.add(lblBay); JLabel lblE = new JLabel("Energy:"); lblE.setFont(new Font("standard 09_66", Font.PLAIN, 11)); lblE.setBounds(849, 814, 95, 14); contentPane.add(lblE); textField_1 = new JTextField(); textField_1.setEditable(false); textField_1.setBounds(934, 749, 240, 20); contentPane.add(textField_1); textField_1.setColumns(10); textField = new JTextField(); textField.setEditable(false); textField.setBounds(934, 811, 240, 20); contentPane.add(textField); textField.setColumns(10); textField.setText(Integer.toString(Energy_reactor)); textField_1.setText(Integer.toString(Heat_reactor)); JButton btnNewButton_1 = new JButton("Heat -1"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if(Heat_reactor!=0) { Heat_reactor--; textField_1.setText(Integer.toString(Heat_reactor)); } } }); btnNewButton_1.setFont(new Font("standard 09_66", Font.PLAIN, 11)); btnNewButton_1.setBackground(Color.LIGHT_GRAY); btnNewButton_1.setBounds(849, 780, 325, 23); contentPane.add(btnNewButton_1); JLabel lblHeat = new JLabel("Heat:"); lblHeat.setFont(new Font("standard 09_66", Font.PLAIN, 11)); lblHeat.setBounds(849, 755, 95, 14); contentPane.add(lblHeat); /*Timer t = new Timer(1500,new ActionListener(){public void actionPerformed(ActionEvent e) {*/ //System.out.println(getIndex); //getIndex = comboBox.getSelectedIndex(); /*}});*/ //t.start(); } public int gi() { return getIndex; } } class GameGraphicsAndDigitalOperations extends JPanel { public String[] aBuy = {"Heat exchanger 50$","Heat inlet 200$","Heat outlet 200$","Heat vent 50$","Reactor rod Uranus 5$"}; public int CombotBoxGetSelect; public int XDRAWPOLE = 1; public int YDRAWPOLE = 1; public int getI; Boolean b=false; public GameGraphicsAndDigitalOperations() { addMouseListener(new myMouse1()); } public void paintComponent(Graphics g) { Image f = null; try{f = ImageIO.read(new File("C:\\Users\\Владик\\Desktop\\Make reactor java IMG\\Pole.png"));} catch(IOException ex){} g.drawImage(f,1,1,null); while(XDRAWPOLE<=20) { g.drawImage(f,XDRAWPOLE*40,1,null); XDRAWPOLE++; } XDRAWPOLE = 1; while(YDRAWPOLE<=20) { g.drawImage(f,1,YDRAWPOLE*40,null); while(XDRAWPOLE<=20) { g.drawImage(f,XDRAWPOLE*40,YDRAWPOLE*40,null); XDRAWPOLE++; } XDRAWPOLE = 1; YDRAWPOLE++; } YDRAWPOLE = 1; try{f = ImageIO.read(new File("C:\\Users\\Владик\\Desktop\\Make reactor java IMG\\Heat_vent_1.png"));} catch(IOException ex){} if(b) { g.drawImage(f,5,5,32,32,null); } } public class myMouse1 implements MouseListener { public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { JavaReactor Per = new JavaReactor(); getI = Per.getIndex; int tX = e.getX(); int tY = e.getY(); int col = e.getClickCount(); int btn = e.getButton(); Per.repaint(); System.out.println(Per.comboBox.getSelectedItem()); if((tX>=1)&&(tX<=40)&&(tY>=1)&&(tY<=40)&&(col==1)&&(btn==1)&&(getI==3)) { b=true; repaint(); } } public void mouseReleased(MouseEvent e) { } } }