![]()
| |
********** Steps to generate this Example... ******************************* // Step 1) Put these three lines in your html file. <applet code="yourjavacode.class" width="350" height="350"> This program requires java enabled browser. </applet> // Step 2) Run command line utility>>> htmlconverter thishtmlfile.html // Step 3) Put the java code in a file, yourjavacode.java // Step 4) Generate class file>>> javac yourjavacode.java // ********** Java for this Example... ************************************* // SwingColorApplet.java - From Watch.java and SwingColorTest.java combined. import java.awt.*; import java.util.*; // for SwingColorApplet.java import java.awt.event.*; import javax.swing.*; // for slider import javax.swing.event.*; // ***************************************************************************** // ===Major Applet Activities (Aug/19/2002 MON) (pg.168) // 1) Initialization - public void init() { /*code*/ } (pg.168) // - sample /*code*/: setBackground(orange); // wont be shown until applet window is redrawn. // 2) Starting - public void start() { /*code*/ } // start (?) or return to pg. (pg.169) // 3) Stopping - public void stop() { /*code*/ } // when leave pg, or call to stop(). (pg.169) // 4) Destruction - public void destroy() { /*code*/ } // often not overridden. (pg.170) // - kill running threads, or release running objects. // 5) Painting - public void paint(Graphics g) { /*code*/ } // display to screen (pg.170) // - text, line, background, image, etc. // - paint may run hundreds of times: when initialized, or browser brought from behind other window, different position... // - "Graphics g" is an instance of class Graphics. It is created and supplied by the browser. // - Needed: import of java.awt.Graphics, usually by import statment at top of your Java source. // - Note: "*" used in w/import statement, does not include subclasses of the package. // - if you need to force a call to paint, call repaint(); // - if Graphics2D needed cast the "Graphics g" parameter. // - most applet work is done inside paint(); // ***************************************************************************** public class SwingColorApplet extends javax.swing.JApplet { JPanel colorPanel = new JPanel(); SwingColorControls rgbPanel; SwingColorControls hsbPanel; int[] maxRgbValue = {255,255,255}; int[] maxHsbValue = {360,100,100}; // Aug/07/2003 Thu (ver 142) boolean allowHexidecimalToggle = true; // false: eliminate hex mode completely. boolean isColorPanelHexidecimal = true; JTextField colorFld; public void init() { //n/a super("SwingColorTest : Day11, Pg283."); //n/a setBounds(0, 0, 650, 150); // before Aug/09/03: setBounds(0, 25, 700, 180); JPanel pane10 = new JPanel(); colorPanel.setBackground(Color.black); colorPanel.setPreferredSize(new Dimension(100,50)); String rgbLabels[] = {"Red"," Green","Blue"}; rgbPanel = new SwingColorControls(this,rgbLabels,maxRgbValue); if (allowHexidecimalToggle) rgbPanel.setHexidecimal(isColorPanelHexidecimal); String hsbLabels[] = {"Hue","Saturation","Brightness"}; hsbPanel = new SwingColorControls(this,hsbLabels,maxHsbValue); if (allowHexidecimalToggle) { // Aug/07/2003 Thu Adding GridBagConstraints (ver 142) GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); pane10.setLayout(gridbag); buildConstraints(constraints, 0, 0, 1, 2, 60, 40); constraints.fill = GridBagConstraints.BOTH; constraints.anchor = GridBagConstraints.CENTER; gridbag.setConstraints(colorPanel, constraints); pane10.add(colorPanel); buildConstraints(constraints, 1, 0, 1, 1, 5, 1); // constraints.fill = GridBagConstraints.HORIZONTAL; gridbag.setConstraints(rgbPanel, constraints); pane10.add(rgbPanel); buildConstraints(constraints, 2, 0, 1, 1, 5, 1); // constraints.fill = GridBagConstraints.HORIZONTAL; gridbag.setConstraints(hsbPanel, constraints); pane10.add(hsbPanel); buildConstraints(constraints, 1, 1, 1, 1, 0, 0); constraints.fill = GridBagConstraints.NONE; Checkbox chkBox_useHexidecmimal = new Checkbox("Hexidecimal Mode",isColorPanelHexidecimal); chkBox_useHexidecmimal.addItemListener(new ChkBoxL(this)); // .addChangeListener(new SlidL(this)); gridbag.setConstraints(chkBox_useHexidecmimal, constraints); pane10.add(chkBox_useHexidecmimal); // buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) { buildConstraints(constraints, 2, 1, 1, 1, 0, 0); constraints.fill = GridBagConstraints.HORIZONTAL; colorFld = new JTextField("000000"); // tFld[i].addFocusListener(this); // tFld[i].addActionListener(this); gridbag.setConstraints(colorFld, constraints); pane10.add(colorFld); } else { GridLayout lOut10 = new GridLayout(1,3 ,5,15); pane10.setLayout(lOut10); pane10.add(colorPanel); pane10.add(rgbPanel); pane10.add(hsbPanel); } setContentPane(pane10); setVisible(true); // FOR APPLET } // Aug/07/2003 Thu (ver 142) // buildConstraints() method reminds you of each constraint and reduces typing. void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) { gbc.gridx = gx; // X-cell-coordinate for left edge of Component's display area's. gbc.gridy = gy; // Y-cell-coordinate for top edge of Component's display area's. gbc.gridwidth = gw; // Num of cell-columns the component's display area will span. gbc.gridheight = gh; // Num of cell-rows the component's display area will span. gbc.weightx = wx; // Specifies how to distribute extra horizontal space. gbc.weighty = wy; // Specifies how to distribute extra vertical space. } public void start() { // repaint(); } public void update(SwingColorControls control) { Color c; int value[] = new int[3], maxVal; for (int i=0; i<3; i++) { // Aug/08/03 Fri (ver 142) if (control.isHexidecimal()) value[i] = Integer.parseInt(control.tFld[i].getText(), 16); else value[i] = Integer.parseInt(control.tFld[i].getText()); if (value[i] < 0) { value[i] = 0; // Aug/07/2003 Thu (ver 142) if (control.isHexidecimal()) control.tFld[i].setText(Integer.toHexString(value[i])); else control.tFld[i].setText("" + value[i]); } if (control == rgbPanel) maxVal = maxRgbValue[i]; else maxVal = maxHsbValue[i]; if (value[i] > maxVal) { value[i] = maxVal; // Aug/07/2003 Thu (ver 142) if (control.isHexidecimal()) control.tFld[i].setText(Integer.toHexString(value[i])); else control.tFld[i].setText("" + value[i]); } } if (control == rgbPanel) { c = new Color(value[0],value[1],value[2]); float hsb[] = Color.RGBtoHSB(value[0],value[1],value[2],new float[3]); hsb[0] *= maxHsbValue[0]; hsb[1] *= maxHsbValue[1]; hsb[2] *= maxHsbValue[2]; for (int i=0; i<3; i++) { // Aug/07/2003 Thu (ver 142) if (hsbPanel.isHexidecimal()) hsbPanel.tFld[i].setText(Integer.toHexString((int)hsb[i])); else hsbPanel.tFld[i].setText(String.valueOf((int)hsb[i])); hsbPanel.slider[i].setValue((int)hsb[i]); } } else { c = Color.getHSBColor((float)value[0]/maxHsbValue[0],(float)value[1]/maxHsbValue[1],(float)value[2]/maxHsbValue[2]); // Aug/07/2003 Thu (ver 142) if (rgbPanel.isHexidecimal()) { rgbPanel.tFld[0].setText(Integer.toHexString(c.getRed()) ); rgbPanel.tFld[1].setText(Integer.toHexString(c.getGreen())); rgbPanel.tFld[2].setText(Integer.toHexString(c.getBlue()) ); } else { rgbPanel.tFld[0].setText(String.valueOf(c.getRed()) ); rgbPanel.tFld[1].setText(String.valueOf(c.getGreen())); rgbPanel.tFld[2].setText(String.valueOf(c.getBlue()) ); } rgbPanel.slider[0].setValue(c.getRed() ); rgbPanel.slider[1].setValue(c.getGreen()); rgbPanel.slider[2].setValue(c.getBlue() ); } colorPanel.setBackground(c); colorPanel.repaint(); } public Insets getInsets() { return(new Insets(10,10,10,10)); } public void buildColorField() // bag { String color1 = ""; String color3 = ""; if (rgbPanel.isHexidecimal()) { color1 = rgbPanel.tFld[0].getText(); color3 = lpad2Zeros(color1); color1 = rgbPanel.tFld[1].getText(); color3 += lpad2Zeros(color1); color1 = rgbPanel.tFld[2].getText(); color3 += lpad2Zeros(color1); } else { color3 = ""; for (int i=0; i<3; i++) { color1 = "" + Integer.toHexString(Integer.parseInt(rgbPanel.tFld[i].getText())); color3 += lpad2Zeros(color1); } } colorFld.setText(color3); } public String lpad2Zeros(String str) { char retAy[] = new char[2]; char strAy[] = str.toCharArray(); if (str.length() == 0) { retAy[0] = '0'; retAy[1] = '0'; } else if (str.length() == 1) { retAy[0] = '0'; retAy[1] = strAy[0]; } else if (str.length() > 1) { retAy[0] = strAy[0]; retAy[1] = strAy[1]; } return(String.valueOf(retAy)); } } /** Listens to the check box. */ class ChkBoxL implements ItemListener { // itemStateChanged(ItemEvent e) SwingColorApplet frame; public ChkBoxL(SwingColorApplet pFrame) { super(); frame = pFrame; } public void itemStateChanged(ItemEvent e) { Checkbox source = (Checkbox)e.getSource(); frame.rgbPanel.setHexidecimal(source.getState()); int sVal = 0; for (int i=0; i<3; i++) { // Aug/08/03 Fri (ver 142) if (!frame.rgbPanel.isHexidecimal()) sVal = Integer.parseInt(frame.rgbPanel.tFld[i].getText(), 16); else sVal = Integer.parseInt(frame.rgbPanel.tFld[i].getText()); if (frame.rgbPanel.isHexidecimal()) frame.rgbPanel.tFld[i].setText(Integer.toHexString(sVal)); else frame.rgbPanel.tFld[i].setText("" + sVal); // rgbPanel.tFld[i].update(); } frame.buildColorField(); frame.rgbPanel.update(frame.rgbPanel.getGraphics()); } } // public class SwingColorControls extends JPanel implements ActionListener, FocusListener class SwingColorControls extends JPanel implements ActionListener, FocusListener { SwingColorApplet frame; JTextField tFld[] = new JTextField[3]; JSlider slider[] = new JSlider[3]; // Aug/09/2003 Sat (ver 142) boolean useHexidecimal = false; public SwingColorControls(SwingColorApplet parent, String[] label, int[] maxVal) { super(); frame = parent; setLayout(new GridLayout(3,3 ,10,10)); for (int i=0; i<3; i++) { add(new JLabel(label[i], JLabel.RIGHT)); tFld[i] = new JTextField("0"); tFld[i].addFocusListener(this); tFld[i].addActionListener(this); add(tFld[i]); slider[i] = new JSlider(0,maxVal[i],0); slider[i].addChangeListener(new SlidL(this,parent,slider,tFld)); add(slider[i]); } } public Insets getInsets() { return(new Insets(10,10,10,10)); } // Invoked when an action occurs. public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JTextField) frame.update(this); } // Invoked when a component gains the keyboard focus. public void focusGained(FocusEvent e) { } // Invoked when a component loses the keyboard focus. public void focusLost(FocusEvent e) { frame.update(this); } // Aug/09/2003 Sat (ver 142) // Returns current Hexidecimal mode. public boolean isHexidecimal() { return(useHexidecimal); } // Aug/09/2003 Sat (ver 142) // Sellects Hexidecimal mode true or false. public boolean setHexidecimal(boolean newValue) { boolean previousValue = useHexidecimal; useHexidecimal = newValue; return(previousValue); } } /** Listens to the slider. */ class SlidL implements ChangeListener { SwingColorControls control; SwingColorApplet frame; JSlider slider[]; JTextField tFld[]; public SlidL(SwingColorControls parent,SwingColorApplet pFrame,JSlider sliderParent[],JTextField tFldParent[]) { super(); control = parent; frame = pFrame; slider = sliderParent; tFld = tFldParent; } public void stateChanged(ChangeEvent e) { JSlider source = (JSlider)e.getSource(); if (source.getValueIsAdjusting()) { int sVal = (int)source.getValue(); for (int i=0; i<3; i++) { if (source == slider[i]) { // Aug/09/2003 Sat (ver 142) if (control.isHexidecimal()) tFld[i].setText(Integer.toHexString(sVal)); else tFld[i].setText("" + sVal); // frame2.update(this); frame.buildColorField(); frame.update(control); break; } } } } } // end of *.java file