让你输入一段英文,然后利用Scrollbar控制RGB来改变里面元音的颜色,显示出来,最后还要显示出输入英文中各个元音的数量~~~~
题目大概如此吧,最后一步显示出元音的数量我不知道怎么解决~~~~~~~~~~~~~~查啦一些资料也没有找到类似的东西
希望有高人指点一下~~~~~`
[此贴子已经被作者于2006-1-19 20:06:17编辑过]
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Oo extends Applet implements TextListener,AdjustmentListener,ActionListener{
Label title;
Label vowels;
TextField msginput;
String msg=null;
Button submit;
Scrollbar sliderred;
Scrollbar slidergreen;
Scrollbar sliderblue;
int sliderredValue=0;
int slidergreenValue=0;
int sliderblueValue=0;
public void init() {
title=new Label("INPUT TEXT");
msginput=new TextField();
vowels=new Label("COLOR OF VOWELS");
add(title);
add(msginput);
add(vowels);
sliderred= new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,256);
add(sliderred);
slidergreen= new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,256);
add(slidergreen);
sliderblue= new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,256);
add(sliderblue);
submit=new Button("submit");
add(submit);
msginput.addTextListener(this);
sliderred.addAdjustmentListener(this);
slidergreen.addAdjustmentListener(this);
sliderblue.addAdjustmentListener(this);
submit.addActionListener(this);
}
public void textValueChanged(TextEvent event){
msg=msginput.getText();
repaint();
}
public void adjustmentValueChanged(AdjustmentEvent event){
sliderredValue=sliderred.getValue();
slidergreenValue=slidergreen.getValue();
sliderblueValue=sliderblue.getValue();
repaint();
}
public void actionPerformed(ActionEvent event){
sliderredValue=Integer.parseInt(msginput.getText());
slidergreenValue=Integer.parseInt(msginput.getText());
sliderblueValue=Integer.parseInt(msginput.getText());
repaint();
}
public void paint(Graphics g){
g.setColor(new Color(sliderredValue,slidergreenValue,sliderblueValue));
g.drawString("Vowels are displayed id RCB color of "+""+sliderredValue+","
+slidergreenValue+","+sliderblueValue+")",100,50);
g.drawString(msg,50,70);
}
}
我想用button去控制它,但有问题,希望高手能指教一下,谢谢啦~
[此贴子已经被作者于2006-1-15 21:44:03编辑过]
谢谢大家的帮助,这个我已经会啦,完成啦,大家看下还有什么可以简化的么
import java.awt.*;
import java.applet.*;
import java.awt.event.*;//import classes for handling of events
import javax.swing.*;
public class VowelAnalyzer extends Applet implements TextListener,AdjustmentListener,ActionListener {
Panel p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13;//declare new panels
Label input,color,red,green,blue;//declare the Label objects
TextField textfield;//declare textfield object
Scrollbar sliderred,slidergreen,sliderblue;//declare scrollbar objects
Button b1;//declare button object
String msg=null;//declare empty String msg
int count;//declare a loop variable
int sliderredValue,slidergreenValue,sliderblueValue=0;//initialize the integer to 0
int countA;//declare a loop variable
int countE;//declare a loop variable
int countI;//declare a loop variable
int countO;//declare a loop variable
int countU;//declare a loop variable
public void init() {//Initialize the applet
setBackground(Color.white);
input=new Label("Input Text : ");//create new label object
add(input);//add object to applet window
textfield=new TextField(30);//create new textfield object
add(textfield);//add object to applet window
color=new Label("Color Of Vowels : " );//create new label object
add(color);//add object to applet window
red=new Label("Red");//create new label object
add(red);//add object to applet window
green=new Label("Green");//create new label object
add(green);//add object to applet window
blue=new Label("Blue");//create new label object
add(blue);//add object to applet window
sliderred=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,256);//create new scrollbar object
add(sliderred);//add object to applet window
slidergreen=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,256);//create new scrollbar object
add(slidergreen);//add object to applet window
sliderblue=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,256);//create new scrollbar object
add(sliderblue);//add object to applet window
b1=new Button("Submit");//create new button object
add(b1);//add object to applet window
b1.addActionListener(this);//attach listener to object input
p1=new Panel();//create new panel
p2=new Panel();//create new panel
p3=new Panel();//create new panel
p4=new Panel();//create new panel
p5=new Panel();//create new panel
p6=new Panel();//create new panel
p7=new Panel();//create new panel
p8=new Panel();//create new panel
p9=new Panel();//create new panel
p10=new Panel();//create new panel
p11=new Panel();//create new panel
p12=new Panel();//create new panel
p13=new Panel();//create new panel
p10.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
p10.add(color);
p9.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
p9.add(textfield);
p8.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
p8.add(input);
p11.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
p11.add(red);
p12.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
p12.add(green);
p13.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
p13.add(blue);
p7.setLayout(new BorderLayout());//set Layout as BorderLayout
p7.add("North",p12);//add object to the Panel p6
p7.add("South",slidergreen);//add object to the Panel p6
p6.setLayout(new BorderLayout());//set Layout as BorderLayout
p6.add("North",p13);//add object to the Panel p5
p6.add("South",sliderblue);//add object to the Panel p5
p5.setLayout(new BorderLayout());//set Layout as BorderLayout
p5.add("North",p11);//add object to the Panel p4
p5.add("South",sliderred);//add object to the Panel p4
p4.setLayout(new GridLayout(1,3));//set Layout as BorderLayout
p4.add(p5);//add object to the Panel p3
p4.add(p7);//add object to the Panel p3
p4.add(p6);//add object to the Panel p3
p3.setLayout(new BorderLayout());//set Layout as BorderLayout
p3.add("North",p10);//add object to the Panel p2
p3.add("South",p4);//add object to the Panel p2
p2.setLayout(new BorderLayout());//set Layout as BorderLayout
p2.add("North",p8);//add object to the Panel p1
p2.add("South",p9);//add object to the Panel p1
p1.setLayout(new BorderLayout());//set Layout as BorderLayout
p1.add("North",p2);//add object to the Panel p
p1.add("Center",p3);//add object to the Panel p
p1.add("South",b1);//add object to the Panel p
add(p1);//add panel p to applet window
}//end init() method
public void textValueChanged(TextEvent e) {// to handle the text
repaint();//call for paint() method, refresh applet window
}//end of textValueChanged() method
public void adjustmentValueChanged(AdjustmentEvent event){ // to handle the scrollbar
repaint();//call for paint() method, refresh applet window
}//end of adjustmentValueChanged() method
public void actionPerformed(ActionEvent event){// to handle event
msg=textfield.getText();//read in text from user input
sliderredValue=sliderred.getValue();//get value from slider and store to sliderValue
slidergreenValue=slidergreen.getValue();//get value from slider1 and store to slider1Value
sliderblueValue=sliderblue.getValue();//get value from slider2 and store to slider2Value
reset();//call for reset() method
repaint();//call for paint() method, refresh applet window
}//end of actionPerformed() method
public void paint(Graphics g) {//paint method
g.drawString("Vowels are displayed in RGB color of ("+sliderredValue+","+slidergreenValue+","+sliderblueValue+").",0,200);
String Vowelx;//declare a String Vowelx
int location = 0;//declare and initialize location to 0
for(count=0;count<=msg.length()-1;count=count+1) {
Vowelx=msg.substring(count,count+1).toLowerCase();
if (Vowelx.equals("a") | Vowelx.equals("e") | Vowelx.equals("i") |
Vowelx.equals("o") | Vowelx.equals("u")) {
g.setColor(new Color(sliderredValue,slidergreenValue,sliderblueValue));//set color from values of individual scrollbars
}
else {//if above condition is FALSE
g.setColor(Color.black);//set color black
}
g.drawString(Vowelx + "", location,180);//execute statement after submit
location = location + 20;//increase value of location by 10
}//end for loop
g.setColor(Color.black);//set color black
//execute statement after submit
g.drawString("Occurrence of vowels: ", 0, 220 );//execute statement after submit
for(count=0;count<=msg.length()-1;count=count+1){
Vowelx=msg.substring(count,count+1).toLowerCase();
if (Vowelx.equals("a")) {
countA++;//count for vowel A
}
if (Vowelx.equals("e")) {
countE++;//count for vowel E
}
if (Vowelx.equals("i")) {
countI++;//count for vowel I
}
if (Vowelx.equals("o")) {
countO++;//count for vowel O
}
if (Vowelx.equals("u")) {
countU++;//count for vowel U
}
}//end for loop
if (countA!=0)
g.drawString("a: "+countA, 20, 240 );//execute statement after submit
if (countE!=0)
g.drawString("e: "+countE, 20, 260 );//execute statement after submit
if (countI!=0)
g.drawString("i: "+countI, 20, 280 );//execute statement after submit
if (countO!=0)
g.drawString("o: "+countO, 20, 300 );//execute statement after submit
if (countU!=0)
g.drawString("u: "+countU, 20, 320);//execute statement after submit
}//end paint() method
public void reset() {
countA = 0; // keep A
countE = 0; // keep E
countI = 0; // keep I
countO = 0; // keep O
countU = 0; // keep U
}//end reset() method
}//end class