简体   繁体   中英

How do I change the drawString font colour in java applet

I have written a paint method for my java applet and this allows my drawString to change colour to whatever the user inputs into the three text fields (R,G,B). My default font colour is black however, I am trying to change it to green but can't because for the font to change colour I have to write g.setColor(mixColor). So does anyone know how I can make the font colour start off as green when I run my applet instead of black.

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





public class text2 extends Applet implements ActionListener
{

Font textFont;

TextField T1;
TextField T2;
TextField T3;
int redColor, greenColor, blueColor;
String as, ag, ab;
Button bttn;
Button bttn2;

public void init() {

    // This routine is called by the system to initialize
    // the applet.  It sets up the font and initial colors
    // the applet.  It adds a button to the applet for
    // changing the message color.

    setBackground(Color.lightGray);

    // The applet is filled with the background color before
    // the paint method is called.  The button and the message
    // in this applet will appear on a light gray background.

    redColor=0;
    greenColor=0;
    blueColor=0;


    textFont = new Font("Serif",Font.BOLD,24);


    T1 = new TextField("",12);
    T2 = new TextField("",12);
    T3 = new TextField("",12);




    add(T1);
    add(T2);
    add(T3);

    bttn = new Button("Change Colour");
    bttn2 = new Button("Reset");
    // Create a new button.  "ChangeColour" is the text
    // displayed on the button.

    bttn.addActionListener(this);
    bttn2.addActionListener(this);
    // Set up bttn to send an "action event" to this applet
    // when the user clicks the button

    add(bttn);
    add(bttn2);



    // Add the button to the applet, so that it
    // will appear on the screen.

}  


public void paint(Graphics g) {

    // This routine is called by the system whenever the content
    // of the applet needs to be drawn or redrawn.  It displays
    // my name and reg number in the proper colour and font.
    this.bttn2.setLocation(600, 600);
    Color mixColor = new Color(redColor, greenColor,blueColor);
    g.setColor(mixColor);
    g.setFont(textFont);       // Set the font.
    g.drawString("Welcome to my applet by: Joshua", 330, 300);

}  


public void actionPerformed(ActionEvent e) {

    // This routine is called by the system when the user clicks
    // on the button.  The response is to change the colorNum
    // which determines the color of the message, and to call
    // repaint() to see that the applet is redrawn with the
    // new color.


    if (e.getSource() == bttn2) {
        T1.setText(null);
        T2.setText(null);
        T3.setText(null);

    }

    if (e.getSource() == bttn)
    {

        as=T1.getText();
        ag=T2.getText();
        ab=T3.getText();
        as=as.trim();
        ag=ag.trim();
        ab=ab.trim();

        redColor= Integer.parseInt(as);
        greenColor= Integer.parseInt(ag);
        blueColor= Integer.parseInt(ab);

        repaint();  // Tell system that this applet needs to be redrawn
    }


}

}

You can do it using the setColor() method of 'java.awt' package

  import java.applet.Applet; 
  import java.awt.*;
  public class Applet1 extends Applet {
      /* In applets there is NO 'main() ' */
      @Override
      public void paint(Graphics g) {
          //create color objects using constructor
          Color red = new Color(255,0,0);
          Color blue = new Color(0,0,255);
          Color black = new Color(0,0,0);
          //create font objects using constructor
          Font myFontBold = new Font("Courier", Font.BOLD ,20);
          Font myFontPlain = new Font("Courier", Font.BOLD ,20);
          //set font , by passing the font object as
          // argument to       the setFont() method
          g.setFont(myFontBold);
          //set font color by passing the color object as 
          //argument to the setColor() method
          g.setColor(Color.blue);
          g.drawString("Name ",25,20); 
          g.setFont(myFontPlain);
          g.setColor(Color.black);
          g.drawString(": Ashutosh K Singh",325,20);
      }
  }

You can use the setForeground method. The synatax is setForeground(Color.green) . Instead of green you can take the color value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM