JColor program

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 3809 Views - Last Post: 16 August 2010 - 05:06 PM Rate Topic: -----

#1 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

JColor program

Posted 15 August 2010 - 11:55 PM

Write an applet that displays a single JButton and any background color you choose. The first time the user clicks the JButton, your applet should display your name in a color different from the background. The second time the user clicks the JButton, make the first name seem to disappear by redrawing the String using the background color. At the same time, you should draw the name again, this time using a larger font and a different color. Save this file with the name, JColor.java.

I appreciate for your help.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JColor extends JApplet implements ActionListener
{
	int width, height;
	JButton enlargeButton = new JButton("Enlarge");
	Font bigFont = new Font("Helvetica", Font.BOLD, 24);
	String hello = "Hello";
	Container con = getContentPane();

	public void init()
	{
		con.setLayout(new FlowLayout());
		con.add(enlargeButton);
		con.setBackground(Color.BLUE);
		enlargeButton.addActionListener(this);
		width = hello.getTextWidth();
		height = hello.getTextHeight();
	}
	
	public void actionPerformed(ActionEvent e)
	{
		

		width = width * 2;
		height = height * 2;
		repaint();
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		g.drawString(hello.getText(), 0, 0, width, height, this);
	}
}




Errors:

JColor.java:19: cannot find symbol
symbol: method getTextWidth()
location: class java.lang.String
width = hello.getTextWidth();
JColor.java:19: cannot find symbol
symbol: method getTextHeight()
location: class java.lang.String
width = hello.getTextHeight();
JColor.java:19: cannot find symbol
symbol: method getText()
location: class java.lang.String
width = hello.getText(), 0, 0 width, height, this);

Is This A Good Question/Topic? 0
  • +

Replies To: JColor program

#2 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: JColor program

Posted 16 August 2010 - 01:11 AM

Well, for starters, there are no getTextWidth() or getTextHeight() method in String class, that's why you get those cannot find symbol errors.
Was This Post Helpful? 0
  • +
  • -

#3 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: JColor program

Posted 16 August 2010 - 06:07 AM

You may want to check out the FontMetrics Class. That'll help you in finding your width and height.
Was This Post Helpful? 0
  • +
  • -

#4 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: JColor program

Posted 16 August 2010 - 06:32 AM

View PosteZACKe, on 16 August 2010 - 05:07 AM, said:

You may want to check out the FontMetrics Class. That'll help you in finding your width and height.


Hi, I have tried what you suggested and still getting the same error.
I tried using the getWidths(), getHeight(), charWidth(), charHeight(), stringWidth() and stringHeight()
neither methods work. Just the same error cannot find symbols.

This post has been edited by bravo659: 16 August 2010 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: JColor program

Posted 16 August 2010 - 06:35 AM

Can you show the code where you tried this? It'll need to be in a format similar to this:
FontMetrics fm = someComponent.getFontMetrics(someFont);
int width = fm.stringWidth(someString);



Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: JColor program

Posted 16 August 2010 - 06:36 AM

Post your revised code, as well as the error messages.
Was This Post Helpful? 0
  • +
  • -

#7 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: JColor program

Posted 16 August 2010 - 06:51 AM

View PosteZACKe, on 16 August 2010 - 05:35 AM, said:

Can you show the code where you tried this? It'll need to be in a format similar to this:
FontMetrics fm = someComponent.getFontMetrics(someFont);
int width = fm.stringWidth(someString);



Hope this helps!


Hi, this is my revised code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JColor extends JApplet implements ActionListener
{
	//declare the variables
	int width, height;
	JButton enlargeButton = new JButton("Enlarge");
	Font bigFont = new Font("Helvetica", Font.BOLD, 24);
	String hello = "Hello";
	Container con = getContentPane();

	//initialize components
	public void init()
	{
		con.setLayout(new FlowLayout());
		con.add(enlargeButton);
		con.setBackground(Color.BLUE);
		enlargeButton.addActionListener(this);
		width = hello.stringWidth();
		height = hello.stringHeight();
	}
	//peform the action trigger event
	public void actionPerformed(ActionEvent e)
	{
		

		width = width * 2;
		height = height * 2;
		repaint();
	}
	//paint and draw object
	public void paint(Graphics g)
	{
		super.paint(g);
		g.drawString(hello.getText(), 0, 0, width, height, this);
	}
}




JColor.java:19: cannot find symbol
symbol: method stringWidth()
location: class java.lang.String
width = hello.stringWidth();
JColor.java:19: cannot find symbol
symbol: method stringHeight()
location: class java.lang.String
width = hello.stringHeight();
JColor.java:19: cannot find symbol
symbol: method getText()
location: class java.lang.String
width = hello.getText(), 0, 0 width, height, this);
Was This Post Helpful? 0
  • +
  • -

#8 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: JColor program

Posted 16 August 2010 - 06:55 AM

See my post before macosxnerd101, you need to create a FontMetrics Object in order to use FontMetrics methods.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: JColor program

Posted 16 August 2010 - 06:58 AM

As Cuzzie said before, those methods don't exist within the String class. Also, the height method in FontMetrics is getHeight(), and it doesn't have a getText() method.
Was This Post Helpful? 0
  • +
  • -

#10 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: JColor program

Posted 16 August 2010 - 07:13 AM

View Postmacosxnerd101, on 16 August 2010 - 05:58 AM, said:

As Cuzzie said before, those methods don't exist within the String class. Also, the height method in FontMetrics is getHeight(), and it doesn't have a getText() method.


So what do I use to get the text to appear and disappear I know the .setVisible(true) will make the text disappear. Ah, getText() is only used when retrieving text from JTextField?
Ok so what it is I can use to make this program work then? I have my code half way done, so far.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: JColor program

Posted 16 August 2010 - 07:18 AM

Most JComponents that allow for text (JButton, JLabel, JTextField, JTextArea, etc.) have getText() and setText() methods. Rather than handling painting, or even messing with the ContentPane (let the Container handle that), I would use a JPanel and the setBackground() method, which accepts a Color param. For the text, I would use a JLabel and update the text using the getText() method. You can also change the font using the JLabel setFont() method. No painting involved, and very easy to remove the text (using setText("") for JLabel).
Was This Post Helpful? 0
  • +
  • -

#12 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: JColor program

Posted 16 August 2010 - 08:03 AM

View Postmacosxnerd101, on 16 August 2010 - 06:18 AM, said:

Most JComponents that allow for text (JButton, JLabel, JTextField, JTextArea, etc.) have getText() and setText() methods. Rather than handling painting, or even messing with the ContentPane (let the Container handle that), I would use a JPanel and the setBackground() method, which accepts a Color param. For the text, I would use a JLabel and update the text using the getText() method. You can also change the font using the JLabel setFont() method. No painting involved, and very easy to remove the text (using setText("") for JLabel).


Hi,
How can I get the program to work?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JColor extends JApplet implements ActionListener
{
	//declare the variables
	int width, height;
	JButton enlargeButton = new JButton("Enlarge");
	JLabel label = new JLabel("Hello");
	Font bigFont = new Font("Helvetica", Font.BOLD, 24);
	Container con = getContentPane();

	//initialize components
	public void init()
	{
		con.setLayout(new FlowLayout());
		con.add(enlargeButton);
		con.setBackground(Color.BLUE);
		enlargeButton.addActionListener(this);
		width = label.getWidth();
		height = label.getHeight();
	}
	//peform the action trigger event
	public void actionPerformed(ActionEvent e)
	{
		

		width = width * 2;
		height = height * 2;
		repaint();
	}
	//paint and draw object
	public void paint(Graphics g)
	{
		super.paint(g);
		g.drawString(label.getText(), 0, 0, width, height, this);
	}
}


1 Error oh the g.drawString(label.getText(), 0, 0, width, height, this);
cannot find the symbol error.

This post has been edited by bravo659: 16 August 2010 - 08:09 AM

Was This Post Helpful? -3
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: JColor program

Posted 16 August 2010 - 08:06 AM

Did you not read my post?

Edit: What is wrong with your code? And why don't you just get/set the text of the JLabel rather than messing with paint()?
Was This Post Helpful? 1
  • +
  • -

#14 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: JColor program

Posted 16 August 2010 - 08:13 AM

View Postmacosxnerd101, on 16 August 2010 - 07:06 AM, said:

Did you not read my post?

Edit: What is wrong with your code? And why don't you just get/set the text of the JLabel rather than messing with paint()?


This is what I suppose to use for Chapter 10 Graphics.
Haven't gotten to use the JPanel yet. Is not in the assignment.

This post has been edited by bravo659: 16 August 2010 - 08:13 AM

Was This Post Helpful? -2
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: JColor program

Posted 16 August 2010 - 08:14 AM

JPanel or not, you can still use JLabel get/set text rather than messing with paint().
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3