Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-22-2008, 04:27 AM
King8654's Avatar
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..
__________________
WWW.FRESHEXAMS.COM - FREE COLLEGE EXAM BANK
CASH giveaway first month - Live Within WEEK

Last edited by King8654 : 04-22-2008 at 05:22 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-24-2008, 10:40 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 221
Zosden is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 10:46 AM
Member
 
Join Date: Apr 2008
Posts: 2
vishnujava is on a distinguished road
Hi
Hi to every body,

i hav joined into the forum today...

i hope i will get good support from all of u
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-24-2008, 10:49 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 237
sanjeevtarar is on a distinguished road
Hello Vishnu,

Welcome to community..

READ THE FAQ first and then move your thread to introduction forum.
__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-24-2008, 10:52 AM
Member
 
Join Date: Apr 2008
Posts: 2
vishnujava is on a distinguished road
Hello Sanjeev

Thanks for your kind info.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] If a object equals another object, do they contain the same data? bobleny New To Java 1 04-17-2008 11:10 PM
A bit of color! tim Java 2D 8 02-12-2008 12:57 AM
How to change font/ font color etc in a graphic object using JCombobox? JavaInLove AWT / Swing 0 02-07-2008 12:28 PM
Creating object of Type Object class venkatv New To Java 3 07-17-2007 04:33 PM
Operator < cannot be applied to java.lang.Object, Object Albert Advanced Java 1 07-13-2007 04:19 PM


All times are GMT +3. The time now is 02:40 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org