View Single Post
  #1 (permalink)  
Old 04-22-2008, 04:27 AM
King8654's Avatar
King8654 King8654 is offline
Member
 
Join Date: Apr 2008
Posts: 9
King8654 is on a distinguished road
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
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
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

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
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..
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- FREE COLLEGE EXAM BANK
CASH giveaway first month - Live Within WEEK
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by King8654 : 04-22-2008 at 05:22 AM.
Reply With Quote
Sponsored Links