advertisement

Article:
  Creating a Color Meter Using Cocoa
Subject:   Java Version - Solution
Date:   2002-05-25 15:08:31
From:   jsumnertx
I've been doing these examples in Java rather than Objective C. Here's the Java version in case anyone else is trying to do the same.


Some notes:
1) Your controller MUST inherit from java.lang.Object, NOT NSObject or else it won't work and you won't get much information why.
2) The NSColor methods were a bit different
3) Use the actual data types of the controls for the outlets rather than "IBOutlet"


Enjoy!
/* Controller */


import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;


public class Controller extends java.lang.Object {


NSTextField alphaField; /* IBOutlet */
NSSlider alphaSlider; /* IBOutlet */
NSTextField blueField; /* IBOutlet */
NSSlider blueSlider; /* IBOutlet */
NSColorWell colorWell; /* IBOutlet */
NSTextField greenField; /* IBOutlet */
NSSlider greenSlider; /* IBOutlet */
NSTextField redField; /* IBOutlet */
NSSlider redSlider; /* IBOutlet */

float redValue=0;
float greenValue=0;
float blueValue=0;
float alphaValue=0;
public void setAlpha(NSControl sender) { /* IBAction */
alphaValue = sender.floatValue();
alphaField.setFloatValue(alphaValue);
alphaSlider.setFloatValue(alphaValue);
updateColor();
}


public void setBlue(NSControl sender) { /* IBAction */
blueValue = sender.floatValue();
blueField.setFloatValue(blueValue);
blueSlider.setFloatValue(blueValue);
updateColor();
}


public void setGreen(NSControl sender) { /* IBAction */
greenValue = sender.floatValue();
greenField.setFloatValue(greenValue);
greenSlider.setFloatValue(greenValue);
updateColor();
}


public void setRed(NSControl sender) { /* IBAction */
redValue = sender.floatValue();
redField.setFloatValue(redValue);
redSlider.setFloatValue(redValue);
updateColor();
}


private void updateColor(){
NSColor aColor;
aColor= NSColor.colorWithCalibratedRGB(redValue, greenValue, blueValue, alphaValue);
colorWell.setColor(aColor);
}
}