ColorChanger

import javax.swing.JFrame; public class StarterColor extends JFrame { public StarterColor() { add(new BoardColor()); setTitle("Board"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500,500); setLocationRelativeTo(null); setVisible(true); setResizable(false); } public static void main(String[] args) { new StarterColor(); } } /** * Can change the shape of the object, which is very useful, because someone might want to see what the color looks like in other shapes * @Charlie Barton * @1.1 */ 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; boolean shape = true; 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); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height); g.setColor(Color.black); g.drawString(" R: " + red + " G: " + green + " B: " + blue,10, d.height-60); g.drawString("Press Button to Change Shape", 30,100); Color c = new Color(red,green,blue); g.setColor(c); if(shape){ g.fillOval(60,230, 75, 75); }else{ g.fillRect(60,230, 75, 75); } g.setColor(Color.red); g.fillOval(10,10, 20, 20); g.setColor(Color.red); g.fillOval(10,50, 20, 20); g.setColor(Color.green); g.fillOval(100,10, 20, 20); g.setColor(Color.green); g.fillOval(100,50, 20, 20); g.setColor(Color.blue); g.fillOval(200,10, 20, 20); g.setColor(Color.blue); g.fillOval(200,50, 20, 20); g.setColor(Color.blue); g.fillOval(200,10, 20, 20); g.setColor(Color.blue); g.fillOval(200,50, 20, 20); g.setColor(Color.black); g.fillOval(100,115, 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 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 <255) red += 5; } if(x > 10 && x < 30 && y > 50 && y < 70){ if(red > 0) red -= 5; } if(x > 110 && x < 130 && y > 10 && y < 30){ if(red < 255) green += 5; } if(x > 110 && x < 130 && y > 50 && y < 70){ if(red > 0) green -= 5; } if(x > 210 && x < 230 && y > 10 && y < 30){ if(blue < 255) blue += 5; } if(x > 210 && x < 230 && y > 50 && y < 70){ if( blue > 0) blue -= 5; } if(x > 100 && x < 120 && y > 115 && y < 135){ if(shape){ shape = false; }else{ shape = true; } } } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } }

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.