My moving square leaving the boundaries of the panel is not noticed by Java

Could someone please tell me why neither System.out.println("Out of bounds!") nor JOptionPane.showMessageDialog(null, "Out of bounds!") get reached?
Here is the code:
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Bounds extends JPanel
    int counter = 0, x = 50, y = 50, width = 20, height = 20;
    public Bounds()
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        ActionListener listener = new graphicsListener();
        Timer timer = new Timer(2000, listener);
        timer.start();
    protected void paintComponent(Graphics g)
        super.paintComponent(g); // so the background gets refreshed
        g.drawRect(x+counter , y+counter, width+counter, height+counter);
        counter+=10;
        System.out.println("Panel width: " + getWidth() + " Panel height: " + getHeight());
        if(x+width >= getWidth() || y+height >= getHeight())
            System.out.println("Out of bounds!");
            JOptionPane.showMessageDialog(null, "Out of bounds!");
    public class graphicsListener implements ActionListener
        public void actionPerformed(ActionEvent event)
            repaint();
    public static void main(String[] args)
        new Bounds();
}Any help identifying the problem would be greatly appreciated!
Thanks in advance!

I changed it to this but still don't see why it's launching infinite JOptionPane windows:
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Bounds extends JPanel
    int x = 50, y = 50, width = 20, height = 20;
    Timer timer;
    public Bounds()
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        ActionListener listener = new graphicsListener();
        timer = new Timer(200, listener);
        timer.start();
    protected void paintComponent(Graphics g)
        super.paintComponent(g); // so that the background gets refreshed
        g.drawRect(x, y, width, height);
        System.out.println("Panel width: " + getWidth() + " Panel height: " + getHeight());
        System.out.println("x+width = " + (x+width));
        System.out.println("y+height = " + (y+height));
        if(x+width >= getWidth() || y+height >= getHeight())
            System.out.println("Out of bounds!");
            JOptionPane.showMessageDialog(null, "Out of bounds!");
            timer.stop(); // Why isn't this having an effect?
        x+=10;
        y+=10;
        width+=20;
        height+=20;
    public class graphicsListener implements ActionListener
        public void actionPerformed(ActionEvent event)
            repaint();
    public static void main(String[] args)
        new Bounds();
}As indicated in the line that says: timer.stop(), I suspected that the timer kept invoking graphicsListener but I can't actually find the bug.

