如何在java小程序中更改抽绳字体颜色
我已经为java小程序编写了一个绘画方法,这允许我的绘图字符串根据用户输入到三个文本字段(R、G、B)中的内容改变颜色。我的默认字体颜色是黑色,但我正在尝试将其更改为绿色,但无法更改,因为要更改字体颜色,我必须编写g.setColor(mixColor)。那么,有人知道当我运行applet时,如何使字体颜色从绿色开始,而不是黑色如何在java小程序中更改抽绳字体颜色,java,fonts,applet,Java,Fonts,Applet,我已经为java小程序编写了一个绘画方法,这允许我的绘图字符串根据用户输入到三个文本字段(R、G、B)中的内容改变颜色。我的默认字体颜色是黑色,但我正在尝试将其更改为绿色,但无法更改,因为要更改字体颜色,我必须编写g.setColor(mixColor)。那么,有人知道当我运行applet时,如何使字体颜色从绿色开始,而不是黑色 import java.awt.*; import java.awt.event.*; import java.applet.*; public class
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
}
}
}
您可以使用
setForeground
方法。synatax是设置前景(Color.green)
。您可以使用颜色值而不是绿色。您可以使用“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);
}
}
这对现有代码没有任何影响,现有代码在调用graphics.setColor时忽略小程序的前景。编辑您的问题以显示redColor、greenColor和blueColor是如何声明和初始化的。现在我将添加我的全部代码您正在
init
方法中将redColor、greenColor和blueColor初始化为零。这意味着您的绘制方法正在有效地绘制新颜色(0,0,0),该颜色为黑色。您可能希望在系统上的任何简单绘图程序中使用颜色选择器,以便可以看到各种红色、绿色和蓝色的结果。对不起,我是java初学者,因此基本上如果我希望在运行小程序时字体颜色最初显示为绿色,是否更改rgb值我现在已经更改了,我刚刚将初始绿色值更改为255,非常感谢