I have a Jframe using a menubar to determine the color and shape of a graphics2d object, at the moment the color stays red no matter what. I think the problem is in the itemListener in the inner class myShape, but I dont know what it is.
Heres the code: -
import java.awt.*;
-
import javax.swing.*;
-
import java.awt.event.*;
-
import java.awt.Graphics2D.*;
-
import java.awt.geom.*;
-
-
-
/** A menu class that gives the option to display a circle or a rectangle
-
* that is either Green, Yellow, or Blue, it makes a text field to display
-
* which is shape/color is present
-
* @author Thomas Capranica
-
*
-
*/
-
public class menu implements ActionListener, ItemListener {
-
JMenuBar menuBar;
-
JMenu menu, submenu;
-
JMenuItem menuItem;
-
JCheckBoxMenuItem Yellow, Blue;
-
JRadioButtonMenuItem Rect, Circ;
-
JMenu firstMenu, secondMenu;
-
JTextField field = new JTextField(30);
-
JTextField field2 = new JTextField(30);
-
String color = "Red";
-
String shape = "Square";
-
boolean yellow = false;
-
boolean blue = false;
-
-
public static void main(String[] args){
-
menu j = new menu();
-
}
-
public menu(){
-
JFrame frame = new JFrame("JMenus");
-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
-
field.setEditable(false);
-
field2.setEditable(false);
-
-
field.setText("The Color is " + color);
-
field2.setText("The Shape is a " + shape);
-
-
menuBar = new JMenuBar();
-
JPanel north = new JPanel();
-
north.setLayout(new GridLayout(1,2));
-
north.add(field);
-
north.add(field2);
-
frame.add(north,BorderLayout.NORTH);
-
-
ButtonGroup Sgroup = new ButtonGroup();
-
-
Yellow = new JCheckBoxMenuItem("Yellow");
-
Blue = new JCheckBoxMenuItem("Blue");
-
Yellow.addItemListener(this);
-
Blue.addItemListener(this);
-
Yellow.addItemListener(new myShape());
-
-
Rect = new JRadioButtonMenuItem("Rectangle");
-
Circ = new JRadioButtonMenuItem("Circle");
-
-
Sgroup.add(Circ);
-
Sgroup.add(Rect);
-
-
firstMenu = new JMenu("Colors");
-
firstMenu.getAccessibleContext().setAccessibleDescription("" +
-
"Sets the Colrors of the Object");
-
firstMenu.add(Yellow);
-
firstMenu.add(Blue);
-
firstMenu.addSeparator();
-
-
secondMenu = new JMenu("Shapes");
-
secondMenu.add(Rect);
-
secondMenu.add(Circ);
-
firstMenu.add(secondMenu);
-
menuBar.add(firstMenu);
-
-
frame.add(new myShape());
-
frame.setJMenuBar(menuBar);
-
frame.setSize(300,200);
-
frame.setVisible(true);
-
}
-
//Checks to see which radioButton is selected, sets the shape accordingly (not done)
-
public void actionPerformed(ActionEvent e){
-
field2.setText("Its A " + shape);
-
}
-
-
// Checks to see which checkBoxes are checked, changes the color accordingly
-
public void itemStateChanged(ItemEvent e){
-
-
JMenuItem source = (JMenuItem)(e.getSource());
-
String select = ((e.getStateChange() == ItemEvent.SELECTED) ?
-
"selected":"unselected");
-
String which = source.getText();
-
if(which.equals("Yellow") && select.equals("selected"))
-
yellow = true;
-
else if(which.equals("Yellow") && select.equals("unselected"))
-
yellow = false;
-
else if(which.equals("Blue") && select.equals("selected"))
-
blue = true;
-
else if(which.equals("Blue") && select.equals("unselected"))
-
blue = false;
-
-
if(blue == true && yellow == false)
-
color = "Blue";
-
else if(yellow == true && blue == false)
-
color = "Yellow";
-
else if(yellow == true && blue == true)
-
color = "Green";
-
else if(yellow == false && blue == false)
-
color = "Red";
-
-
field.setText("The Color is " + color);
-
}
-
class myShape extends JPanel implements ItemListener {
-
Graphics2D g2;
-
Color c;
-
public void paintComponent(Graphics g){
-
super.paintComponent(g);
-
g2 = (Graphics2D) g;
-
g2.setColor(c);
-
g2.fill(new Rectangle(125,50,50,50));
-
}
-
public void itemStateChanged(ItemEvent e){
-
JMenuItem source = (JMenuItem)(e.getSource());
-
String select = ((e.getStateChange() == ItemEvent.SELECTED) ?
-
"selected":"unselected");
-
String which = source.getText();
-
if(which.equals("Yellow") && select.equals("selected"))
-
yellow = true;
-
else if(which.equals("Yellow") && select.equals("unselected"))
-
yellow = false;
-
else if(which.equals("Blue") && select.equals("selected"))
-
blue = true;
-
else if(which.equals("Blue") && select.equals("unselected"))
-
blue = false;
-
-
if(blue == true && yellow == false)
-
c = Color.BLUE;
-
else if(yellow == true && blue == false)
-
c = Color.YELLOW;
-
else if(yellow == true && blue == true)
-
c = Color.GREEN;
-
else if(yellow == false && blue == false)
-
c = Color.RED;
-
repaint();
-
}
-
}
-
}
2 7475 BigDaddyLH 1,216
Recognized Expert Top Contributor
Please enclose your posted code in [code] tags (See How to Ask a Question).
This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.
Please use [code] tags in future.
MODERATOR
BigDaddyLH 1,216
Recognized Expert Top Contributor
Very close! You wrote: - frame.add(new myShape());
which meant you never had a change to teach your component to listen to the menu items. Replace that with: -
myShape component = new myShape();
-
Yellow.addItemListener(component);
-
Blue.addItemListener(component);
-
frame.add(component);
-
My the way, there is a strong convention in Java to give classes (and interfaces) names that start with capital letters: Menu and MyShape, and all other names start with lower case letters.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Robert W. |
last post by:
In my Winforms app I'm trying to get an image's background to appear
transparent on a form that has a gradient background. So I added a
PictureBox and then attempted to add a custom paint command for the
PictureBox. But it's not working. Here's the code I've written:
public void InitializeLinearGradients()
{
this.Paint += new PaintEventHandler(PaintClient);
this.SizeChanged += new EventHandler(SizeClient);
pictureLogo.Paint += new...
|
by: K.N.Ranjit |
last post by:
Hi,
This is the 4th mail I'm sending regarding this problem.
I'm really not been able to sort this problem out.I'm not
been able to change my starting position of my cordinates
from top-left to bottom-left of my form.Thrice I got the
reply but unfortunately it did'nt work.Here is my coding.
private sub form_load(...)
dim y as integer
y=me.height
location.y = 0-y
|
by: Roger |
last post by:
Is this an easy thing to change?
I want to put an option button on a form and want to dynamically change the
color (Red, Yellow, Green) for
showing a status. I figured it would be an easy task, but am finding it a
little difficult as there isn't a simple property to set.
Thanks,
Rog
|
by: Thief_ |
last post by:
I'm trying to create many squares on a form and for every new square, change
the background colour to the next colour. I'm trying to create a colour
palette showing all available colours and their colour name.
My problem is that I can not figger out how to create a new panel at
specific intervals across and down the form. Here's the code I have so far:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles...
|
by: Brad Rogers |
last post by:
All,
Being immersed in vb.net and trying CSharp after almost a year I forgot the
differences. I like vb fixing the uppercase/lowercase names and seeming to
be more flexible to code entry.
But while trying to insert a text box to see when a method is used, and
putting a counter to bump some variable?
The textbox sits there unchanged. I put a breakpoint at the text write
| |
by: kCura |
last post by:
First off, I'm sorry if this isn't the write thread to post this message but
I couldn't find one dedicated to System.Drawing.
I'm developing an intranet application in VB.NET that uses a Hosted Windows
Form (HWF) dedicated to image manipulation. This HWF is also supposed to
allow the user to print an image to their default printer. I'm using a
System.Drawing.Printing.PrintDocument object to initiate printing, and
everything works locally...
|
by: Kristian Frost |
last post by:
Hi,
I'm just getting started with VB.Net, and I'm having trouble getting the
routing around of some of the data straight in my mind, which has led me
to the following problem.
Basically, I'm trying to create an object that will monitor the status of
some switches.
I created a User Control to put into a form, on which I want to have five
graphical representations of LEDs, which I've got working, light up and
turn off when the...
|
by: Joe |
last post by:
does anyone know why this source code throws an error??
Dim myBitmap As System.Drawing.Bitmap
Dim myGraphics As Graphics
myBitmap = New System.Drawing.Bitmap(fileName:="C:\test.tif")
myGraphics = PictureBox1.CreateGraphics
Dim expansionRectangle As New Rectangle(25, 550, 1000, 350)
Dim destRectangle1 As New Rectangle(0, 0, 1000, 350)
myGraphics.DrawImage(myBitmap, destRectangle1, expansionRectangle,
|
by: CSharper |
last post by:
Is it possible to selectivly change the color of an item in text. I
saw ForeColor option, but it changes the color of all the items. If it
is not possible, is there any other control list listbox where we can
see more than one item and change the color during run time.
Thanks.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |