Draw a square with the cursor

i would like to draw a square with the movement of the coursor, while its left button is clicked and the cursor is over an image.
when i leave of pushing the right button the square have to dissapear.
if you don't understand, it's the same in windows when you click the left button and square appears.
My question is how can i do this?
thanks

Not sure I completely understand the question, but maybe this will give you an idea:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class DrawSquare extends JPanel
     Point startPoint = null;
     Point endPoint = null;
     public DrawSquare()
          setPreferredSize(new Dimension(500,500));
          MyMouseListener ml = new MyMouseListener();
          addMouseListener(ml);
          addMouseMotionListener(ml);
          setBackground(Color.white);
     public void paintComponent(Graphics g)
          // automatically called when repaint
          super.paintComponent(g);
          g.setColor(Color.black);
          if (startPoint != null && endPoint != null)
               // draw the current dragged line
               int x = Math.min(startPoint.x, endPoint.x);
               int y = Math.min(startPoint.y, endPoint.y);
               int width = Math.abs(endPoint.x - startPoint.x);
               int height = Math.abs(endPoint.y - startPoint.y);
               g.drawRect(x, y, width, height);
     class MyMouseListener extends MouseInputAdapter
          public void mousePressed(MouseEvent e)
               if (SwingUtilities.isLeftMouseButton(e))
                    startPoint = e.getPoint();
                    repaint();
          public void mouseReleased(MouseEvent e)
               if (SwingUtilities.isLeftMouseButton(e))
                    startPoint = null;
                    endPoint = null;
//                    repaint();
          public void mouseDragged(MouseEvent e)
               if (SwingUtilities.isLeftMouseButton(e))
                    endPoint = e.getPoint();
                    repaint();
     public static void main(String[] args)
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          DrawSquare d = new DrawSquare();
          frame.getContentPane().add( d );
          frame.pack();
          frame.setVisible(true);
}

