/*
* MY ADDED FEATURE:
* I added circles next to each button that would show the potential colors.
* The little circle displays the color that would be displayed if you pressed the button.
* For example, the circle next to the "red + 5" button would display the current color with a red value raised by 5
* The bigger circle displays the current color with the respective color + 50. This is to show the color that would be displayed if you repeatedly hit that button.
* For example, next to the "red + 5" button the bigger circle shows the current color with a red value raised by 50. If you repeatedly pressed that red button (10x to be exact), you would see that color.
*/
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
import java.util.Scanner;
public class BoardColor extends JPanel implements Runnable, MouseListener
{
private Dimension d;
int BOARD_WIDTH=500;
int BOARD_HEIGHT=500;
int x = 0;
private Thread animator;
//RGB Values ******** STEP 1: no need to code here, just be aware of variables to use
int red = 0;
int green = 0;
int blue = 0;
public BoardColor()
{
addMouseListener(this);
setFocusable(true);
d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
if (animator == null ) {
animator = new Thread(this);
animator.start();
}
setDoubleBuffered(true);
}
public void paint(Graphics g){
super.paint(g);
//Creates window
g.setColor(Color.white);
g.fillRect(0, 0, d.width, d.height);
//Initializes color, converts it to hex, displays info to screen
Color c = new Color(red,green,blue);
String hexString = convertHexColor(c);
g.setColor(Color.black);
g.drawString(" R: " + red + " G: " + green + " B: " + blue + " Hex: " + hexString,10, d.height-60);
//Adding potential colors (P indicates plus 5, M indicates minus 5, PP indicates plus 25, MM indicates minus 25)
//RED
Color cPr = c;
if (red <= 250)
cPr = new Color(red + 5, green, blue);
Color cPPr = c;
if (red <= 205)
cPPr = new Color(red + 50, green, blue);
Color cMr = c;
if (red >= 5)
cMr = new Color(red - 5, green, blue);
Color cMMr = c;
if (red >= 50)
cMr = new Color(red - 50, green, blue);
//GREEN
Color cPg = c;
if (green <= 250)
cPg = new Color(red, green + 5, blue);
Color cPPg = c;
if (green <= 205)
cPPg = new Color(red, green + 50, blue);
Color cMg = c;
if (green >= 5)
cMg = new Color(red, green - 5, blue);
Color cMMg = c;
if (green >= 50)
cMMg = new Color(red, green - 50, blue);
//BLUE
Color cPb = c;
if (blue <= 250)
cPb = new Color(red, green, blue + 5);
Color cPPb = c;
if (blue <= 205)
cPPb = new Color(red, green, blue + 50);
Color cMb = c;
if (blue >= 5)
cMb = new Color(red, green, blue - 5);
Color cMMb = c;
if (blue >= 50)
cMMb = new Color(red, green, blue - 50);
//Big Color Oval
g.setColor(c);
g.fillOval(60,230, 75, 75);
//Plus red oval and potential color ovals - smaller one is +5, bigger one is +50
g.setColor(Color.red);
g.fillOval(10,10,20,20);
g.setColor(cPr);
g.fillOval(37,15,15,15);
g.setColor(cPPr);
g.fillOval(60,10,20,20);
//Minus red oval and potential color ovals - smaller one is -5, bigger one is -50
g.setColor(Color.red);
g.fillOval(10,50,20,20);
g.setColor(cMr);
g.fillOval(37,55,15,15);
g.setColor(cMMr);
g.fillOval(60,50,20,20);
//Plus green oval and potential color ovals - smaller one is +5, bigger one is +50
g.setColor(Color.green);
g.fillOval(100,10,20,20);
g.setColor(cPg);
g.fillOval(127,15,15,15);
g.setColor(cPPg);
g.fillOval(150,10,20,20);
//Minus green oval and potential color ovals - smaller one is -5, bigger one is -50
g.setColor(Color.green);
g.fillOval(100,50, 20, 20);
g.setColor(cMg);
g.fillOval(127,55,15,15);
g.setColor(cMMr);
g.fillOval(150,50,20,20);
//Plus blue oval and potential color ovals - smaller one is +5, bigger one is +50
g.setColor(Color.blue);
g.fillOval(200,10,20,20);
g.setColor(cPb);
g.fillOval(227,15,15,15);
g.setColor(cPPb);
g.fillOval(250,10,20,20);
//Minus blue oval and potential color ovals - smaller one is -5, bigger one is -50
g.setColor(Color.blue);
g.fillOval(200,50,20,20);
g.setColor(cMb);
g.fillOval(227,55,15,15);
g.setColor(cMMb);
g.fillOval(250,50,20,20);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
int animationDelay = 500;
long time =
System.currentTimeMillis();
while (true) {//infinite loop
// spriteManager.update();
repaint();
try {
time += animationDelay;
Thread.sleep(Math.max(0,time -
System.currentTimeMillis()));
}catch (InterruptedException e) {
System.out.println(e);
}//end catch
}//end while loop
}//end of run
public String convertHexColor(Color cH) {
int red = cH.getRed();
int green = cH.getGreen();
int blue = cH.getBlue();
String hex = "#";
hex += convertHexNum(red);
hex += convertHexNum(green);
hex += convertHexNum(blue);
return hex;
}
public String convertHexNum(int num) {
String[] chars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
int result = num/16;
String character = chars[result];
String character2 = chars[(num - (result * 16))];
return character + character2;
}
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
//STEP 2: use example of RED and apply this to Green and Blue
if(x > 10 && x < 30 && y > 10 && y < 30)
if (red <= 250)
red += 5;
if(x > 10 && x < 30 && y > 50 && y < 70)
if (red >= 5)
red -= 5;
if (x > 100 && x < 120 && y > 10 && y < 30)
if (green <= 250)
green += 5;
if(x > 100 && x < 120 && y > 50 && y < 70)
if (green >= 5)
green -= 5;
if (x > 200 && x < 220 && y > 10 && y < 30)
if (blue <= 250)
blue += 5;
if(x > 200 && x < 220 && y > 50 && y < 70)
if (blue >= 5)
blue -= 5;
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
}//end of class
1 Response
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.