Similar Messages

  • I have spotty signal at my home, so some functions don't work until I leave the house. I noticed on the main screen, it says I have 54 emails, but when I open the app, I only have 4. I powered down and back up. No fix. What gives?

    I have spotty signal at my home, so some functions don't work until I leave the house. I noticed on the main screen, it says I have 54 emails, but when I open the app, I only have 4. I powered down and back up. No fix. What gives?

    Im having this same problem an my phone is about 3 weeks old. I am not sure about the voice over part though...I have not yet figured out how to work the voice recognition part of the phone...I am such a newbie with this phone. I did the power off/on suggestion, several times. I tried leaving it off for 10 minutes. Tried to reset with the power on/off and home button pressed at the same time. Charged battery. Still makes no difference. You cannot scroll any direction. Have to double click to open apps. Have to double click letters when texting or emailing in order to get the letter in the text box. Can scroll through app choices, have to click on the line with the dots in it and move from page to page. Can't access the settings - > general - > accessibility - because it won't allow me to scroll down to "general".
    Anyone help you with your fix yet?

  • In Yahoo Mail, when composing an email I get an "Asking to leave this page" panel; it does not matter whether i choose stay or leave, the panel pops up again....and again...the only way out is to Force Quite Firefox and start all over...help please...

    In Yahoo Mail, when composing an email I get an "Asking to leave this page" panel; it does not matter whether i choose stay or leave, the panel pops up again....and again...the only way out is to Force Quite Firefox and start all over...help please...

    Thanks for responding....but isn't your potential solution designed for Windows users? I have a Mac.
    If it does apply to Mac, I am also hoping for something simpler as this could take weeks to isolate the problem and it would be more efficient to use another browser for mail
    Also, I should have added, this problem occurs only occasionally and seemingly only when I am taking some time to compose a message

  • Hi My name ali from iran  IDi I opened my iPad in China because the Chinese did not notice but I update my information  Unfortunately, due to the country's name to Iran sanctions not Sbn I chose to make Iraq  Now I'm able to take my Mastercard and softwar

    Hi
    My name ali from iran
    IDi I opened my iPad in China because the Chinese did not notice but I update my information
    Unfortunately, due to the country's name to Iran sanctions not Sbn I chose to make Iraq
    Now I'm able to take my Mastercard and software applications I buy from iTunes
    But I do not know how?, Please help me to register your MasterCard

    Dear Hi
    I would also be in itunes and App Store Get into English
    I've opened all of IDI in China is Chinese and I do not understand Software
    Program
    Please advise me please
    Thank you
    Ali

  • When transfering tracks across to ipod a sliding panel appears on the right hand side of itunes. after acknowleadging the transfer the panel does not slide back out of the way to alow another transfer.

    When transfering tracks to an ipod a sliding panel appears on the right hand side of itunes. This allows the selected track to be transfered. After registering this action the panel does not slide back out of the way to allow scrolling and more song selection. Any solutions..Help?

    I have an even simpler method. I use MyPublisher to print my books. I select their page layout for a two-page spread, drag and drop the image. DONE!  I understand that most people have their favorite places to have work done. I have been extremely pleased with MyPublisher.com, so I haven't even tried the book module in Lightroom.

  • In a word press moving photo gallery, the photos are not visible though slide change indicator advances

    http://sedonaharmony.org/
    On the above website there is a photo gallery moving banner at the top of this home page. When I first went on this site I could see the photos properly as they advanced. Then, the blog photos were changed and I could no longer see the photos. The banner on my screen is a complete blank even though the upper right circles indicate that "something" is progressing. When I first open the site I see a momentary flash of the site name graphic then the banner goes blank (white). Thanks for your help

    The script that loads the images doesn't seem to be working in Firefox 3.x versions including Firefox 3.6.23<br />
    The banner loads if JavaScript is disabled, but with JavaScript enabled a script replaces that banner with a slide show that fails to load the images.<br />
    It work in Firefox 4 and the current Firefox 7.0.1 version, so you will have to update if you want to visit that site.

  • I just purchased my Ipad 2 64g. the main reason i purchased was to use in my airplane for live gps tracking. i have downloaded the software with the moving map and the gps will not connect in the air. it works periodically while standing outside. Any sug?

    I just purchased my Ipad 2 64g. The main reason I purchased was to use the gps moving map feature. I have the paid AP but the gps does not work. Do I need an auxiliarry GPS?

    Is it just the wireless 64g, or the one with 3G?  If just wireless, you purchased the wrong one.  Also, if you just wanted GPS tracking, a GPS would have been a cheaper solution, no monthly charges.

  • White Square in the menu bar (Not a hardware issue)

    Hi there,
    I have a Macbook Pro 13" with Retina display (late 2012), and I recently updated Mac OS to Yosemite.
    After updating, I have noticed that there is a white square in my menu bar and I do not know how to fix it, I can tell that it isn't a dead pixel as I can zoom into it and it will not remain in the same place, it will zoom in normally as if it is 'stuck' on the screen, I will add some screenshots to explain my issue:
    As you can see here, there is a white square in the menu bar near the centre of the screen at the top.
    When I zoom in, it moves to suit, and if I place my cursor over the white square, then it disappears as if it is a layer below the cursor, below shows how it doesn't stay in the top of the screen when I zoom in:

    Maybe NSPopover (Safari's 'Show downloads' uses one, for example)?

  • On implementingthe paint method, the panel does not up

    Hi,
    I have a panel over which i am adding a second panel.
    the second panel appears but when i implement the paintmethod() of second panel, it does not show...
    what can be the reason???
    deepak

    hi!
    i included the statement super.paint(g); in the paint method and now i can see the panel.
    Following is the paint() method i have written, the on;y problem left is that i can't see the images i had drawn...otherwise line and rectangles are coming fine
    super.paint(g);
                        if(showImage)
                                  Point pe0 = new Point(180, 110);
                                  Point pe1 = new Point(180, 280);
                                  Point p0 = new Point(138, 182);
                                  Point ce0 = new Point(120, 75);
                                  Point ce2 = new Point(240, 75);
                                  Point ce1 = new Point(120, 380);
                                  Point ce3 = new Point(240, 380);
                                  g.drawImage(verticalCloudImage, (int)p0.getX() - 20, (int)p0.getY() - 85, this);
                                  g.drawImage(peImage, (int)pe0.getX(), (int)pe0.getY(), this);
                                  g.drawImage(peImage, (int)pe1.getX(), (int)pe1.getY()+35, this);
                                  g.drawImage(cloudImage, (int)ce0.getX()-88, (int)ce0.getY()-52, this);
                                  g.drawImage(ceImage, (int)ce0.getX(), (int)ce0.getY(), this);
                                  g.drawImage(cloudImage, (int)ce2.getX(), (int)ce2.getY()-45, this);
                                  g.drawImage(ceImage, (int)ce2.getX(), (int)ce2.getY(), this);
                                  g.drawImage(cloudImage, (int)ce1.getX()-88, (int)ce1.getY()-22, this);
                                  g.drawImage(ceImage, (int)ce1.getX(), (int)ce1.getY(), this);
                                  g.drawImage(cloudImage, (int)ce3.getX(), (int)ce3.getY()-22, this);
                                  g.drawImage(ceImage, (int)ce3.getX(), (int)ce3.getY(), this);
                                  int ceHeight = ceImage.getHeight(this);
                                  int ceWidth = ceImage.getWidth(this);
                                  int peHeight = peImage.getHeight(this);
                                  int peWidth = peImage.getWidth(this);
                                  int pHeight = pImage.getHeight(this);
                                  int pWidth = pImage.getWidth(this);
                                  g.drawLine((int)ce0.getX() + ceWidth/2, (int)ce0.getY() + ceHeight,
                                            (int)pe0.getX() + peWidth/2, (int)pe0.getY());
                                  g.drawLine((int)ce2.getX() + ceWidth/2, (int)ce2.getY() + ceHeight,
                                            (int)pe0.getX() + peWidth/2, (int)pe0.getY());
                                  g.draw3DRect((int)pe0.getX() + peWidth/2 - 3, (int)pe0.getY() + peHeight - 1,
                                                 5, (int)p0.getY() - (int)pe0.getY() - peHeight - 2+23, true);
                                  g.draw3DRect((int)p0.getX() + pWidth/2 - 10, (int)p0.getY() + peHeight - 1+20,
                                                 5, (int)pe1.getY() - (int)p0.getY() - peHeight + 2+15, true);
                                  g.fill3DRect((int)pe0.getX() + peWidth/2 - 3, (int)pe0.getY() + peHeight - 1,
                                                 5, (int)p0.getY() - (int)pe0.getY() - peHeight - 2+23, true);
                                  g.fill3DRect((int)p0.getX() + pWidth/2 - 10, (int)p0.getY() + peHeight - 1+20,
                                                 5, (int)pe1.getY() - (int)p0.getY() - peHeight + 2+15, true);
                                  g.drawLine((int)ce1.getX() + ceWidth/2, (int)ce1.getY(),
                                                 (int)pe1.getX() + peWidth/2, (int)pe1.getY() + peHeight+35);
                                  g.drawLine((int)ce3.getX() + ceWidth/2, (int)ce3.getY(),
                                                 (int)pe1.getX() + peWidth/2, (int)pe1.getY() + peHeight+35);

  • Moving layers leaves the old position lines ghosted on the screen!

    Hello guys,
    I don't know if this is an easy fix or not but this issue has randomly shown it's ugly head and is very irritating!
    When I move layers now from position A to position B, the old lines are still shown on screen in position A. Shown in this picture: http://i.imgur.com/6B1mcZq.jpg
    Any help on how to remove this issue would be greatly appreciated!

    Hi nskmartinez,
    For me it sounds like a hardware malfunction Either these lines are caused by internal monitor or graphic card itself. Easiest way to check this is connecting an external monitor. If the same happens on external monitor its graphic card problem and the mainboard needs replacing.
    If you need spare parts you can contact an authorized service provider in your country. The guys are able to order all Toshiba parts. Furthermore they can help you to replace the parts if you are not familiar with notebook disassembling.
    Sorry for this more or less standard answer but here its user to user forum only and if its really a hardware malfunction you need professional help to get spare parts and replace them. Furthermore you have already tried the recovery disk that should always and work and if not => Hardware problem.

  • Stop the cursor from leaving the frame/panel?

    Im making a simple target game where you shoot targets with the mouse. What I'd like to do is stop the mouse cursor from leaving the panel/frame. I've tried moving the cursor there with the Robot class, but 1) it doesnt lock it directly to the edge, 2) it doesnt stop at the top edge and left edge, 3) the bottom edge results in the cursor being placed in the wrong place and finally, 4) if u move the cursor fast enough it breaks out anyway :(
    Hope someone can help,
    Thanx.

    Wow nice responses. I have two questions:
    1) What is native code?Write the code to capture the mouse in C/C++ (Windows supports capturing the mouse cursor - dunno about linux or solaris), then write a JNI wrapper function so you can call it from within Java.
    >
    2) Is it difficult to make it full screen? I was
    thinking of doing that but the thought of rescaling
    everything (i.e. fonts, images etc) made me think
    otherwise.Fullscreen is easy, simply graphicsDevice.setFullscreenWindow(window);
    Once in fullscreen, you can change the resolution to whatever you like (limited to what is supported by the gfx card ofcourse :p), so you shouldn't have to worry about rescaling gfx 2much.
    >
    Thanx,
    Jon

  • Is there any listener when leaving the page?

    Hi,
    I use JDev 11g,
    I use this
    *((DCIteratorBinding)this.getBindings.get("MyIterator1")).getViewObject().getApplicationModule().getTransaction.isDirty();*
    to know if there is a change happen in database or not
    I want to check this change when I leave the page.Where can I write this code? Is there any listener when leave the page or not?
    Thank You.
    Sameh Nassar

    Thank You Sireesha,
    I make a class which implements interface PhaseListener and implement 2 methods (beforePhase and afterPhase).
    then I write
    <lifecycle>
    <phase-listener>MyClass</phase-listener>
    </lifecycle>
    in faces-config.xml file
    thank you.
    Sameh Nassar

  • My songs on ipad have a red circle with a red square in the middle and they don't play anyone know what is happening?

    Several  songs have the cloud symbol with a red arrow and I think they play. But the ones with the red circle and red square in the center do not play. Can this be fixed> What does it mean?
    Thanks in advance,

    See this Apple support document.
    http://support.apple.com/kb/ts1463
    You'll notice that the problem is usually associated with a failed/damaged hard drive, meaning you'll likely need to have your iPod serviced if you want to get it working again.
    B-rock

  • The film does not start when I press play

    When I am trying to play a sequense inside of Premier pro and press the "play" button nothing happen. The button changes to a square but the film does not play. What is the problem? Sometimes it work when I save and restart the program

    What audio devices are you running?
    Might be a Tablet?
    Are the drivers up to date?
    Try Disabling them  one by one ( temporarily)
    Hope some thing works for you

  • The JPanel did not show when the menu is selected

    My program consists a JMenu bar with sub menu items. When the user select on the menu items a panel with the gridbag layout will show with all the labels.but the panel did not show. Can anyone show the problem for me ?
    AdminFrameMain.java
    public class AdminFrameMain
         private DisplayMenuBar menubar;
         private DisplayToolBar toolbar;
         private DisplayStatusBar statusbar;     
         public AdminFrameMain()
              JFrame frame = new JFrame("S-League Administration Management System");
              Toolkit kit = frame.getToolkit();
              Dimension windowsize =kit.getScreenSize();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              Container content = frame.getContentPane();
              content.setLayout(new BorderLayout());
              menubar = new DisplayMenuBar(frame,content);
              toolbar = new DisplayToolBar(content);
              statusbar = new DisplayStatusBar(content);
              frame.setSize(800,600);
              frame.setVisible(true);
         public static void main(String [] args){
              AdminFrameMain tm = new AdminFrameMain();
    DisplayMenuBar.java
    public class DisplayMenuBar
         private JMenu addMenu,addTeamMenu;
         private JMenuBar bar = new JMenuBar();
         private JToolBar toolbar = new JToolBar();
         private JMenuItem addTeamItem;
         private JFrame setFrame;
         private Container setContent;
         private AddTeamManagement addTeamMang;
         public DisplayMenuBar(JFrame frame,Container c)
              setFrame = frame;
              setContent = c;
              SetMenuBar();
         public void SetMenuBar()
         setFrame.setJMenuBar(bar);
         addMenu = new JMenu("Add");
         //file menu items list
         //Add sub menu
         addTeamMenu = new JMenu("Team Management");
         addMenu.add(addTeamMenu);
         //Add sub menu items
         addTeamMenu.add(addTeamItem = new JMenuItem("Add Team"));
         addTeamMenu.setMnemonic('T');     
         addTeamItem.setMnemonic('T');
         //team items listener
         //addTeamItem.addActionListener(taskcommand);          
         addTeamItem.addActionListener(new ActionListener()
              public void actionPerformed(ActionEvent e)
                   AddTeamManagement addTeamMang = new AddTeamManagement(setFrame,setContent);
         bar.add(addMenu);
    AddTeamManagement.java
    public class AddTeamManagement
         private JPanel addTeamPanel;
         private JFrame frame;
         private JButton createTeamBt,resetTeamBt;
         private Container addTeamContent;
         private JTextArea teamDescTextArea,teamIndpTextArea;
         private JTextField teamNameField;
         private JLabel teamID,teamDesc,teamInfo,numOfPlayers,teamZone,playersNum;
         private GridBagLayout gridBag;
         private GridBagConstraints constraints;
         public AddTeamManagement(JFrame f,Container c)
              System.out.println("add team mg");
              addTeamPanel = new JPanel();
              frame = f;
              addTeamContent = c;
              gridBag = new GridBagLayout();
              addTeamPanel.setLayout(gridBag);
              addTeamPanel.setBackground(Color.pink);
              constraints = new GridBagConstraints();
              teamID = new JLabel("Team ID:");
              teamDesc = new JLabel("Team Description:");
              teamInfo = new JLabel("Team Info:");
              numOfPlayers = new JLabel("No Of Players:");
              playersNum = new JLabel("15 Maximun");
              teamZone = new JLabel("Team Zone:");
              constraints.fill = constraints.VERTICAL;
              constraints.weightx = 1;
              constraints.weighty = 0;
              addComponent(teamID,0,0,1,1);
              constraints.fill = constraints.VERTICAL;
              constraints.weightx = 1;
              constraints.weighty = 0;
              addComponent(teamDesc,1,0,1,1);
              constraints.fill = constraints.VERTICAL;
              constraints.weightx = 1;
              constraints.weighty = 0;
              addComponent(teamInfo,2,0,1,1);
              constraints.fill = constraints.VERTICAL;
              constraints.weightx = 1;
              constraints.weighty = 0;
              addComponent(numOfPlayers,3,0,1,1);
              constraints.fill = constraints.VERTICAL;
              constraints.weightx = 1;
              constraints.weighty = 0;
              addComponent(teamZone,4,0,1,1);
              System.out.println("showing");
              addTeamContent.add(addTeamPanel,BorderLayout.CENTER);
         public void addComponent(Component component,int row,int column,int width,int height)
              System.out.println("adding c");
              constraints.gridx = column;
              constraints.gridy = row;
              constraints.gridwidth = width;
              constraints.gridheight = height;
              gridBag.setConstraints(component,constraints);
              addTeamPanel.add(component);
              addTeamPanel.setVisible(true);
    }

    Hello,
    you are missing only one link, just add following line to your actionPerformed method of DisplayMenuBar class and all problem will be solved
    setContent.validate();
    Actually, Swing component does not updated automatically. when you do any changes to the component layout it will set that component as invalidated component. To update the view you need to call validate() method defined in JComponent class.
    Virus

Maybe you are looking for

  • HT4927 How do I retrieve photos from Time Machine on a new macbook air?

    My previous mac book pro had all my photos on it. I thought I saved the library to a disk, but was unsuccessful. I also backed up my photos to my Time machine daily.  Once my macbook and coffee got acquainted, it no longer worked. I am unable to retr

  • Integrate SAP B1 & SAP R/3, To BOBJ

    Dear All,               We have a requirement, wherein, the client has two platforms, viz SAP B1 & SAP R/3. They want the data to be displayed on a common platform, say BOBJ, can you please suggest, how to go forward with it. Cheers!!!!! Umang

  • Mac Mini KVM USB problem prevents boot when using certain USB ports

    Hi, Just purchased a brand new Mac Mini 2.4 Quad Core to use as a server. This morning we removed from the box & connected it to our NTI Unimux 16-port KVM. The KVM already has 10 Mac Mini's connected (various vintages, no problems). We connected the

  • Linked List mess

    Hello, I think I've gotten myself into a bit of a mess. I create a Linked List containing many strings. I do this for 10 separate tabs. I then want to combine Linked Lists into something else like a LinkedList again so I can then write that one combi

  • Java.lang.ClassNotFoundException: long

    I'm migrating a Weblogic Integration 8.5 sp5 application to Weblogic Integration 9.2 MP1. The application makes call to different JPDs through JPDProxies in java service class as follow: WorkflowRequest wfAdmissibility = (WorkflowRequest)JpdProxyImpl