Results 1 to 5 of 5
  1. #1
    King8654's Avatar
    King8654 is offline Member
    Join Date
    Apr 2008
    Posts
    9
    Rep Power
    0

    Default JRadioButton set object color

    [SOLVED]

    writing program which draws object(car) when mousepressed on the drawing panel. The user selects a radiobutton up top which specifies the color, then clicks and the cars are that color. I have the car and drawingpanel all set, just cant figure out how to have the jradiobutton in ButtonPanel setColor for objects on Drawingpanel, which then is read by the paintcomponent method.

    mainapp
    Java Code:
    import javax.swing.*;
    import java.util.ArrayList;
    import java.awt.geom.Ellipse2D;
    import java.util.Iterator;
    
    
    public class MyApp extends JFrame {
    
    private JPanel drawingPanel;
    private JPanel buttonPanel;
    
    public MyApp(String title)
    {
    super(title);
    this.setSize(600,450);
    
    DrawingPanel drawingPanel = new DrawingPanel();
    ButtonPanel buttonPanel = new ButtonPanel(drawingPanel);
    this.add(drawingPanel, java.awt.BorderLayout.CENTER);
    this.add(buttonPanel, java.awt.BorderLayout.NORTH);
    
    this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    
    
    }
    
    public static void main(String [] args) {
    MyApp app = new MyApp("Cars Galore!");
    }
    }
    drawingpanel
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.ArrayList;
    import java.awt.geom.Rectangle2D;
    import java.util.Iterator;
    
    public class DrawingPanel extends JPanel implements MouseListener {
    
    private Car _auto;
    ArrayList <Car> a;
    Iterator <Car> iterator;
    
    int i;
    
    private Color color;
    
    public DrawingPanel() {
        super();
        this.setBackground(java.awt.Color.WHITE);
        _auto = new Car(this);
        _auto.setLocation(275, 200);
        setBackground(java.awt.Color.WHITE);
        a = new ArrayList <Car>();
        iterator = a.iterator();
        
            
        int i=a.size();
        this.addMouseListener(this);
    
    }
        
         public void mousePressed(MouseEvent e) {
           int x = e.getX() - 25;
           int y = e.getY() - WIDTH/2;
           
           _auto.setLocation(x,y);
           repaint();
          
        }
          
          public void mouseReleased(MouseEvent evt) { }
          public void mouseDragged(MouseEvent evt) { } 
          public void mouseEntered(MouseEvent evt) { }
          public void mouseExited(MouseEvent evt) { }
          public void mouseMoved(MouseEvent evt) { }
          public void mouseClicked(MouseEvent evt) { }
    
          
          public void paintComponent(Graphics aBrush){
              Graphics2D aBetterBrush = (Graphics2D) aBrush;
              _auto.setColor(color);
              _auto.draw(aBetterBrush);
              _auto.fill(aBetterBrush);
                        
                       
                     }
        }
    buttonpanel

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.ArrayList;
    import java.awt.geom.Rectangle2D;
    import java.util.Iterator;
    
    public class ButtonPanel extends JPanel  {
    
    private JRadioButton _button1;
    private JRadioButton _button2;
    private JRadioButton _button3;
    private JRadioButton _button4;
    
    private Car _auto;
    private DrawingPanel _car;
    
                    
            public ButtonPanel(DrawingPanel aPanel) {
                super(new GridLayout(1,0));
             
            ButtonGroup group = new ButtonGroup();
                
            _button1 = new JRadioButton("Red", true);
            _button1.addActionListener (new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    _car.setColor(Color.RED); }});
            group.add(_button1);
            add(_button1);
            _button2 = new JRadioButton("Green", false);
            _button2.addActionListener (new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    _auto.setColor(Color.RED); }});
            group.add(_button2);
            add(_button2);
            _button3 = new JRadioButton("Blue", false);
            _button3.addActionListener (new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    _auto.setColor(Color.BLUE); }});
            group.add(_button3);
            add(_button3);
            _button4 = new JRadioButton("Yellow", false);
            _button4.addActionListener (new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    _auto.setColor(Color.YELLOW); }});
            group.add(_button4);
            add(_button4);
                
        }
           
    }
    car
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Car
    {
        // instance variables 
        private final int HEIGHT = 25;
        private final int WIDTH = 50;
        SmartEllipse _wheelleft, _wheelright;
        SmartRectangle _maincar, _cartop;
        private JPanel _panel;
        int x,y,width;
        
        public Car(JPanel aPanel) {
            super();
        
             _maincar = new SmartRectangle(Color.RED);
             _cartop = new SmartRectangle(Color.RED);
             _wheelleft = new SmartEllipse(Color.BLACK);
             _wheelright = new SmartEllipse(Color.BLACK);
             _panel = aPanel;
             this.setSize(WIDTH,HEIGHT);
             
                      
             }
        public void setLocation(int x, int y) {
    	_maincar.setLocation(x, y); 
    	_cartop.setLocation(x+12.5, y-15); 
    	_wheelleft.setLocation(x+8, y + 20); 
    	_wheelright.setLocation(x+28, y + 20);
        }
             public int getX() {
    	return (int)_maincar.getX();
        }
            public int getY() {
    	return (int)_maincar.getY();     
        
        }
            
            public double getMaxX() {
    	return _maincar.getMaxX();
        }
            public double getMaxY() {
    	return _maincar.getMaxY();
        }
        
            public void setSize (int width, int height) {
    	_maincar.setSize(50, 25);
    	_cartop.setSize(width/2, height-10);
       	_wheelleft.setSize(width-35, height-10);
       	_wheelright.setSize(width-35, height-10);
        }
        
        public double getWidth () {
    	return _maincar.getWidth();
        }
        public double getHeight() {
    	return _maincar.getHeight();
        }
        
        public int getMinBoundX() {
    	return (int) _panel.getX();
        }
        public int getMaxBoundX() {
    	return (int) (_panel.getX() + _panel.getWidth()
    		      - this.getWidth());
        }
        public int getMinBoundY() {
    	return (int) _panel.getY();
        }
        public int getMaxBoundY() {
    	return (int) (_panel.getY() + _panel.getHeight()
    		      - this.getHeight());
        }
        
        
        public void draw(Graphics2D aBetterBrush)
            {
                       
            _maincar.draw(aBetterBrush);
            _cartop.draw(aBetterBrush);
            _wheelleft.draw(aBetterBrush);
            _wheelright.draw(aBetterBrush);
           
                 
        } 
    
        public void fill(Graphics2D aBetterBrush) {
            _maincar.fill(aBetterBrush);
            _cartop.fill(aBetterBrush);
            _wheelleft.fill(aBetterBrush);
            _wheelright.fill(aBetterBrush);
            
        }
        public void setColor(Color aColor) {
            _maincar.setFillColor(aColor);
            _cartop.setFillColor(aColor);
                            
        }
    
    
    }
    put programming aside for week or two, and 4:20 yest didnt help. I know its simple, the car draws fine on mousepressed, and the draw/fill methods working fine, just cant figure out the setColor at this time. .thanks..
    Last edited by King8654; 04-22-2008 at 04:22 AM.

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    try making an array of ints that hold the color values and then tell the paintComponent g.setColor(myArrayOfColors[i] or something along those lines.

    myArray[0] = Color.GREEN; .... an so on

  3. #3
    vishnujava is offline Member
    Join Date
    Apr 2008
    Posts
    14
    Rep Power
    0

    Default Hi

    Hi to every body,

    i hav joined into the forum today...

    i hope i will get good support from all of u

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hello Vishnu,

    Welcome to community..

    READ THE FAQ first and then move your thread to introduction forum.
    sanjeev,संजीव

  5. #5
    vishnujava is offline Member
    Join Date
    Apr 2008
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  3. Replies: 1
    Last Post: 04-17-2008, 10:10 PM
  4. A bit of color!
    By tim in forum Java 2D
    Replies: 8
    Last Post: 02-11-2008, 11:57 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •