Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: java game

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java game

    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);
                }
     
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,973
    Thanks
    51
    Thanked 2,269 Times in 2,241 Posts

    Default Re: java game

    What value is in the r_wall variable?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default Re: java game

    Your design might be the problem. Components (JButtons) are placed in containers (JPanels) according to the container's layout manager. The layout manager divides the container into distinct areas that don't move around, bump into each other, or trade places with each other so that components placed in those areas don't touch, overlap (intersect), or collide.

    The components placed in the container according to the layout manager will have "next to", "above", "below", etc. relationships that the programmer could establish and keep track of to control what the components are "allowed" to do.

    If you simplify your program so that it does not require access to special graphics (use letters or titles), we could run it to better understand what you're trying to do.

    I also recommend you write a separate class to be the ActionListener for your javax.swing.Timer.

Similar Threads

  1. Game in Java
    By Freedom-Claw in forum Object Oriented Programming
    Replies: 2
    Last Post: March 27th, 2013, 03:50 PM
  2. Replies: 3
    Last Post: March 1st, 2013, 10:01 PM
  3. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 11:21 AM
  4. [SOLVED] Java Game
    By aman_chauhan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 3rd, 2012, 04:53 AM
  5. Need help for my java game
    By blunderblitz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:32 AM