Similar Messages

  • I'm having problems (1)selecting onscreen text, (2) having problems resizing menu boxes and selecting menues with the cursor. I'm not able to select menus and move them. I'm not sure how to correct this.

    I'm having problems (1) selecting onscreen text, (2) resizing menu boxes and selecting menues with the cursor. I'm not able to select menus and move them. I'm not sure how to correct this.

    1) This is because of software version 1.1. See this
    thread for some options as to how to go back to 1.0,
    which will correct the problem...
    http://discussions.apple.com/thread.jspa?threadID=3754
    59&tstart=0
    2) This tends to happen after videos. Give the iPod a
    minute or two to readjust. It should now be more
    accurate.
    3) This?
    iPod shows a folder icon with exclamation
    point
    4) Restore the iPod
    5) Try these...
    iPod Only Shows An Apple Logo and Will Not Start
    Up
    iPod Only Shows An Apple Logo
    I think 3,4, and 5 are related. Try the options I
    posted for each one.
    btabz
    I just noticed that one of the restore methods you posted was to put it into Disk Mode First rather than just use the resstore straight off, I Have tried that and seems to have solved the problem, If it has thank you. previously I have only tried just restoring it skipping this extra step. Hope my iPod stays healthy, if it doesnt its a warrenty job me thinks any way thanks again

  • Tecra M2 - black screen with the cursor flashing

    Hi guys,
    Im hoping someone out there may be able to help me fix my laptop.
    Ive read a couple of posts but not sure they can help me.
    When I swicth on my Tecra M2, I just end up with a black screen with the cursor flashing in the top left or I get a screen filled with alternating 0's and 1's.
    Sometimes the TOSHIBA splash screen appears with the boot options, other times it doesnt.
    I cannot boot to CD and it wont boot to the HDD. Ive taken the HDD out and the CDROM out but it does the same thing which to me suggests that its not getting through the BIOS even as far as the boot sequence.
    Does this sound like BIOS failure to you and do you know of a way to fix it? Ive tried taking the battery out, powering off and starting again but the same thing happens.
    Any advice would be be appreicated.
    Thanks,
    Ross

    I have had this twice now and in both cases it was caused by a faulty HDD - to confirm that the motherboard etc is still OK load the recovery CD in the DVD drive (or even better a freestanding XP disc), press F2 on startup and shift the cursor under the DVD drive as the boot drive.
    If the motherboard etc is OK you will get the reload instructions (from the recovery disc) or a Windows install screens (from the XP install disc).
    Satisfied that the machine at least works - replace the HDD and recover from the recovery disc or try a Windows repair from the recovery console (from the XP install disc with the HDD still in place).
    In my cases I was forced to recover to a new HDD and then rebuild the machine from backups - I now clone the HDD when rebuilt.

  • Wallpaper with the cursor when connected to external display

    Hi guys. I recently bought a MacBook 13" unibody polycarbonate white and I had bad luck and cracked the screen and I don't have the money to get it repaired so I decided to buy a mini display to VGA and hook it up to my old dell monitor.
    I turned off the computer and plugged the port in and it connects to the monitor but I only see the wallpaper with the cursor and no login screen or anything. I was reading around and found out that I might have to play with the resolution to make the monitor work. Now my delimma is that my LCD screen is cracked so how can I make the monitor work without looking ?

    Can you see anything on your MacBook screen? If you can access System Preferences you can drag the window over to the other screen. You have the display set in Extended Desktop mode. In System Preferences>Display on the MacBook screen there should be an Arrangement tab when you have the MacBook hooked up to the TV and both screens working. When you click the Arrangement tab do you see two monitors side by side? One of them will have a Menu Bar at the top. Just click on the Menu Bar and drag it to the second monitor. That will make the second monitor your main screen. You can now use your MacBook in Clamshell Mode with a wired or Bluetooth keyboard and mouse.  http://support.apple.com/kb/HT3131 When you disconnect from the TV your Menu Bar will automatically change back to the MacBook.
    Or if you want to work on the MacBook screen while showing it on a TV you can check the Mirror Display box on the lower left hand side of the Arrangement tab under the two monitors box.

  • I reinstalled my windows xp now the firefox icon on my desktop is now a white square with the firefox inside

    I reinstalled my windows xp now the firefox icon on my desktop is now a white square with the firefox inside how do I fix it

    Did you try to change that icon in the Properties via the right-click context menu?

  • The first DVD menu doesn't support "clicking" with the cursor

    I just created a disc image of my iDVD project and I have found that when I run it using the DVD Player, I can't navigate the first DVD menu with the cursor. The only way I can move through it is by using the arrow keys and then pressing "return" or "enter."
    When I preview my project in iDVD I don't have this problem. Any ideas as to what is going on here? Has this question been asked and addressed? I wasn't able to find any posts that outlined a similar problem...
    Any help would be greatly appreciated.
    Thanks,
    Aaron

    Has this question been asked and addressed? I wasn't able to find any posts that outlined a similar problem...
    Any help would be greatly appreciated.
    Let me know if any of these links are helpful:
    http://discussions.apple.com/thread.jspa?messageID=5940413&#5940413
    http://discussions.apple.com/thread.jspa?messageID=5575979&#5575979
    http://discussions.apple.com/thread.jspa?messageID=5402707&#5402707
    http://discussions.apple.com/thread.jspa?messageID=5550606&#5550606

  • TS1702 The app works but won't show the icon on the screen-its just a blank square with the app title.

    Any ideas on how to fix this problem? Some of the app icons are just a blank square with the name of the app. They still work. I've tried to resync but get same result.

    What about completing removing these apps from the iPod Touch and redownloading them from the App Store?
    B-rock

  • I am running Elements 11 on Windows 8.  When I use the healing tool, the entire image moves with the cursor.  How do I fix???  Thanks!

    I am running Elements 11 on Windows 8.  When I use the healing tool, the entire image moves with the cursor.  How do I fix???  Thanks!

    With the Healing Brush Tool selected, in the Tool Options, click on Clone Overlay and check Clipped.

  • Need help on drawing multiple squares on the screen

    I am working on a project for aircraft seating and what i have to do is create square on the screen as seat. I got to draw 180 squares on the screen and it sounds easy but i have been stuck at it for about 10 days now my superviser doesn't know java so he can't help me on what i should do next and java superviser's told me they have no time to see me so if anyone here can help to guide me where i am goin wrong or how i should do it would be very helpful. Here is the code i have so far i think i am maybe close to gettin it working but then again i could be far off.
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class Seating extends JFrame {
        private JPanel boardPanel;
        private JPanel panel2;
        private Square board[][];
        public Seating() {
            boardPanel = new JPanel();
            boardPanel.setLayout(new GridLayout(6,30,0,0));
            //create board
            board = new Square[6][30];
            //rows in the board
           for(int row = 0; row < board.length; row++)
               //column on the board
                for(int column = 0; column < board[row].length; column++)
                    //create square
                    board[row][column] = new Square(' ', 3 * row + column);
                    //add square
                    boardPanel.add(board[row][column]);
                panel2 = new JPanel();
                panel2.add(boardPanel, BorderLayout.CENTER);
                add(panel2, BorderLayout.CENTER);
                setSize(2000,1500);
                setVisible(true);
                 private class Square extends JPanel
                    public Dimension getPreferedSize()
                      return new Dimension(20,20);
                    public Dimension getMinimumSize()
                        return getPreferedSize();
                    public void paintComponent(Graphics g)
                        super.paintComponents(g);
                        g.drawRect(0, 0, 19, 19);
    }

    Yeah I'd make a Seat class, subclassing JLabel, and then add some business logic to them to reflect that. For example, if they're already booked, given them a different color and disable clicks on them. If they're bookable, add a mouse click listener. Maybe I'd make different subclasses of Seat for those different roles. I'd add them all to the grid, but also add them to a Map indexed by seat name or something so I could find them and update them as needed.

  • When I create a rectangle, I can not to change the shape of a rectangle with the cursor in illustrator CC 2014 For what?

    When I create a rectangle, I can not to change the shape of a rectangle with the cursor in illustrator CC 2014 For what?

    Xavigarcia,
    I am afraid you have come across the Live Rectangle bug which is limited to the MAC versions starting with 10.7 and 10.8, but not 10.9 (Mavericks) or 10.10 (Yosemite). Hopefully, the bug will be fixed soon.
    So a switch to Mavericks or Yosemite with a reinstallation might be the way to solve it here and now.
    To get round it in each case, it is possible to Expand the Live Rectangles to get normal old fashioned rectangles, or to Pathfinder>Unite, or to use the Scale Tool or the Free Transform Tool.
    A more permanent way round that is to create normal old fashioned rectangles, after running the free script created by Pawel, see this thread with download link:
    https://forums.adobe.com/thread/1587587

  • Is anyone having trouble with the cursor jumping around since Lion was installed on your computer? Never happened until Lion.

    Is anyone having trouble with the cursor jumping around while typing since you installed Lion? Never had any problems of that sort prior to Lion. I have an older machine. I believe it is a 2008 MacBook Pro. Especially happens with Yahoo. I am baffled.

    Use the trackpad to scroll, thats what it was designed for. The scroll bars automatically disappear when not being used and will appear if you scroll up or down using the trackpad.
    This is a user-to-user forum and most people will post on here if they have problems. You very rarely get people posting to say there update went smooth. The fact is the vast majority of Mountain Lion users will not be experiencing any major problems with the OS, or maybe with apps which are not compatible, but thats hardly Apple's fault if developers don't update their apps.

  • My outlook email sent page seems locked with the cursor changing to a rotating rainbow coloured ball - how do I stop it?

    My outlook email sent page seems locked with the cursor changing to a rotating rainbow coloured ball - how do I stop it?

    Hello,
    Open Console in Applications>Utilities & see if there are any clues or repeating messages when this happens.

  • Macbook does not shut down or restart. It stops at an blank white screen with the cursor moving around it. Updated to the latest software

    I have a Macbook Air 13.3" 2012 model. For a couple of days my Macbook does not shuts down. It just stops at a blank white screen with the cursor still hovering around it.  I have to shut it down forcefully every time using the power button.

    These instructions must be carried out as an administrator. If you have only one user account, you are the administrator.
    Launch the Console application in any of the following ways:
    ☞ Enter the first few letters of its name into a Spotlight search. Select it in the results (it should be at the top.)
    ☞ In the Finder, select Go ▹ Utilities from the menu bar, or press the key combination shift-command-U. The application is in the folder that opens.
    ☞ Open LaunchPad. Click Utilities, then Console in the icon grid.
    Step 1
    For this step, the title of the Console window should be All Messages. If it isn't, select
              SYSTEM LOG QUERIES ▹ All Messages
    from the log list on the left. If you don't see that list, select
              View ▹ Show Log List
    from the menu bar at the top of the screen.
    In the top right corner of the Console window, there's a search box labeled Filter. Initially the words "String Matching" are shown in that box. Enter "BOOT_TIME" (without the quotes.)
    Each message in the log begins with the date and time when it was entered. Note the timestamps of the BOOT_TIME log messages, which refer to the times when the system was started. Now clear the search box and scroll back in the log to the last boot time when you had the problem. Select the messages logged before the boot, during the time something abnormal was happening. Copy them to the Clipboard by pressing the key combination command-C. Paste into a reply to this message by pressing command-V.
    For example, if the system was unresponsive or was failing to shut down for three minutes before you forced a restart, post the messages timestamped within three minutes before the boot time, not after. Please include the BOOT_TIME message at the end of the log extract—not at the beginning.
    If there are long runs of repeated messages, please post only one example of each. Don’t post many repetitions of the same message.
    ☞ When posting a log extract, be selective. A few dozen lines are almost always more than enough.
    ☞ Some private information, such as your name, may appear in the log. Anonymize before posting.
    Please don't indiscriminately dump thousands of lines from the log into this discussion.
    Please don't post screenshots of log messages—post the text.
    Step 2
    In the Console window, select
              DIAGNOSTIC AND USAGE INFORMATION ▹ System Diagnostic Reports
    (not Diagnostic and Usage Messages) from the log list on the left. If you don't see that list, select
              View ▹ Show Log List
    from the menu bar.
    There is a disclosure triangle to the left of the list item. If the triangle is pointing to the right, click it so that it points down. You'll see a list of reports. A crash report has a name that begins with the name of the crashed process and ends in ".crash". A panic report has a name that begins with "Kernel" and ends in ".panic". A shutdown stall report has a name that ends in ".shutdownstall". Select the most recent of each, if any. The contents of the report will appear on the right. Use copy and paste to post the entire contents—the text, not a screenshot. It's possible that none of these reports exists.
    I know the report is long, maybe several hundred lines. Please post all of it anyway.
    ☞ If you don't see any reports listed, but you know there was a crash or panic, you may have chosen Diagnostic and Usage Messages from the log list. Choose DIAGNOSTIC AND USAGE INFORMATION instead.
    In the interest of privacy, I suggest that, before posting, you edit out the “Anonymous UUID,” a long string of letters, numbers, and dashes in the header of the report, if it’s present (it may not be.)
    ☞ Please don’t post other kinds of diagnostic report—they're very long and rarely helpful.

  • How to select a set of points in a xy graph contained in an area draw with the cursor ?

    I'm using tIhe XY graph to plot a Pointcare representation of RR intervals (heart beat). I would like to use the mouse to draw a polygone surrounding the points of interest and get their indexes from the original array. Thank you to oriented me to some strategy!
    Olivier 
    Solved!
    Go to Solution.

    This can be done, but unfortunately is not trivial. Since this is the second request I have seen in the last week for this type of functionality, I would encourage you to post the idea to the LabVIEW Idea Exchange.
    All graphs have a front, middle, and back image, which is set using a picture control data type.  You can use this to draw your cursor using an unfilled rectangle.  You can capture mouse events on the XY graph using the event structure, and update your enclosed rectangle (or ellipse, or whatever you would like to draw) when the mouse moves.  You can extract the enclosed data points from your original data set when a mouse up event occurs or when the user clicks inside the rectangle after drawing it.  You may also want to include ways to tweak the boundaries of the rectangle.
    This is relatively straightforward if you have used the event structure before, but could be very confusing if not.  Let us know if you need more information or help.
    This account is no longer active. Contact ShadesOfGray for current posts and information.

  • Sometimes when I open my laptop back from sleep, I get a black screen with the cursor showing.

    Recently I've been getting this weird problem where my macbook air will show a black screen with only the cursor visible that I can move around but nothing else can be done to get to the desktop. The only way is to close the lid and wait for around 10 or more seconds and then open the lid again to bring the laptop back from sleep and hope that the laptop goes to the lock screen and then to the desktop. This works often but sometimes the only solution seems to be to restart the laptop. This also happens when I tilt the lid to the point where it is almost closed and when I tilt it back the screen presents the above state. Theis has been happening often now and it is almost impossible for the above not to happen atleast 2 to 3 times a day. iThis is a relatively new problem that has been occuring since the past two weeks or so and never happened to me on my Macbook Pro 15 2011. My current system is a Macbook Air 11" 2013 Haswell 1.3 Ghz 4GB RAM 256 GB SSD. Now I dont want to have to send in my laptop if I dont have to if there is a way I can solve this myself. Anybody else have this problem?

    Hello tseten,
    I would be concerned too if my MacBook Air was not waking from sleep appropriately.  For an issue like this, I recommend resetting your System Management Controller (SMC) and NVRAM.
    Resetting the SMC on portables with a battery you should not remove on your own
    Note: Portable computers that have a battery you should not remove on your own include MacBook Pro (Early 2009) and later, all models of MacBook Air, and MacBook (Late 2009).
    Shut down the computer.
    Plug in the MagSafe power adapter to a power source, connecting it to the Mac if its not already connected.
    On the built-in keyboard, press the (left side) Shift-Control-Option keys and the power button at the same time.
    Release all the keys and the power button at the same time.
    Press the power button to turn on the computer. 
    Note: The LED on the MagSafe power adapter may change states or temporarily turn off when you reset the SMC.
    You can find the full article here:
    Intel-based Macs: Resetting the System Management Controller (SMC)
    http://support.apple.com/kb/ht3964http://support.apple.com/kb/ht3964
    Resetting NVRAM / PRAM
    Shut down your Mac.
    Locate the following keys on the keyboard: Command (⌘), Option, P, and R. You will need to hold these keys down simultaneously in step 4.
    Turn on the computer.
    Press and hold the Command-Option-P-R keys before the gray screen appears.
    Hold the keys down until the computer restarts and you hear the startup sound for the second time.
    Release the keys.
    You can find the full article here:
    About NVRAM and PRAM
    http://support.apple.com/kb/ht1379
    Thank you for using Apple Support Communities.
    Best,
    Sheila M.

Maybe you are looking for