Hi, I'm working on an assignment for school...one of the applets i have to develop is a colour picker...But unfortunately i'm having some trouble with it...
I would just like to ask if anyone could help me along a little bit and give me some insight as what to do to change this into the non-depreciated format...
I've tried to replace this with this code, but i get error after error.. Could anyone help me with this? I'm hitting a brick wall... here is the whole applet if you want to have a look...i would REALLY appreciate it if someone could point me in the right direction at least, im not having much luck...
Here is my (pathetic) effort of trying to recode my program, but it doesnt work at all anymore...
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
publicclass ColourPickerMouse extends Applet implements MouseListener {
Image rgbImageCanvas; //
Image rgbImageBar; //
int pixels[] = newint[0x10000]; //
int pixels2[] = newint[2560]; //
Color viewColor = Color.red; // Default colour at start
int rgbXSelect1; // Selecting bounds for x axis on Canvas
int rgbYSelect1; // Selecting bounds for y axis on Canvas
int rgbYSelect2; // Selecting bounds for y axis on Bar
Label labelSample; // Label for sample colour square
Label labelHex; // Label for Hex value
TextField textFieldHex; // TextField for Hex value
Label labelRGBValue; // Label for RGB value of Blue
Label labelR; // Label for RGB value of Blue
Label labelG; // Label for RGB value of Blue
Label labelB; // Label for RGB value of Blue
TextField textFieldB; // TextField for RGB value of Blue
TextField textFieldR; // TextField for RGB value of Red
TextField textFieldG; // TextField for RGB value of Green
int x,y;
publicvoid init() {
MakeImageCanvas();
MakeImageBar();
setLayout(null);
labelSample = new Label("Selected Colour:");
labelSample.setBounds(312, 12, 100, 10);
add(labelSample);
labelHex = new Label("Hex Value:");
labelHex.setBounds(312, 80, 84, 24);
add(labelHex);
textFieldHex = new TextField();
textFieldHex.setBounds(312, 110, 80, 20);
textFieldHex.setEditable(false);
add(textFieldHex);
labelRGBValue = new Label("RGB Values:");
labelRGBValue.setBounds(312, 150, 84, 24);
add(labelRGBValue);
labelR = new Label("Red:");
labelR.setBounds(312, 178, 36, 20);
add(labelR);
textFieldR = new TextField();
textFieldR.setBounds(360, 178, 45, 20);
textFieldR.setEditable(false);
add(textFieldR);
labelG = new Label("Green:");
labelG.setBounds(312, 202, 40, 20);
add(labelG);
textFieldG = new TextField();
textFieldG.setBounds(360, 202, 45, 20);
textFieldG.setEditable(false);
add(textFieldG);
labelB = new Label("Blue:");
labelB.setBounds(312, 226, 40, 20);
add(labelB);
textFieldB = new TextField();
textFieldB.setBounds(360, 226, 45, 20);
textFieldB.setEditable(false);
add(textFieldB);
addMouseListener(this);
}
publicvoid mouseMoved (MouseEvent e) {}
publicvoid mouseDragged (MouseEvent e) {}
publicvoid mouseReleased (MouseEvent e) {}
publicvoid mouseClicked (MouseEvent e) {}
publicvoid mouseEntered (MouseEvent e) {}
publicvoid mouseExited (MouseEvent e) {}
publicvoid mousePressed (MouseEvent e) {}
publicvoid processMouseEvent(MouseEvent event) {
event.getX();
event.getY();
if (x >= 8 && y >= 8 && x <= 269 && y <= 269) {
rgbXSelect1 = Math.max(0, Math.min(255, x - 10));
rgbYSelect1 = Math.max(0, Math.min(255, y - 10));
Graphics g = getGraphics();
DrawBackground(g);
DrawSelector(g);
} elseif (x >= 275 && y >= 8 && x <= 286 && y <= 263) {
rgbYSelect2 = Math.max(0, Math.min(255, y - 10));
viewColor = new Color(pixels2[rgbYSelect2 * 10]);
MakeImageCanvas();
Graphics g = getGraphics();
DrawBackground(g);
} else {
returnfalse;
}
returntrue;
}
publicvoid paint(Graphics g) {
DrawBackground(g);
DrawSelector(g);
}
publicvoid DrawBackground(Graphics g) { // Displays for the background of applet
// If selection is over Canvas area it is covered
g.setColor(Color.white);
g.fillRect(5, 5, 266, 5);
g.fillRect(5, 5, 5, 266);
g.fillRect(267, 5, 5, 266);
g.fillRect(5, 267, 266, 5);
g.setColor(Color.black);
// Border for Canvas
g.drawRect(10, 10, 257, 257);
// Display the colours for Canvas
g.drawImage(rgbImageCanvas, 11, 11, this);
// Border for Bar
g.drawRect(276, 10, 11, 253);
// Display the colours for Bar
g.drawImage(rgbImageBar, 277, 11, this);
}
publicvoid DrawSelector(Graphics g) { //Displays for Canvas, Bar and GUI
// Selection circles
g.setColor(Color.white);
g.drawOval((10 + rgbXSelect1) - 5, (10 + rgbYSelect1) - 5, 11, 11);
g.setColor(Color.black);
g.drawOval((10 + rgbXSelect1) - 4, (10 + rgbYSelect1) - 4, 9, 9);
// Selector on bar
g.fillRect(274, (10 + rgbYSelect2) - 1, 16, 2);
// Sample colour square
int pickColor = pixels[rgbYSelect1 * 256 + rgbXSelect1] & 0xffffff;
// Draw border
g.drawRect(321, 35, 37, 37);
// Get selected colour and fill square
Color selectedColor = new Color(pickColor);
g.setColor(selectedColor);
g.fillRect(322, 36, 36, 36);
// Textfield information
textFieldHex.setText("#" + Integer.toHexString(pickColor).toUpperCase());
textFieldR.setText(Integer.toString(selectedColor.getRed()));
textFieldG.setText(Integer.toString(selectedColor.getGreen()));
textFieldB.setText(Integer.toString(selectedColor.getBlue()));
}
publicvoid FillArray(int pixels[], int counter, int r, int g, int b, int incR, int incG, int incB) { // Array to print colours
for (int j = 0; j < 256; j += 6) {
r += incR;
g += incG;
b += incB;
for (int i = 0; i < 10; i++) {
pixels[counter++] = 0xff000000 | r << 16 | g << 8 | b;
}
}
}
publicvoid MakeImageCanvas() { // Make the colours for the Canvas from array
int counter = 0;
double incDestR = (double) ( -viewColor.getRed()) / 256D;
double incDestG = (double) ( -viewColor.getGreen()) / 256D;
double incDestB = (double) ( -viewColor.getBlue()) / 256D;
for (int i = 255; i >= 0; i--) {
int destR = Math.min(255, Math.max(0, (int) Math.round((double) viewColor.getRed() + (double) (255 - i) * incDestR)));
int destG = Math.min(255, Math.max(0, (int) Math.round((double) viewColor.getGreen() + (double) (255 - i) * incDestG)));
int destB = Math.min(255, Math.max(0, (int) Math.round((double) viewColor.getBlue() + (double) (255 - i) * incDestB)));
double incR = (double) (destR - i) / 256D;
double incG = (double) (destG - i) / 256D;
double incB = (double) (destB - i) / 256D;
for (int j = 0; j < 256; j++) {
int r = Math.min(255, Math.max(0, (int) Math.floor((double) i + (double) j * incR)));
int g = Math.min(255, Math.max(0, (int) Math.floor((double) i + (double) j * incG)));
int b = Math.min(255, Math.max(0, (int) Math.floor((double) i + (double) j * incB)));
pixels[counter++] = 0xff000000 | r << 16 | g << 8 | b;
}
}
rgbImageCanvas = createImage(new MemoryImageSource(256, 256, pixels, 0, 256));
}
publicvoid MakeImageBar() { // Make the colours for the Bar from array
int counter = 0;
FillArray(pixels2, counter, 255, 0, 0, 0, 6, 0);
FillArray(pixels2, counter += 420, 255, 255, 0, -6, 0, 0);
FillArray(pixels2, counter += 420, 0, 255, 0, 0, 0, 6);
FillArray(pixels2, counter += 420, 0, 255, 255, 0, -6, 0);
FillArray(pixels2, counter += 420, 0, 0, 255, 6, 0, 0);
FillArray(pixels2, counter += 420, 255, 0, 255, 0, 0, -6);
rgbImageBar = createImage(new MemoryImageSource(10, 252, pixels2, 0, 10));
}
}
If anyone wants to look at the original code, feel free to ask!
Thanks for anyone having a look at this, i really REALLY appreciate it!