I am trying to have a Jbutton (blocker02) act like a wall that will not allow a moving Jbutton to pass through. I tried using " if (blocker02.getBounds().intersects(r_wall)) but it hasn't been successful.
Any suggestions on how to make this work? Thanks for any advice!
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.geom.Rectangle2D; public class myJPanel2 extends JPanel implements ActionListener { JButton blocker01,blocker02, ch1; int x,y,w,l; JButton jb1; JButton jb2; JButton jb3; JButton jb4; JLabel jl1; JLabel jl2; JLabel jl3; JLabel jl4; JLabel jl5; //Timers Timer gameClock; int limit = 0; int delay = 0; int i = 0; ClassLoader cl = this.getClass().getClassLoader(); //ImageIcon blue = new ImageIcon("/Users/Scott/NetBeansProjects/Assignment09_Team18_D2/src/arcade_blue_unpressed.png"); //ImageIcon yellow = new ImageIcon("/Users/Scott/NetBeansProjects/Assignment09_Team18_D2/src/arcade_yellow_unpressed.png"); //ImageIcon orange = new ImageIcon("/Users/Scott/NetBeansProjects/Assignment09_Team18_D2/src/arcade_orange_unpressed.png"); //ImageIcon red = new ImageIcon("/Users/Scott/NetBeansProjects/Assignment09_Team18_D2/src/arcade_red_unpressed.png"); //Image blackBackground = new ImageIcon("/Users/Scott/NetBeansProjects/Assignment09_Team18_D2/src/blackMetalTexturedBackgroundcrop03.png").getImage(); ImageIcon blue = new ImageIcon(cl.getResource("images/arcade_blue_unpressed01.png")); ImageIcon yellow = new ImageIcon(cl.getResource("images/arcade_yellow_unpressed01.png")); ImageIcon orange = new ImageIcon(cl.getResource("images/arcade_orange_unpressed01.png")); ImageIcon red = new ImageIcon(cl.getResource("images/arcade_red_unpressed01.png")); Image blackBackground = new ImageIcon(cl.getResource("images/blackMetalTexturedBackgroundcrop03.png")).getImage(); private Rectangle2D r_wall; public myJPanel2() { super(); //GridLayout grid = new GridLayout(1,1); //setLayout(grid); //setBackground(Color.green); jb1 = new JButton();//Start Button jb2 = new JButton();//Instructions Button jb3 = new JButton();//About Button jb4 = new JButton();//High Scores Button jl1 = new JLabel("Player: "); jl2 = new JLabel("Level: "); jl3 = new JLabel("Opponent: "); jl4 = new JLabel("Score: "); jl5 = new JLabel("Timer: "); //Items added to myJPanel2 add(jl1); add(jl2); add(jl3); add(jl4); add(jl5); add(jb1); add(jb2); add(jb3); add(jb4); //**The following commands for jb1-4 turn off the JButton borders so only the image is shown** //************************************* jb1.setFocusPainted(false); jb1.setContentAreaFilled(false); jb1.setBorderPainted(false); jb1.setIcon(blue); //************************************* jb2.setFocusPainted(false); jb2.setContentAreaFilled(false); jb2.setBorderPainted(false); jb2.setIcon(yellow); //************************************* jb3.setFocusPainted(false); jb3.setContentAreaFilled(false); jb3.setBorderPainted(false); jb3.setIcon(orange); //************************************* jb4.setFocusPainted(false); jb4.setContentAreaFilled(false); jb4.setBorderPainted(false); jb4.setIcon(red); //************************************* //JLabel settings----------------------------- jl1.setFont(new Font("Serif", Font.BOLD, 16)); jl1.setForeground(Color.white); jl1.setPreferredSize(new Dimension(180,16)); jl2.setFont(new Font("Serif", Font.BOLD, 16)); jl2.setForeground(Color.white); jl2.setPreferredSize(new Dimension(100,16)); jl3.setFont(new Font("Serif", Font.BOLD, 16)); jl3.setForeground(Color.white); jl3.setPreferredSize(new Dimension(140,16)); jl4.setFont(new Font("Serif", Font.BOLD, 16)); jl4.setForeground(Color.white); jl4.setPreferredSize(new Dimension(100,16)); jl5.setFont(new Font("Serif", Font.BOLD, 16)); jl5.setForeground(Color.white); jl5.setPreferredSize(new Dimension(535,16)); //-------------------------------------------- //------TIMER ------------------------------------------- delay = 1000; //milliseconds gameClock = new Timer(delay, this); }//End Public myJPanel2 //Override used to add a background image to myJPanel @Override public void paintComponent(Graphics g){ super.paintComponent(g); g.drawImage(blackBackground, 0, 0, this); }//End Override paintComponent public void actionPerformed(ActionEvent event) { Object obj = event.getSource(); //Timer Action displayed on Panel Initiated by "Begin Game" button //on optionJPanelD. //Action Listener for Timer Start is on myJPanel1 if (obj == gameClock) { i = i+1; jl5.setText("Timer: "+i); } if (obj==gameClock) { blocker02.setBounds(x,y,w,l); } if (blocker02.getBounds().intersects(r_wall)) { } else blocker02.setBounds(x,y,w,l); } }