如何在java applet中更改drawString字体颜色

时间:2015-11-01 13:48:39

标签: java fonts applet

我为我的java applet编写了一个paint方法,这允许我的drawString将颜色更改为用户输入到三个文本字段(R,G,B)中的任何颜色。我的默认字体颜色是黑色,但我试图将其更改为绿色但不能,因为要更改颜色的字体我必须写g.setColor(mixColor)。所以有人知道当我运行我的applet而不是黑色时,我可以使字体颜色从绿色开始。

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
    }


}

}

2 个答案:

答案 0 :(得分:0)

您可以使用“ java.awt”包的setColor()方法来实现

  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);
      }
  }

答案 1 :(得分:-2)

您可以使用setForeground方法。 synatax是setForeground(Color.green)。而不是绿色,你可以采取颜色值。