Results 1 to 16 of 16
  1. #1
    PR0PHET is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Post Pac-Man in NetBeans- a Timer and jLabel/jPannel problem

    Hi everyone!

    While looking for a solution for my problem i discovered this site so I registered to see if anyone could help me out with this one...
    This is the description, I can post pieces of code or the entire program if required.

    So basically I am doing a Pac-Man game, using NetBeans.

    I use jLabels/jPannels to display the user controlled icon and obstacles, plus a timer and key event listener to move pac-man around.
    Example if the "down key" is pressed every 100ms the loop sets the icon's position to (x,y+1).
    I've double checked everything and pac-man travels correctly and reacts to the obstacles and borders properly.
    I've also already done a very basic method for a "ghost" which randomly travels around.

    The problem is as follows :

    Every time I change the text of a label or hide it, the labels which i move around
    (the jlabels that contain the pac-man icon and the ghost icon or jpannels)
    get reset to their starting position - (for example 0,0).
    Even if I put a setLocation right afterwards it just ignores it.

    Since this is my first time working with a Timer I've figured out there might be something I don't know.
    Anyone I've asked so far didn't know the answer, since I'm sure people here have more experience
    I believe someone will be able to provide me with a solution.


    Thank you very much.
    Last edited by PR0PHET; 11-11-2010 at 06:49 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You've obviously got a bug in your code, but myself, I have no idea what's wrong without seeing the code.

    I do suggest though that while it's fine to use NetBeans as an IDE, you probably shouldn't be using it to auto-generate your GUI especially now when you're just learning Java and Swing.

  3. #3
    PR0PHET is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    Sure i see. Well fact is I'm doing this as a school project. They've only thought us the basics and then showed us NetBeans. They recommended it to us and thought us how to use it. I don't know how to do make the GUI without NetBeans.

    My professor was the first one i've asked about this problem, but he had never used the timer function before and could't identify the problem.

    Here is the link to the NetBeans project.
    The code is a bit messy since this just a prototype, but you'll probably understand what it does - it's pretty simple.
    (uploaded with dropbox since the size of the file was too big to post as an attachment)
    Last edited by PR0PHET; 11-12-2010 at 12:10 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by PR0PHET View Post
    Here is the link to the NetBeans project.
    The code is a bit messy since this just a prototype, but you'll probably understand what it does - it's pretty simple.
    (uploaded with dropbox since the size of the file was too big to post as an attachment)
    Since most of us have many other responsibilities including families, jobs, and volunteer work, we won't have the time to wade through a large messy program. If you still need our help, your best bet is to clean up your code, shorten it to the bare essentials that shows your problem, and post it here. It should be the smallest compilable program that we can run and test and with it be able to observe your problem first hand, an SSCCE

    Either way, much luck!

  5. #5
    PR0PHET is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    Indeed i do understand people have higher priorities than fixing some random guys java problem. I thought that maybe someone might have been familiar with the problem. This is the first time that i'm asking for help online so i forgot about posting a proper test case. The code is not really so bad. One or two methods, but sure i should have provided an easy test case exposing the problem and i will as soon as i have some time (later today). I'm not in hurry so if anyone (either you or anyone else) could check it out when they have some time i would be most grateful. I started my project early because i had predicted i would have had problems of this kind.
    So please if you could check this out when you have some time it would be much appreciated.

    If i won't get a response i'll just ask someone else or try to improvise.

  6. #6
    PR0PHET is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    So i'm back. I've been a it busy and i didn't have the time to make it yesterday. Nevertheless here it is. It's a very basic application exposing my problem. Try starting it and then setting the jLabel text or set it to invisible/visible.

    As said no need to hurry, check it out when you can - if you can.
    I just really need to find a solution.

    http://dl.dropbox.com/u/14868285/javaTestCase.zip

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Closer but still not an SSCCE. For instance, many of us don't use NetBeans, and so all the org.jdesktop.application code and strange layouts aren't available, but also, that code really aren't necessary for your test case to demonstrate its problem. Same for the Logger. So again, I advise you to simplify, simplify. Use the most basic of gui's, most basic of layouts, delete all components unnecessary (such as the logger), and then post your resulting code here directly in the forum using code tags.

    edit: Also your code references resources and images that we don't have.

    You're getting there so don't give up. Luck!
    Last edited by Fubarable; 11-13-2010 at 04:05 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    OK, I studied your code quite a bit, and I see that you're doing your animation by using a Swing Timer to change the position of a component (jPanel1) held by another component (jPanel2 acting as the container) which uses the AbsoluteLayout as its layout manager. I am not very familiar with this layout manager, but I suspect that whenever your other buttons effect the GUI, this manager is being triggered to reset things to a starting position. Perhaps things will work better if you have the container, jPanel2 use a null layout. If you do this, you'll need to set the size and location of any components it holds, not the preferredSize as you're doing now.

    Not sure if you're going to read this, but I hope you do, cuz to be quite honestly, it took a bit of work to figure out. All I ask is that you please notify me if when you have read this, and let us know if it helps.. Thanks.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Here is my test case that shows the closest to an SSCCE that I could get to. I had to go NetBeans to download the code for the the AbsoluteLayout class. To make this work, comment out one of the setLayout lines and leave one uncommented, then on second run swap the commented vs. uncommented ones. While running the animation, click on the border of the GUI and change it. This will trigger the layout managers to do their things, and will show you that if you use AbsoluteLayout, the layout manager will bring the box back to its starting postion, while if you use null layout, it won't:

    Java Code:
       // !! ****** Comment out one or the other below to test condition ****** !! 
       private static final LayoutManager CONTAINER_LAYOUT = new org.netbeans.lib.awtextra.AbsoluteLayout();
       // private static final LayoutManager CONTAINER_LAYOUT = null;

    Here's my whole program:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JavaTestCaseApp2 extends JPanel {
       private static final long serialVersionUID = 1L;
       private static final int LENGTH = 400;
       private static final int BOX_SIDE = 50;
       private static final Dimension ANIMATION_CONTAINER_SIZE = new Dimension(LENGTH, 80);
       private static final Dimension MOVING_PANEL_SIZE = new Dimension(BOX_SIDE, BOX_SIDE);
       
       
       // !! ****** Comment out one or the other below to test condition ****** !! 
       private static final LayoutManager CONTAINER_LAYOUT = new org.netbeans.lib.awtextra.AbsoluteLayout();
       // private static final LayoutManager CONTAINER_LAYOUT = null;
    
       // I needed to use the one below to get this to work for me, but you won't need it.   
       // private static final LayoutManager CONTAINER_LAYOUT = new AbsoluteLayout(); 
       
    
       private static final Point STARTING_POS = new Point(10, 15);
       public static final int ANIMATE_DELAY = 10;
       private static final int DELTA_X = 1;
    
       private JPanel animationContainer = new JPanel();
       private JPanel movingPanel = new JPanel();
       private Timer animateTimer = null;
       
       public JavaTestCaseApp2() {
          animationContainer.setPreferredSize(ANIMATION_CONTAINER_SIZE);
          animationContainer.setLayout(CONTAINER_LAYOUT);
          
          movingPanel.setPreferredSize(MOVING_PANEL_SIZE);
          movingPanel.setSize(MOVING_PANEL_SIZE);
          movingPanel.setLocation(STARTING_POS);
          movingPanel.setBorder(BorderFactory.createLineBorder(Color.black));
          
          animationContainer.add(movingPanel, new AbsoluteConstraints(STARTING_POS));
    
          JButton animateBtn = new JButton("Animate");
          animateBtn.addActionListener(new AnimateListener());
          JPanel buttonPanel = new JPanel();
          buttonPanel.add(animateBtn);
          
          
          setLayout(new BorderLayout());
          add(animationContainer, BorderLayout.CENTER);
          add(buttonPanel, BorderLayout.SOUTH);
       }
       
       private class AnimateListener implements ActionListener {
    
          public void actionPerformed(ActionEvent e) {
             if (animateTimer == null || !animateTimer.isRunning()) {
                animateTimer = new Timer(ANIMATE_DELAY, new ActionListener() {
                   public void actionPerformed(ActionEvent e2) {
                      Point p = movingPanel.getLocation();
                      if (p.x + BOX_SIDE >= LENGTH) {
                         p = new Point(STARTING_POS.x, p.y);
                         ((Timer)e2.getSource()).stop();
                      } else {
                         p = new Point(p.x + DELTA_X, p.y);
                      }
                      movingPanel.setLocation(p);
                      animationContainer.repaint();
                   }
                });
                animateTimer.start();
             }
          }
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Java Test Case");
          frame.getContentPane().add(new JavaTestCaseApp2());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Fubarable, AbsoluteLayout? what was that all about? is it like CSS and can let you position by providing coordinates like setBounds on a null layout?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It's part of the NetBeans-specific library. It appears to be a "smarter" null layout, but it has enough differences from null layout including a desire to glue components to their positions, that it appears that it shouldn't be used for animation.

  13. #13
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Brilliant, thanks Fubarable. Netbeans should come with a "i got years of doing it myself" warning :D

  14. #14
    PR0PHET is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    Thank you, thank you very much. It works. I'm sorry i was trying to make an example using your instructions, but for some reasons the timer was giving me some strange errors. Plus i had a lot of stuff and studying to do which didn't give me enough time to make it.

    Thank you again for your help I really appreciate it. You are my savior :)
    I hope I haven't wasted to much of your time. I should have realized what was the problem while doing the SSCCE.

    Thank you again for all your effort and time, you are awesome.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  16. #16
    PR0PHET is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    Yeah i tried it and it works wonderfully. It's not the first time NetBeans is giving me problems like these, but usually they were easy to figure out or someone knew how to deal with them. This time i was lost.

    Thank you very much again. You've really helped me out.

Similar Threads

  1. problem with JLabel.setText();
    By nonabhai in forum AWT / Swing
    Replies: 5
    Last Post: 10-09-2010, 05:44 AM
  2. Jlabel update problem
    By fantasyme in forum AWT / Swing
    Replies: 3
    Last Post: 04-14-2010, 06:10 AM
  3. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-07-2010, 12:02 AM
  4. Swing JLabel Problem
    By g123456 in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 03:17 AM
  5. JLabel + GUI problem
    By tonyelaltaico in forum Java Applets
    Replies: 5
    Last Post: 02-03-2009, 02:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •