How to combine the menu, frame, panel,button n their action/mouse listeners

Hi there,
I have my code below and I wish to make a program in which you have to click somewhere in the window and it will generate either an ellipse or rectangle. I tried to do that by making radio buttons for ellipse and rectangle. So if the user chooses any of the above and clicks somewhere inside the window then it draws that shape on the particular location clicked at. You can also set the color of the shape by clicking onto the color menu (either red,blue or green). But i am unable to implement the mouselistener as i am confused where should i implement it. In the rectangle panel or in constructor. Basically i get confused as to where i should put a particular listener. here is my example below and i compiled it but it doesn't work as required givin me errors.
Thank You
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.*;
import javax.swing.JMenu;
import javax.swing.*;
import javax.swing.JMenuItem;
import java.awt.event.MouseEvent;
import java.util.Random;
public class A2
This program tests the MenuFrame.
     public static void main(String[] args)
A2Frame frame = new A2Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
class A2Frame extends JFrame
public A2Frame()
final int DEFAULT_FRAME_WIDTH = 600;
final int DEFAULT_FRAME_HEIGHT = 600;
setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);
addWindowListener(new WindowCloser());
// add drawing panel to content pane
JPanel panel = new RectanglePanel();
Container contentPane = getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
//contentPane.add(controlPanel, BorderLayout.SOUTH);
// construct menu
JMenuBar menuBar1 = new JMenuBar();
setJMenuBar(menuBar1);
JMenu fileMenu = new JMenu("File");
menuBar1.add(fileMenu);
JMenu colourMenu = new JMenu("Colour");
menuBar1.add(colourMenu);
JMenu editMenu = new JMenu("Edit");
menuBar1.add(editMenu);
//MenuListener listener = new MenuListener();
JMenuItem newMenuItem = new JMenuItem("New");
fileMenu.add(newMenuItem);
newMenuItem.addActionListener(listener);
JMenuItem exitMenuItem = new JMenuItem("Exit");
fileMenu.add(exitMenuItem);
exitMenuItem.addActionListener(listener);
JMenuItem randomizeMenuItem = new JMenuItem("Randomize");
editMenu.add(randomizeMenuItem);
randomizeMenuItem.addActionListener(listener);
JMenuItem redMenuItem = new JMenuItem("Red");
colourMenu.add(redMenuItem);
redMenuItem.addActionListener(listener);
JMenuItem greenMenuItem = new JMenuItem("Green");
colourMenu.add(greenMenuItem);
greenMenuItem.addActionListener(listener);
JMenuItem blueMenuItem = new JMenuItem("Blue");
colourMenu.add(blueMenuItem);
blueMenuItem.addActionListener(listener);
class MenuListener implements ActionListener
public void actionPerformed(ActionEvent event)
// find the menu that was selected
Object source = event.getSource();
if (source == exitMenuItem)
System.exit(0);
else if (source == newMenuItem)
panel.reset();
else if (source == randomizeMenuItem)
panel.randomize();
else if (source == redMenuItem)
panel.setRed();
else if (source == greenMenuItem)
     panel.setGreen();
else if (source == blueMenuItem)
          panel.setBlue();
MenuListener listener = new MenuListener();
class ChoiceListener implements ActionListener
public void actionPerformed(ActionEvent event)
if (rectangleButton.isSelected())
g2.draw(new Rectangle2D.Double(0,0,5,5));
else if (ellipseButton.isSelected())
g2.draw(new Ellipse2D.Double (0,0,5,5));
rect.repaint();
ChoiceListener listener2 = new ChoiceListener();
ControlPanel();
pack();
private RectanglePanel panel;
private class WindowCloser extends WindowAdapter
public void windowClosing(WindowEvent event)
System.exit(0);
public void ControlPanel()
JPanel shapeGroupPanel = createRadioButtons();
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1, 1));
controlPanel.add(shapeGroupPanel);
contentPane.add(controlPanel, BorderLayout.SOUTH);
public JPanel createRadioButtons()
JRadioButton rectangleButton = new JRadioButton("Rectangle");
rectangleButton.addActionListener(listener2);
JRadioButton ellipseButton = new JRadioButton("Ellipse");
ellipseButton.addActionListener(listener2);
rectangleButton.setSelected(true);
// add radio buttons to button group
ButtonGroup group = new ButtonGroup();
group.add(rectangleButton);
group.add(ellipseButton);
JPanel panel = new JPanel();
panel.add(rectangleButton);
panel.add(ellipseButton);
panel.setBorder (new TitledBorder(new EtchedBorder(), "Shape"));
//return panel;
class RectanglePanel extends JPanel
public RectanglePanel()
rect = new Rectangle(0, 0, RECT_WIDTH, RECT_HEIGHT);
color = Color.white ;
class ShapeListener implements MouseListener
public void mousePressed(MouseEvent event)
int x = event.getX();
int y = event.getY();
rect.setLocation(x,y);
repaint();
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
ShapeListener listener1 = new ShapeListener();
public void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(color);
g2.fill(rect);
g2.draw(rect);
public void reset()
rect.setLocation(0, 0);
repaint();
public void randomize()
Random generator = new Random();
rect.setLocation(generator.nextInt(getWidth()),generator.nextInt(getHeight()));
repaint();
public void setRed()
color = Color.red;
repaint();
public void setGreen()
color = Color.green;
repaint();
public void setBlue()
color = Color.blue;
repaint();
private Rectangle rect;
private static final int RECT_WIDTH = 20;
private static final int RECT_HEIGHT = 30;
private Color color ;
private Random random;
public void setShape()
if (rectangleButton.isSelected())
g2.draw(new Rectangle2D.Double(0,0,5,5));
else if (ellipseButton.isSelected())
g2.draw(new Ellipse2D.Double (0,0,5,5));
rect.repaint();
private JRadioButton rectangleButton;
private JRadioButton ellipseButton;
private ActionListener listener;
private JMenuItem exitMenuItem;
private JMenuItem newMenuItem;
private JMenuItem redMenuItem;
private JMenuItem greenMenuItem;
private JMenuItem blueMenuItem;
private JMenuItem randomizeMenuItem;
}

