This site requires JavaScript, please enable it in your browser!
Greenfoot back
Taeyo
Taeyo wrote ...

2020/9/23

hi, how do I run my ball image using jbutton arrow and players img using keyarrows the panel1 is set in Grid layout .(java eclipse)

Taeyo Taeyo

2020/9/23

#
import java.awt.EventQueue; import javax.swing.JFrame; import java.awt.GridLayout; import javax.swing.JPanel; import javax.swing.ImageIcon; import javax.swing.JButton; import java.awt.Color; import javax.swing.border.SoftBevelBorder; import javax.swing.border.BevelBorder; public class ballGame { public void ball(int m, int n) { walls.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\ball.png")); } public void player1(int a, int b) { walls.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\player1.png")); } public void player2(int a, int b) { walls.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\player2.png")); } JButton walls = new JButton; private JFrame frame; /** * Launch the application. */ public static void main(String args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ballGame window = new ballGame(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Asg2() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 1000, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JPanel panel = new JPanel(); panel.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null)); panel.setBounds(0, 92, 577, 458); frame.getContentPane().add(panel); panel.setLayout(null); JPanel panel_1 = new JPanel(); panel_1.setBackground(Color.WHITE); panel_1.setBounds(21, 21, 508, 408); panel.add(panel_1); panel_1.setLayout(new GridLayout(13, 16)); for(int i = 0;i<13;i++) { for(int j = 0; j<16; j++) { JButton wall = new JButton (); wall.setBackground(Color.white); wall.setBorderPainted(false); wall.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\grey.jpg")); walls=wall; if(j == 7 || j == 8) { wall.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\wall.jpg")); walls=wall; } panel_1.add(walls); } } ball(8, 3); player1(8, 1); player2(8, 14); JPanel panel_2 = new JPanel(); panel_2.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null)); panel_2.setBackground(new Color(240, 240, 240)); panel_2.setBounds(627, 92, 174, 397); frame.getContentPane().add(panel_2); panel_2.setLayout(null); JButton btnNewButton = new JButton("^"); btnNewButton.setBounds(62, 38, 69, 23); panel_2.add(btnNewButton); JButton button = new JButton("<"); button.setBounds(10, 81, 69, 23); panel_2.add(button); JButton button_1 = new JButton(">"); button_1.setBounds(89, 81, 69, 23); panel_2.add(button_1); JButton btnV = new JButton("v"); btnV.setBounds(62, 122, 69, 23); panel_2.add(btnV); } }
danpost danpost

2020/9/23

#
This line:
JButton walls = new JButton;
is suspicious (missing a parameter list on constructor call).
Taeyo Taeyo

2020/9/23

#
JButton walls = new JButton;
the jbutton walls and jbutton images are kept in gridlayout panel using 2d array
for(int i = 0;i<13;i++) { for(int j = 0; j<16; j++) { JButton wall = new JButton (); wall.setBackground(Color.white); wall.setBorderPainted(false); wall.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\grey.jpg")); walls=wall; if(j == 7 || j == 8) { wall.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\wall.jpg")); walls=wall; } panel_1.add(walls); }
danpost danpost

2020/9/23

#
This line:
JButton walls = new JButton;
is not a valid line of code.
danpost danpost

2020/9/23

#
Also, you create a ballGame object, yet do not show its code. Then, you show an Asg2 class and show no code creating an instance of that class.
Taeyo Taeyo

2020/9/23

#
import java.awt.EventQueue; import javax.swing.JFrame; import java.awt.GridLayout; import javax.swing.JPanel; import javax.swing.ImageIcon; import javax.swing.JButton; import java.awt.Color; import javax.swing.border.SoftBevelBorder; import javax.swing.border.BevelBorder; public class ballGame { public void ball(int m, int n) { walls.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\ball.png")); } public void player1(int a, int b) { walls.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\player1.png")); } public void player2(int a, int b) { walls.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\player2.png")); } private JFrame frame; /** * Launch the application. */ public static void main(String args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ballGame window = new ballGame(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public ballGame() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 1000, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JPanel panel = new JPanel(); panel.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null)); panel.setBounds(0, 92, 577, 458); frame.getContentPane().add(panel); panel.setLayout(null); JButton walls = new JButton; JPanel panel_1 = new JPanel(); panel_1.setBackground(Color.WHITE); panel_1.setBounds(21, 21, 508, 408); panel.add(panel_1); panel_1.setLayout(new GridLayout(13, 16)); for(int i = 0;i<13;i++) { for(int j = 0; j<16; j++) { JButton wall = new JButton (); wall.setBackground(Color.white); wall.setBorderPainted(false); wall.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\grey.jpg")); walls=wall; if(j == 7 || j == 8) { wall.setIcon(new ImageIcon("C:\\Users\\dell\\Downloads\\images (2)\\images\\wall.jpg")); walls=wall; } panel_1.add(walls); } } ball(8, 3); player1(8, 1); player2(8, 14); JPanel panel_2 = new JPanel(); panel_2.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null)); panel_2.setBackground(new Color(240, 240, 240)); panel_2.setBounds(627, 92, 174, 397); frame.getContentPane().add(panel_2); panel_2.setLayout(null); JButton btnNewButton = new JButton("^"); btnNewButton.setBounds(62, 38, 69, 23); panel_2.add(btnNewButton); JButton button = new JButton("<"); button.setBounds(10, 81, 69, 23); panel_2.add(button); JButton button_1 = new JButton(">"); button_1.setBounds(89, 81, 69, 23); panel_2.add(button_1); JButton btnV = new JButton("v"); btnV.setBounds(62, 122, 69, 23); panel_2.add(btnV); } }
danpost danpost

2020/9/24

#
For help on your buttons, refer to this page of the java tutoriaals. Also, you can review the code that goes along with it.
Taeyo Taeyo

2020/9/24

#
the code page is error
danpost danpost

2020/9/24

#
Taeyo wrote...
the code page is error
Try this.
You need to login to post a reply.