⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 colourapplet.java

📁 此程序可实现:在一个可视化界面中随机生成3种safe-colour(不显示白色);用户可将颜色自由添加至4种类似的色族中;程序最后显示出用户添加颜色的分布。
💻 JAVA
字号:
/**
 * An Applet class to creat a frame and some buttons. 
 * When a user presses the “generate colour” button a random colour produced.
 * After the colour has been displayed to the user, 
 * the user can choose what colour the displayed colour most represents. 
 * The program will save the colour so that it cannot be generated again. 
 * And it will show the detial of the colour you press.
 * @author Ruixiao Wu
 * @version 1.0  02/06/2006
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;

public class ColourApplet extends JApplet {
	/**
	 * A class extends Japplet
	 * 
	 * @author Ruixiao Wu
	 * @version 1.0 02/06/2006
	 */
	private static final long serialVersionUID = 6231585757959381193L;

	private static final int red = 255;

	// declare some globle variables.
	static ArrayList<Integer> greenList = new ArrayList<Integer>();

	static ArrayList<Integer> blueList = new ArrayList<Integer>();

	ArrayList<Integer> redCountList = new ArrayList<Integer>();

	ArrayList<Integer> pinkCountList = new ArrayList<Integer>();

	ArrayList<Integer> orangeCountList = new ArrayList<Integer>();

	ArrayList<Integer> yellowCountList = new ArrayList<Integer>();

	// creat three panels.
	JPanel textPanel1 = new JPanel();

	JPanel textPanel2 = new JPanel();

	JPanel windowPanel = new JPanel();

	JPanel buttonPanel = new JPanel();

	// creat five butttons.
	JButton generateButton = new JButton("generate colour");

	JButton redButton = new JButton("red");

	JButton pinkButton = new JButton("pink");

	JButton orangeButton = new JButton("orange");

	JButton yellowButton = new JButton("yellow");

	// creat two text field to show the information of the program.
	JTextField textField1 = new JTextField(45);

	JTextField textField2 = new JTextField(45);

	// init method.
	public void init() {
		// creat a container and set some parameters of it.
		Container container = getContentPane();
		container.setBackground(Color.BLACK);
		container.setLayout(new BorderLayout(0, 0));
		setSize(500, 500);

		// set parameters of the button panel.
		buttonPanel.setSize(500, 40);
		buttonPanel.setLocation(0, 460);

		// add buttons to the button panel.
		buttonPanel.add(generateButton);
		buttonPanel.add(redButton);
		buttonPanel.add(pinkButton);
		buttonPanel.add(orangeButton);
		buttonPanel.add(yellowButton);

		// set parameters of the text panel and add text fields to the text
		// panel.
		// textPanel1.setLayout(new GridLayout(1, 2));
		textPanel1.setSize(500, 30);
		textPanel2.setSize(500, 28);
		textPanel1.setLocation(0, 0);
		textPanel2.setLocation(0, 30);
		textPanel1.add(textField1);
		textPanel2.add(textField2);
		textField1.setText("Press \"generate\" to generate a colour!");

		// set parameters of the window panel.
		windowPanel.setSize(500, 400);
		windowPanel.setLocation(0, 60);
		windowPanel.getLayout();
		windowPanel.setBackground(Color.BLACK);

		// add three panels to the container and set their locations.
		container.add(textPanel1, BorderLayout.NORTH);
		container.add(textPanel2, BorderLayout.CENTER);
		container.add(windowPanel, BorderLayout.CENTER);
		container.add(buttonPanel, BorderLayout.SOUTH);

		// creat the colour list(green and blue), let them store the variable of
		// the safe colour.
		for (int i = 0; i <= 255; i += 51)
			for (int j = 0; j <= 255; j += 51) {
				greenList.add(i);
				blueList.add(j);
			}

		// creat 5 button actions.
		buttonAction genegrateAction = new buttonAction();
		buttonAction redAction = new buttonAction();
		buttonAction pinkAction = new buttonAction();
		buttonAction orangeAction = new buttonAction();
		buttonAction yellowAction = new buttonAction();

		// add the actions to the ActionListener.
		generateButton.addActionListener(genegrateAction);
		redButton.addActionListener(redAction);
		pinkButton.addActionListener(pinkAction);
		orangeButton.addActionListener(orangeAction);
		yellowButton.addActionListener(yellowAction);
	}

	/**
	 * A classd to deal with the actions.
	 */
	boolean checkButton = true;// declare a boolean variable to make sure each
								// button can be press once!

	int checkFinalButton;

	public class buttonAction implements ActionListener {
		public buttonAction() {

		}

		/**
		 * A method to perform actions.
		 * 
		 * @param checkButton,action,count,i
		 */
		public void actionPerformed(ActionEvent event) {
			Object action = event.getSource();// creat the Object in order to
												// performe below.
			int count = greenList.size();// the same to the blueList.
			int i = (int) (Math.random() * count);// get random number in
													// order to print randomly.

			if (action.equals(generateButton) && checkButton == true) {
				if (MyColour.checkColor(red, greenList.get(i), blueList.get(i)) == true) {
					textField1.setText("I won't print white!");
					textField2
							.setText("Press \"generate\" to generate another colour!");
					windowPanel.setBackground(Color.BLACK);
					greenList.remove(i);
					blueList.remove(i);
					checkButton = true;
				} else {
					windowPanel.setBackground(new Color(255, greenList.get(i),
							blueList.get(i)));
					textField2.setText("Colour in RGB: R=255,G="
							+ greenList.get(i) + ",B=" + blueList.get(i)
							+ "\tColour in hexadecimal: ##FF"
							+ MyColour.convertGreenTohex(greenList.get(i))
							+ MyColour.convertBlueTohex(blueList.get(i)) + ".");
					textField1.setText("Press \"colour\" button to add color!");
					greenList.remove(i);
					blueList.remove(i);
					count--;
					checkButton = false;
				}
			}
			if (action.equals(redButton) && checkButton == false) {
				redCountList.add(1);
				textField1.setText("Press \"generate\" to generate a colour!");
				checkButton = true;
				checkFinalButton = 1;
			}
			if (action.equals(pinkButton) && checkButton == false) {
				pinkCountList.add(1);
				textField1.setText("Press \"generate\" to generate a colour!");
				checkButton = true;
				checkFinalButton = 2;
			}
			if (action.equals(orangeButton) && checkButton == false) {
				orangeCountList.add(1);
				textField1.setText("Press \"generate\" to generate a colour!");
				checkButton = true;
				checkFinalButton = 3;
			}
			if (action.equals(yellowButton) && checkButton == false) {
				yellowCountList.add(1);
				textField1.setText("Press \"generate\" to generate a colour!");
				checkButton = true;
				checkFinalButton = 4;
			}

			// declare 4 variable to record the user's input.
			int redcount = redCountList.size();
			int pinkcount = pinkCountList.size();
			int orangecount = orangeCountList.size();
			int yellowcount = yellowCountList.size();

			// print the detial information of the user's input.
			if (redcount + pinkcount + yellowcount + orangecount == 35) {
				textField1.setText("Ends!  \tThe Total colour is:35.");
				if (checkFinalButton == 1)
					textField2.setText("Red = " + redcount + ", Pink = "
							+ pinkcount + ", orange = " + orangecount
							+ ", yellow = " + yellowcount
							+ "\tThe final colour it shows is: red.");
				else if (checkFinalButton == 2)
					textField2.setText("Red = " + redcount + ", Pink = "
							+ pinkcount + ", orange = " + orangecount
							+ ", yellow = " + yellowcount
							+ "\tThe final colour it shows is: pink.");
				else if (checkFinalButton == 3)
					textField2.setText("Red = " + redcount + ", Pink = "
							+ pinkcount + ", orange = " + orangecount
							+ ", yellow = " + yellowcount
							+ "\tThe final colour it shows is: orange.");
				else if (checkFinalButton == 4)
					textField2.setText("Red = " + redcount + ", Pink = "
							+ pinkcount + ", orange = " + orangecount
							+ ", yellow = " + yellowcount
							+ "\tThe final colour it shows is: yellow.");
			}// End if.
		}// End method.
	}//End buttonAction class.    
}//End ColourApplet class.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -