|
Re: RGB - Color Question: Please Help
|
Posted: Dec 24, 2002 5:13 PM
|
|
I think you were trying to open the java code in JBuilder design window and you had missed some content. You get "com.borland.jbuilder.cmt.CmtParseException" error message in JBuilder when JBuilder is not able to parse the source code successfully. Try copying and pasting the code again and it will work. Following is the code where you have to enter rgb values in text fields. I have just changed the previous code using sliders to text fields.
package colorchooser;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.* ;
import java.awt.event.*;
public class CCApplet extends JApplet {
private final static int RED = 0;
private final static int GREEN = 1;
private final static int BLUE = 2;
JPanel mainPanel = new JPanel();
JPanel scrollPanels = new JPanel();
GridBagLayout gridBagLayout1 = new GridBagLayout();
JLabel redLabel = new JLabel();
JLabel greenLabel = new JLabel();
JLabel blueLabel = new JLabel();
GridBagLayout gridBagLayout2 = new GridBagLayout();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel redDecimal = new JLabel();
JLabel greenDecimal = new JLabel();
JLabel jLabel7 = new JLabel();
JLabel redHex = new JLabel();
JLabel greenHex = new JLabel();
JLabel jLabel10 = new JLabel();
JLabel redOctal = new JLabel();
JLabel greenOctal = new JLabel();
JLabel jLabel13 = new JLabel();
JLabel blueDecimal = new JLabel();
JLabel blueHex = new JLabel();
JLabel blueOctal = new JLabel();
JLabel colorLabel = new JLabel();
JLabel jLabel5 = new JLabel();
JTextField redValue = new JTextField();
JTextField greenValue = new JTextField();
JTextField blueValue = new JTextField();
public CCApplet() throws HeadlessException {
}
public void init() {
try {
jbInit();
this.setDetails();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**
* sets the color in label and also sets the color values in
* decimal and hex
*/
public void setDetails ( ) {
// Get color values
try {
int red = this.getColorValue(RED);
int green = this.getColorValue(GREEN);
int blue = this.getColorValue(BLUE);
// Set the decimal values
this.redDecimal.setText(red + "");
this.greenDecimal.setText(green + "");
this.blueDecimal.setText(blue + "");
// In hex
this.redHex.setText(Integer.toHexString(red) + "");
this.greenHex.setText(Integer.toHexString(green) + "");
this.blueHex.setText(Integer.toHexString(blue) + "");
// In Octal
this.redOctal.setText(Integer.toOctalString(red) + "");
this.greenOctal.setText(Integer.toOctalString(green) + "");
this.blueOctal.setText(Integer.toOctalString(blue) + "");
// In RGB form
// set the backgrond color for label
colorLabel.setBackground(new Color(red, green, blue));
}
catch (Exception e ) {
JOptionPane.showMessageDialog(this, e.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
public int getColorValue ( int color ) {
String colorName = "";
JTextField tf ;
switch ( color ) {
case RED:
tf = this.redValue;
colorName = "red";
break;
case GREEN:
tf = this.greenValue;
colorName = "green";
break;
case BLUE:
tf = this.blueValue;
colorName = "blue";
break;
default:
throw new RuntimeException ( "Color name must be red, green or blue" );
}
// Get the value entered bu user
String value = tf.getText();
if ( value == null || value.trim().equals("") ) {
value = "0" ;
}
// Try to parse the value
int intValue = 0 ;
try {
intValue = Integer.parseInt(value);
}
catch ( Exception e ) {
tf.requestFocus();
throw new RuntimeException ( colorName + " value must be an integer" ) ;
}
// Check the range
if ( intValue < 0 || intValue > 255 ) {
tf.requestFocus();
throw new RuntimeException ( colorName + " value must be between 0-255" ) ;
}
return intValue;
}
public static void main(String[] args) throws HeadlessException {
CCApplet colorChooser1 = new CCApplet();
colorChooser1.init();
JFrame f = new JFrame ("Color Chooser" ) ;
f.setBounds ( 20, 20 , 400, 400 ) ;
f.getContentPane().add ( colorChooser1 ) ;
f.show() ;
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(new BorderLayout());
mainPanel.setLayout(gridBagLayout2);
scrollPanels.setLayout(gridBagLayout1);
scrollPanels.setMinimumSize(new Dimension(300, 300));
scrollPanels.setPreferredSize(new Dimension(300, 300));
redLabel.setBackground(Color.red);
redLabel.setText("Red:");
greenLabel.setText("Green:");
blueLabel.setText("Blue:");
mainPanel.setMinimumSize(new Dimension(100, 100));
mainPanel.setPreferredSize(new Dimension(100, 100));
jLabel1.setFont(new java.awt.Font("SansSerif", 1, 11));
jLabel1.setText("Color");
jLabel2.setFont(new java.awt.Font("SansSerif", 0, 11));
jLabel2.setText("Red:");
jLabel3.setFont(new java.awt.Font("SansSerif", 0, 11));
jLabel3.setText("Green:");
jLabel4.setFont(new java.awt.Font("SansSerif", 1, 11));
jLabel4.setText("Decimal");
redDecimal.setFont(new java.awt.Font("SansSerif", 0, 11));
redDecimal.setText("jLabel5");
greenDecimal.setFont(new java.awt.Font("Dialog", 0, 11));
greenDecimal.setText("jLabel6");
jLabel7.setFont(new java.awt.Font("SansSerif", 1, 11));
jLabel7.setText("Hex");
redHex.setFont(new java.awt.Font("Dialog", 0, 11));
redHex.setText("jLabel8");
greenHex.setFont(new java.awt.Font("Dialog", 0, 11));
greenHex.setText("jLabel9");
jLabel10.setFont(new java.awt.Font("SansSerif", 1, 11));
jLabel10.setText("Octal");
redOctal.setFont(new java.awt.Font("Dialog", 0, 11));
redOctal.setText("jLabel11");
greenOctal.setFont(new java.awt.Font("Dialog", 0, 11));
greenOctal.setText("jLabel12");
jLabel13.setFont(new java.awt.Font("SansSerif", 0, 11));
jLabel13.setText("Blue:");
blueDecimal.setFont(new java.awt.Font("Dialog", 0, 11));
blueDecimal.setText("jLabel14");
blueHex.setFont(new java.awt.Font("Dialog", 0, 11));
blueHex.setText("jLabel15");
blueOctal.setFont(new java.awt.Font("Dialog", 0, 11));
blueOctal.setText("jLabel16");
colorLabel.setBackground(Color.pink);
colorLabel.setFont(new java.awt.Font("SansSerif", 0, 11));
colorLabel.setForeground(Color.black);
colorLabel.setMinimumSize(new Dimension(100, 20));
colorLabel.setOpaque(true);
colorLabel.setPreferredSize(new Dimension(100, 20));
colorLabel.setText("");
jLabel5.setText("Color:");
redValue.setMinimumSize(new Dimension(60, 21));
redValue.setPreferredSize(new Dimension(60, 21));
redValue.setText("100");
redValue.addKeyListener(new CCApplet_redValue_keyAdapter(this));
greenValue.setMinimumSize(new Dimension(60, 21));
greenValue.setPreferredSize(new Dimension(60, 21));
greenValue.setText("150");
greenValue.addKeyListener(new CCApplet_greenValue_keyAdapter(this));
blueValue.setMinimumSize(new Dimension(60, 21));
blueValue.setPreferredSize(new Dimension(60, 21));
blueValue.setText("255");
blueValue.addKeyListener(new CCApplet_blueValue_keyAdapter(this));
this.getContentPane().add(mainPanel, BorderLayout.NORTH);
mainPanel.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(jLabel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(jLabel3, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(jLabel4, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(redDecimal, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(greenDecimal, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(jLabel7, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(redHex, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(greenHex, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(jLabel10, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(redOctal, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(greenOctal, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(jLabel13, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(blueDecimal, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(blueHex, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
mainPanel.add(blueOctal, new GridBagConstraints(3, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3), 0, 0));
this.getContentPane().add(scrollPanels, BorderLayout.SOUTH);
scrollPanels.add(greenLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 5, 5));
scrollPanels.add(blueLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 5, 5));
scrollPanels.add(redLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 5, 5));
scrollPanels.add(jLabel5, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
scrollPanels.add(redValue, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 37, 0));
scrollPanels.add(greenValue, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 34, 0));
scrollPanels.add(blueValue, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 33, 0));
scrollPanels.add(colorLabel, new GridBagConstraints(1, 0, 4, 1, 0.0, 0.0
,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 5, 0), 147, 15));
}
void redValue_keyReleased(KeyEvent e) {
this.setDetails();
}
void greenValue_keyReleased(KeyEvent e) {
this.setDetails();
}
void blueValue_keyReleased(KeyEvent e) {
this.setDetails();
}
}
class CCApplet_redValue_keyAdapter extends java.awt.event.KeyAdapter {
CCApplet adaptee;
CCApplet_redValue_keyAdapter(CCApplet adaptee) {
this.adaptee = adaptee;
}
public void keyReleased(KeyEvent e) {
adaptee.redValue_keyReleased(e);
}
}
class CCApplet_greenValue_keyAdapter extends java.awt.event.KeyAdapter {
CCApplet adaptee;
CCApplet_greenValue_keyAdapter(CCApplet adaptee) {
this.adaptee = adaptee;
}
public void keyReleased(KeyEvent e) {
adaptee.greenValue_keyReleased(e);
}
}
class CCApplet_blueValue_keyAdapter extends java.awt.event.KeyAdapter {
CCApplet adaptee;
CCApplet_blueValue_keyAdapter(CCApplet adaptee) {
this.adaptee = adaptee;
}
public void keyReleased(KeyEvent e) {
adaptee.blueValue_keyReleased(e);
}
}
|
|