Trying to change the color of a graphics object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mjrtom19
    New Member
    • Feb 2008
    • 1

    Trying to change the color of a graphics object

    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:
    [CODE=Java]
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event. *;
    import java.awt.Graphi cs2D.*;
    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;
    JCheckBoxMenuIt em Yellow, Blue;
    JRadioButtonMen uItem 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.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);

    field.setEditab le(false);
    field2.setEdita ble(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(field 2);
    frame.add(north ,BorderLayout.N ORTH);

    ButtonGroup Sgroup = new ButtonGroup();

    Yellow = new JCheckBoxMenuIt em("Yellow");
    Blue = new JCheckBoxMenuIt em("Blue");
    Yellow.addItemL istener(this);
    Blue.addItemLis tener(this);
    Yellow.addItemL istener(new myShape());

    Rect = new JRadioButtonMen uItem("Rectangl e");
    Circ = new JRadioButtonMen uItem("Circle") ;

    Sgroup.add(Circ );
    Sgroup.add(Rect );

    firstMenu = new JMenu("Colors") ;
    firstMenu.getAc cessibleContext ().setAccessibl eDescription("" +
    "Sets the Colrors of the Object");
    firstMenu.add(Y ellow);
    firstMenu.add(B lue);
    firstMenu.addSe parator();

    secondMenu = new JMenu("Shapes") ;
    secondMenu.add( Rect);
    secondMenu.add( Circ);
    firstMenu.add(s econdMenu);
    menuBar.add(fir stMenu);

    frame.add(new myShape());
    frame.setJMenuB ar(menuBar);
    frame.setSize(3 00,200);
    frame.setVisibl e(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 itemStateChange d(ItemEvent e){

    JMenuItem source = (JMenuItem)(e.g etSource());
    String select = ((e.getStateCha nge() == ItemEvent.SELEC TED) ?
    "selected":"uns elected");
    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.paintComp onent(g);
    g2 = (Graphics2D) g;
    g2.setColor(c);
    g2.fill(new Rectangle(125,5 0,50,50));
    }
    public void itemStateChange d(ItemEvent e){
    JMenuItem source = (JMenuItem)(e.g etSource());
    String select = ((e.getStateCha nge() == ItemEvent.SELEC TED) ?
    "selected":"uns elected");
    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();
    }
    }
    }[/CODE]
    Last edited by BigDaddyLH; Feb 27 '08, 04:23 AM. Reason: added code tags
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    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

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Very close! You wrote:
      [CODE=Java]frame.add(new myShape());[/CODE]
      which meant you never had a change to teach your component to listen to the menu items. Replace that with:
      [CODE=Java]
      myShape component = new myShape();
      Yellow.addItemL istener(compone nt);
      Blue.addItemLis tener(component );
      frame.add(compo nent);
      [/CODE]
      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.

      Comment

      Working...