grammer corrected.
I posted a program that lays out buttons based on mouse actions on JButton Adding Problem - reply 5. It uses MouseInputAdapter and Container.addMouseListener. It watches for mousePressed and mouseReleased events to get the limits for the button. The same could be used for ellipses.

Similar Messages

  • My classic is stuck on a blank screen and it won't connect to any device or reset when I hold the menu and center buttons. Any ideas what's up and how to fix it?

    I know click hold on and off then hold the menu and center button until the apple logo appears. I have tried this so many times and my iPod Classic is still unresponsive! I've tried plugging my iPod into my Mac, my wall charger, my ihome, everything, but nothing works. It won't connect to iTunes to restore or anything. I've tried leaving it unplugged long enough to let the battery die and then tried plugging it in, a method I've utilized successfully with other iPods but that didn't work either. I really just starting to lose faith in the quality of this product. The first classic I bought didn't work at all and I had to exchange it. This one I bought to replace my 5 year old nano and it hasn't even lasted 7 months! Somebody please offer me a solution and assure me that I didn't waste $250 on a crap piece of technology. I'm desperate!

    Although you say you have let the battery drain, was it for long enough? Since nothing else that you've tried so far has worked, I suggest that you leave the iPod unplugged for at least four days, preferably a week. That way, if something is stopping the iPod from turning the screen on, then whatever it is will drain the battrey. However, since the screen isn't on, that may take longer than if the screen was on.
    Then, after that time, plug the iPod into a power source and leave it alone for at least thirty minutes. Only after thirty minutes will it show any signs of life, but you should leave it until it is fully charged before trying to use it.
    If you get to this stage, the fact that the battery has drained will cause the iPod to reset when it springs back to life.
    Let us know how you get on.

  • How do i unfreeze my ipod when ive already tried the menu and select button and that didnt help?

    how do i unfreeze my ipod when ive already tried the menu and select button and that didnt help?

    How long did you press and hold both the Select (Center) and Menu buttons together?  Have you tried doing this procedure more than once?  Is the hold switch in the Off position?
    If nothing else, let the iPod's battery fully drain. Then charge it back up again.
    B-rock

  • IPod nano 3rd gen. Apple logo remains on screen and iTunes doesn't recognise it's plugged in, I can't even turn it off. I've tried to get to disc menu but when I hold the 'menu and select button' it reboots it doesn't recognise the select and play button

    iPod nano 3rd gen. Apple logo remains on screen and iTunes doesn't recognise it's plugged in, I can't even turn it off. I've tried to get to disc menu but when I hold the 'menu and select button' it reboots and then doesn't recognise the select and play button to show the tick. I'm helpless.

    Try:
    - iOS: Not responding or does not turn on
    - Also try DFU mode after try recovery mode
    How to put iPod touch / iPhone into DFU mode « Karthik's scribblings
    - If not successful and you can't fully turn the iOS device fully off, let the battery fully drain. After charging for an least an hour try the above again.
    - Try on another computer                 
    - If still not successful that usually indicates a hardware problem and an appointment at the Genius Bar of an Apple store is in order.
    Apple Retail Store - Genius Bar       

  • How to popup a menu when rigth button clicked?

    Hi, i have a mouse listener on a Jtree, it is working fine and now i want that when the user clicks in the right button of the mouse, a popUp menu appears with diferent options depending on the type of node....i only need to know how do make the menu to appear...
    thanks

    I have had this same problem. For me it always occurs on Mandrake (I've only tried 8.0 and 8.1), but I have never had it occur on other Linux distributions such as Red Hat or Debian.
    Quick caveat: Although Sun says that this is not a
    bug I have installed Java on 5 different computers
    with various versions of Mandrake Linux (7.1, 8.0, and
    8.1) In these installations a left click (the main
    button click) will return a mouse event with a button
    mask of BUTTON1_MASK + BUTTON3_MASK instead of simply
    BUTTON1_MASK. This makes your popup window appear on
    every mouse click instead of just right button click.
    The solution (until this bug is fixed) is to see if
    the int returned from the getModifiers() method on
    mouse event == BUTTON1_MASK + BUTTON3_MASK and if it
    does then DONT popup the window.
    Oddly enough, I've had other people test this and they
    tell me that the mouse events return the proper button
    mask but every time I've tested it I get this bug.
    Just a FYI. If you see that behavior then this is how
    to fix it.

  • Disk Mode? How long pressing the select and play buttons?

    Please, I`ve tried everything to turn it on my ipod since i drop it. It doesn`t appear in itunes or desktop, or my computer. It was with that exclamation mark. I was trying to turn to Disk mode, but it doesn`t go!! For how long should i hold the select and play buttons? I was holding for about 5 minutes until i gave up. Does it take that long?
    Thanks, Juliana

    How to put your iPod into disk mode:
    1) First, make sure that your iPod is mostly charged.
    2) Toggle the "hold" switch on/off a few times.
    3) Hold the "menu" and "select" (center) buttons down together for 7 seconds.
    4) As soon as the Apple logo appears on the screen, let off the menu and center buttons, and immediately click the "select" (center) and "play/pause" buttons until the "disk mode" screen appears.
    If that didn't work, your iPod might be broken.
    But before you presume that, try those four steps above several more times. Sometimes you need to do this step several times before you get the hang of it.
    I hope this helps!
    ~~Kylene

  • HT1320 My Ipod nano says its locked and screens on and it's staying frozen.  I have tried pressing the menu and select button simultaneously and still not reset.  What should I do?.

    My Ipod nano says its locked and screens on and it's staying frozen.  I have tried pressing the menu and select button simultaneously and still not reset.  What should I do?

    Right click on the iPod Nano icon.  "Eject" should pop up.  At least that is the way it works on Macs. 
    Just found this: 
    iPod: How to Safely Disconnect if iPod doesn't eject

  • I have an older iPod that no longer shows up in iTunes or appears to be charging.  When I press the Menu and Select buttons, the Apple logo appears and then an icon of a battery with an exclamation point.  Is it dead for good?

    I have an older iPod that no longer shows up in iTunes or appears to be charging.  When I press the Menu and Select buttons, the Apple logo appears and then an icon of a battery with an exclamation point.  Is it dead for good?

    If it is otherwise working, you can change the battery.  If you want to do it yourself, you can buy the part on eBay and other online sources.  Just make sure you know the type of iPod you have so that you get the right part.
    http://support.apple.com/kb/HT1353
    There are online guides, including the ones on this web site
    http://www.ifixit.com/Device/iPod
    (Note:  Most replacement batteries come with plastic/nylon tools seen in the guides.)
    You can also have a repair business do the work.
    If it is just the battery, if you do a Reset (using the Menu and Select buttons) while it is connect to your computer, I think it should still be recognized by iTunes.

  • How to combine the line items of 2 Sales orders into 1 delivry

    how to combine the line items of 2 Sales orders into 1 delivry
    and their process, pre-requisites and tcode

    The prerequisites are:
    1) In the customer master sales area data, shipping tab, there is a field called Order combination. u must tick that.
    2) for the two orders, the sold to party & ship to party must be same
    3) both orders must have created from same plant & shipping points
    4) the line items must have same loading grp.
    5) the both orders sheduline line date must be same.
    transaction code for the same is VL04.
    enter the required data and select the order nos to be processed.
    Do reward points if it is useful

  • My IPod was frozen so I held down the Menu and middle button and it went off. Now it's got a dark screen and won't do anything.

    My IPod classic was frozen so I followed instructions online and held donw the Menu and middle button and the screen went blank. I now have a dark screen and can't get the IPod to do anything.

    Hello Lola51,
    I would be concerned as well if my iPod was unresponsive.  I found a resource that I think would help in a situation like this.
    I recommend following the steps in this troubleshooting assistant:
    iPod classic Troubleshooting Assistant
    http://www.apple.com/support/ipod/five_rs/classic/
    Thank you for posting in the Apple Support Communities. 
    Best,
    Sheila M.

  • How to change the picture frame size in Lightroom 5 Book

    I would like to control the actual picture frame size when making a Book in Lightroom 5. Or else know the sizes of the different picture frames provided by choosing the different page layouts. That way I can either change the picture frame dimensions to accomodate each photo or change the image size to fit in the offered picture frame. I find that using the software as is winds up cropping the images in unwanted ways. Modifying the picture frame would be my preferred alternative. Appreciate any ideas

    Hi again Tony,
                I have been using Adobe Photoshop 7, Photoshop Elements, Perfect Photo Suite, Photo Studio, etc., changing Image size, placing a picture on a page and then, do a simple last minute size adjustment by using the arrows to stretch or shrink it in place. Things would be a lot simpler if I could do the same thing with the cells in Lightroom 5. Cell pad adjustments do not fill the bill.
    I think we’ve pretty much concluded this exchange. Thanks again for your effort.
        david
    ay [email protected]
    Sent: Wednesday, March 12, 2014 11:27 PM
    To: dgbrow
    Subject: How to change the picture frame size in Lightroom 5 Book
    Re: How to change the picture frame size in Lightroom 5 Book
    created by Tony Jay <http://forums.adobe.com/people/Tony+Jay>  in Photoshop Lightroom - View the full discussion <http://forums.adobe.com/message/6205206#6205206

  • HT3180 struggling to wake my apple tv wont respond even when holding the menu and down button any ideas ?

    struggling to wake my apple tv wont respond even when holding the menu and down button any ideas ?

    Did you try unplugging the unit from power, leaving it for ten minutes, then plugging in again?
    If you have & it didn't work, Damian Smith1 posted this possible solution (I like to give credit where credit is due).
    Connect your ATV to your computer and try restoring it to factory setting Use the link below to walk you through the process
    Apple TV (2nd generation): Restoring your Apple TV
    support.apple.com/kb/HT4367

  • How to hide the Print and Export button in analytics report or tasks pane

    Hi Experts,
    In BIEE 11g,
    How to hide the Print and Export button in analytics report or tasks pane ?
    For example:
    In console,I have created one userA which is belong to BIConsumer GROUP , when I make use of the highest user 'weblogic' to create one simple report, then the userA will login the analytivs to view this report (not dashboard), it will show the print and export as below.So customers do not want to give him the privilege for printing and exporting.
    In addtion, go to catalog->tasks(left corner), it will also show the print and export button. So how to hide or not access these button?
    Note: Maybe it can use the policy for consumer role for implementing this requirement, but I do not know how to modify the policy. Are you facing the problem? Thanks.

    Hi,
    1. Create seperate folder for Reports & Dashobard.
    2. For BI_Consumer (userA) set the catalog permission to view Dashboard Folder only and remove permission on the Report Folder (you can give traverse permission but don't give Open permision).
    3. By this user won't be able to open or run any reports in that folder and the only way he can see the reports is through Dashboard and on dashboard the export and print buttone can be removed very easily.
    Mark helpful if it helps.
    Regards,
    Kashi
    Edited by: K N Yadav on 24 May, 2013 1:51 AM

  • STMS: how to hide the Import All Requests button in the import queue view?

    Hi All,
    I'd like to know how to hide the "Import all requests" button in the import queue view.
    Thanks a lot for your answers.
    G.

    Hi,
    about hiding i have a doubt but u can inactivate by following procedure,
    As referred in a thread:
    On the domain controller, in STMS>overview>systems>(double click the one you want to change)>Select Transport Tool tab and click on change. Add Import_single_only value 1, import_single_strategy value 1 and no_import_all value 1. Save distribute and activate.
    or
    Refer to OSS NOTE 194000.
    Thanks & regards.

  • How to combine the same variables from 2 queries in one webi report

    HI ALL,
    I created two BW queries/universe and put them into one web intelligence report. Both queries have the same mandatory variable. I combined the relevant dimensions but can't find how to combine the two variables, which resulted in pop-up for 2 variables.
    Any idea?

    Hey Dick Zheng,
    Just to close it,
    Instead of working on existing reports.
    Create new report(s) on top of these changed Universes(2).
    1. New WebI report on top of Univ1 (With Prompt)
    2. New WebI report on top of Univ2 (Without Prompt)
    If both are working as expected. Do follow below:
    1. Open any Report (just created in above steps)
    2. Edit Query
    3. Add Query
    4. Select another Universe
    5. Drag few objects
    6. Run Queries
    Now see it is working as expected or not.??
    Still any issues post here...,,
    Gracias...!!

Maybe you are looking for