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;
import java.text.DecimalFormat;
public class RGB extends JPanel implements Runnable, MouseListener
{
private Dimension d;
int BOARD_WIDTH=300;
int BOARD_HEIGHT=500;
DecimalFormat format = new DecimalFormat("000");
private Thread animator;
int red = 0;
int green = 0;
int blue = 0;
String hex = "0123456789ABCDEF";
public RGB()
{
addMouseListener(this);
setFocusable(true);
d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
setBackground(Color.black);
if (animator == null ) {
animator = new Thread(this);
animator.start();
}
setDoubleBuffered(true);
}
public void paint(Graphics g){
super.paint(g);
Color c = new Color(red,green,blue);
g.setColor(Color.white);
g.setFont (new Font("Courier", Font.PLAIN, 30));
g.drawString(format.format(red),44,165);
g.drawString(format.format(green),124,165);
g.drawString(format.format(blue),204,165);
//buttons to set color to black or white, saving time
g.setColor(Color.white);
g.drawString("BLK",27,450);
g.drawString("WHT",220,450);
//hex converter, always good, even though java takes hex and RGB
String hexR = hex.substring(red/16,red/16+1) + hex.substring(red%16,red%16+1);
String hexG = hex.substring(green/16,green/16+1) + hex.substring(green%16,green%16+1);
String hexB = hex.substring(blue/16,blue/16+1) + hex.substring(blue%16,blue%16+1);
g.drawString("#" + hexR + hexG + hexB,88,215);
g.setColor(c);
g.fillOval(75,260,150,150); //display
//includes +-5 increments, min/max, and medium value selectors
g.setColor(Color.red);
//r up
int rrupx[] = {62,78,70};
int rrupy[] = {40,40,24};
g.fillPolygon(rrupx,rrupy,3);
int rupx[] = {55,85,70};
int rupy[] = {70,70,40};
g.fillPolygon(rupx,rupy,3);
//r med
g.fillOval(64,69,11,11);
//r down
int rdownx[] = {55,85,70};
int rdowny[] = {80,80,110};
g.fillPolygon(rdownx,rdowny,3);
int rrdownx[] = {62,78,70};
int rrdowny[] = {110,110,126};
g.fillPolygon(rrdownx,rrdowny,3);
g.setColor(Color.green);
//g up
int ggupx[] = {142,158,150};
int ggupy[] = {40,40,24};
g.fillPolygon(ggupx,ggupy,3);
int gupx[] = {135,165,150};
int gupy[] = {70,70,40};
g.fillPolygon(gupx,gupy,3);
//g med
g.fillOval(144,69,11,11);
//g down
int gdownx[] = {135,165,150};
int gdowny[] = {80,80,110};
g.fillPolygon(gdownx,gdowny,3);
int ggdownx[] = {142,158,150};
int ggdowny[] = {110,110,126};
g.fillPolygon(ggdownx,ggdowny,3);
g.setColor(Color.blue);
//b up
int bbupx[] = {222,238,230};
int bbupy[] = {40,40,24};
g.fillPolygon(bbupx,bbupy,3);
int bupx[] = {215,245,230};
int bupy[] = {70,70,40};
g.fillPolygon(bupx,bupy,3);
//b med
g.fillOval(224,69,11,11);
//b down
int bdownx[] = {215,245,230};
int bdowny[] = {80,80,110};
g.fillPolygon(bdownx,bdowny,3);
int bbdownx[] = {222,238,230};
int bbdowny[] = {110,110,126};
g.fillPolygon(bbdownx,bbdowny,3);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
int animationDelay = 50;
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 void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
//+-5 buttons
if(x > 55 && x < 85 && y > 40 && y < 70)
if(red < 255)
red += 5;
if(x > 55 && x < 85 && y > 80 && y < 110)
if(red > 0)
red -= 5;
if(x > 135 && x < 165 && y > 40 && y < 70)
if(green < 255)
green += 5;
if(x > 135 && x < 165 && y > 80 && y < 110)
if(green > 0)
green -= 5;
if(x > 215 && x < 245 && y > 40 && y < 70)
if(blue < 255)
blue += 5;
if(x > 215 && x < 245 && y > 80 && y < 110)
if(blue > 0)
blue -= 5;
//black/white buttons
if(x > 27 && x < 87 && y > 435 && y < 460){
red = 0;
green = 0;
blue = 0;
}
if(x > 220 && x < 280 && y > 435 && y < 460){
red = 255;
green = 255;
blue = 255;
}
//min/max buttons
if(x >= 62 && x <= 78 && y >= 24 && y <= 40)
red = 255;
if(x >= 62 && x <= 78 && y >= 110 && y <= 126)
red = 0;
if(x >= 142 && x <= 158 && y >= 24 && y <= 40)
green = 255;
if(x >= 142 && x <= 158 && y >= 110 && y <= 126)
green = 0;
if(x >= 222 && x <= 238 && y >= 24 && y <= 40)
blue = 255;
if(x >= 222 && x <= 238 && y >= 110 && y <= 126)
blue = 0;
//med buttons
if(x >= 55 && x <= 85 && y >= 70 && y <= 80)
red = 125;
if(x >= 135 && x <= 165 && y >= 70 && y <= 80)
green = 125;
if(x >= 215 && x <= 245 && y >= 70 && y <= 80)
blue = 125;
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
}//end of class
Be the first to 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.