• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with drawString that needs to change colors  RSS feed

 
Jill Stemm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I have to do,.
Develop a Java GUI application that displays a single JButton on a background with a color different than the default color. Code the JButton so that it rotates through the following functionality:

•The first click uses Java Graphics to display your name in a color different than the window’s background color
•The next click will replace the existing text with your name in a larger font
•The next click will remove the text from the window

Successive clicks will continue to rotate through the list above. Therefore a fourth click would display your name in a color different than the window’s background color, and so on.

NOTE: This application must use the drawRect and Drawstring methods from the Graphics class and the JPanel's paint method. Use of a JLabel for text output will result in a loss of credit.

HINT: To remove the existing text, simply have the paint method call drawRect to draw over top of it.

here is my code so far and I know it is bad I have my confused now so sorry in advance and after 13 hours of messing with it I would be forever in your debt if you help me figure it out.

package colorChange;

import javax.swing.*;

import org.w3c.dom.css.Rect;

import java.awt.*;
import java.awt.event.*;



public class ButtonColor extends JPanel implements ActionListener
{

/**
*
*/
private static final long serialVersionUID = 1L;
Font bigFont = new Font("Serif", Font.BOLD,22);
String name = "Jill Stemm";
public static Color color = (Color.GREEN);
public void paintComponent(Graphics g2)
{
super.paintComponent(g2);

Font font = new Font("Serif", Font.BOLD, 22);
g2.setFont(font);
g2.drawString("Jill Stemm", 95, 85);
g2.setColor(color);
g2.fillRect(70,100 ,150,75 );




}









public static void main(String[]args){



JFrame main = new JFrame("Click to Change color of font");
JButton click = new JButton("Click Me");
click.addActionListener(
new ActionListener(){


@Override
public void actionPerformed(ActionEvent event) {

color = JColorChooser.showDialog(null, "Select a color", color);




}
}



);







//JFrame
main.setLayout(new BorderLayout());
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setSize(300,300);
main.setVisible(true);
main.add(click,BorderLayout.SOUTH);

ButtonColor one = new ButtonColor();
ButtonColor two = new ButtonColor();
ButtonColor three = new ButtonColor();
ButtonColor four = new ButtonColor();


//JPanel one setup
main.add(one);
one.setSize(10,10);





}







private static void ButtonColor() {
// TODO Auto-generated method stub

}









@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}







}
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jill,
Welcome to the Java Ranch.
Please use code tags to post your code because that renders it to be readable, i have done it for you now.
 
Jill Stemm
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:Hello Jill,
Welcome to the Java Ranch.
Please use code tags to post your code because that renders it to be readable, i have done it for you now.



Thanks Sorry about that,
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh please, dont say 'sorry' about it. Its a natural phenomenon. I made the same mistake when i first came here.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!