Results 1 to 6 of 6
- 02-07-2008, 11:28 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 13
- Rep Power
- 0
How to change font/ font color etc in a graphic object using JCombobox?
Hello
My program im writing recently is a small tiny application which can change fonts, font sizes, font colors and background color of the graphics object containing some strings. Im planning to use Jcomboboxes for all those 4 ideas in implementing those functions. Somehow it doesnt work! Any help would be grateful.
So currently what ive done so far is that: Im using two classes to implement the whole program. One class is the main class which contains the GUI with its components (Jcomboboxes etc..) and the other class is a class extending JPanel which does all the drawing. Therefore it contains a graphics object in that class which draws the string. However what i want it to do is using jcombobox which should contain alit of all fonts available/ font sizes/ colors etc. When i scroll through the lists and click the one item i want - the graphics object properties (font sizes/ color etc) should change as a result.
What ive gt so far is implemented the jcomboboxes in place. Problem is i cant get the font to change once selecting an item form it.
Another problem is that to set the color of font - i need to use it with a graphics object in the paintcomponent method. In this case i dnt want to create several diff paint.. method with different property settings (font/ size/ color)
Below is my code; perhaps you'll understand more looking at code.
1) providing the GUI is correctly implemented with componentsJava Code:public class main... Color[] Colors = {Color.BLUE, Color.RED, Color.GREEN}; ColorList = new JComboBox(Colors); ColorList.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { JComboBox cb = (JComboBox)ev.getSource(); Color colorType = (Color)cb.getSelectedItem(); drawingBoard.setBackground(colorType); } });;
2) Combobox stores the colors in an array
3) ActionListener should do following job: (but cant get it right - that is where my problem is)
- once selected the item (color/ font size etc... as i would have similar methods for each) i want, it should pass the item into the drawingboard class (JPanel) and then this class should do the job.
My problems have been highlighted in the comments of code above.Java Code:public class DrawingBoard extends JPanel { private String message; public DrawingBoard() { setBackground(Color.white); Font font = new Font("Serif", Font.PLAIN, fontSize); setFont(font); message = ""; } public void setMessage(String m) { message = m; repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); //setBackground(Color.RED); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint g2.drawString(message, 50, 50); } public void settingFont(String font) { //not sure how to implement this? //Jcombobox should pass an item to this //it should match against all known fonts in system then set that font to the graphics } private void settingFontSize(Graphics g, int f) { //same probelm with above.. } public void setBackgroundColor(Color c) { setBackground(c); repaint(); // still not sure if this done corretly. } public void setFontColor(Color c) { //not sure how to do this part aswell. //i know a method " g.setColor(c)" exist but i need to use a graphics object - and to do that i need to pass it in (then it will cause some confusion in the main class (previous code) }
Any help will be much appreciated thanks!!!Last edited by JavaInLove; 02-07-2008 at 11:32 AM.
- 03-25-2009, 01:31 AM #2
Member
- Join Date
- Mar 2009
- Location
- Portugal
- Posts
- 1
- Rep Power
- 0
Hello From Portugal
Hello, I'm new here :D
Thanks for all poster who have posted useful information here
sletlyenlaree from Elvas, Portugal
- 04-12-2009, 05:21 PM #3
I'm New Here
Hello,
I love this place and willing to learn a lot from the members here.
- 04-14-2009, 12:32 PM #4
First time, great to be here!
Hope everyone is doing excellent today
Excited to be here!
- 04-25-2009, 07:57 PM #5
I'm New Here
Hello,
What a forum, this forum surely will get many quality members for not too long :p
-
Similar Threads
-
how to set font size for a xml value
By bala_kj in forum Advanced JavaReplies: 3Last Post: 04-01-2008, 11:00 AM -
Swing - Changing component default font
By Java Tip in forum Java TipReplies: 0Last Post: 03-11-2008, 10:52 PM -
JTable with Font
By Rama Koti Reddy in forum AWT / SwingReplies: 1Last Post: 12-12-2007, 05:22 PM -
Help with save font information in jtextarea
By valery in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:22 PM -
Font question
By orchid in forum Java 2DReplies: 1Last Post: 08-01-2007, 02:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks