Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Help with drawString that needs to change colors

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

}







}
 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Goodbye moon men. Hello tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic