it-artikel:java:my-first-real-java-project-flashysquare-v.1.0
Differences
This shows you the differences between two versions of the page.
— | it-artikel:java:my-first-real-java-project-flashysquare-v.1.0 [2022-08-31 12:30] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== My first real Java project - flashySquare V.1.0 ====== | ||
+ | |||
+ | This was my first self initiated Java Project from the scratch for training purposes. It requires the acm library which you can download from http:// | ||
+ | |||
+ | SPECIAL THANKS to Adrian Tassoni and the Clinical Trials Unit at the University Medical Center Freiburg who supported me. | ||
+ | |||
+ | Here's the Code for all to test and learn. Free to use. Just gimme credit and link to my website if you use it somewhere. | ||
+ | |||
+ | <code java flashySquare.java> | ||
+ | import java.awt.*; | ||
+ | |||
+ | import acm.graphics.*; | ||
+ | import acm.program.*; | ||
+ | import acm.util.*; | ||
+ | |||
+ | |||
+ | /** | ||
+ | * flashySquare V. 1.0 | ||
+ | | ||
+ | * @author Axel Werner [mail@awerner.myhome-server.de] in 2011-03-15 | ||
+ | | ||
+ | * Fades an graphics rectangle in random colors. | ||
+ | * Simulating/ | ||
+ | | ||
+ | * This was my first self initiated Java Project | ||
+ | * from the scratch for training purposes. | ||
+ | | ||
+ | * SPECIAL THANKS to Adrian Tassoni and the Clinical Trials Unit | ||
+ | * at the University Medical Center Freiburg who supported me | ||
+ | | ||
+ | * Requires the acm library. You can download it from http:// | ||
+ | | ||
+ | */ | ||
+ | public class flashySquare extends GraphicsProgram { | ||
+ | |||
+ | private static int canvasWidth; | ||
+ | private static int canvasHeight; | ||
+ | |||
+ | public static void main() { | ||
+ | flashySquare flashySquare = new flashySquare(); | ||
+ | flashySquare.run(); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | /** | ||
+ | * Main program | ||
+ | */ | ||
+ | public void run() { | ||
+ | |||
+ | getCanvasSize(); | ||
+ | |||
+ | GRect square = new GRect( canvasWidth/ | ||
+ | canvasHeight/ | ||
+ | canvasWidth/ | ||
+ | canvasHeight/ | ||
+ | ); | ||
+ | square.setFilled(true); | ||
+ | add(square); | ||
+ | |||
+ | Color lastColor=Color.BLACK; | ||
+ | Color nextColor=Color.BLACK; | ||
+ | |||
+ | while(true){ | ||
+ | lastColor = nextColor; | ||
+ | nextColor = getRandomColor(); | ||
+ | fadeColor( square, lastColor, nextColor, 128 , 20 ); | ||
+ | pause(1000); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * This Method fades a GSquare Object from one Color to another in < | ||
+ | * uses < | ||
+ | * | ||
+ | * @param square Object of type GRect | ||
+ | * @param lastColor Starting fill Color of the GRect Object | ||
+ | * @param nextColor Finish fill Color of the GRect Object | ||
+ | * @param MAXSTEPS Number of Steps (resolution) to get from Start to Finish Color | ||
+ | * @param delay in ms to delay between each step. | ||
+ | */ | ||
+ | private void fadeColor( GRect square, Color lastColor, Color nextColor, final int MAXSTEPS, int delay ) { | ||
+ | |||
+ | int lastColorR=lastColor.getRed(); | ||
+ | int lastColorG=lastColor.getGreen(); | ||
+ | int lastColorB=lastColor.getBlue(); | ||
+ | |||
+ | int nextColorR=nextColor.getRed(); | ||
+ | int nextColorG=nextColor.getGreen(); | ||
+ | int nextColorB=nextColor.getBlue(); | ||
+ | |||
+ | int tempColorR=lastColorR; | ||
+ | int tempColorG=lastColorG; | ||
+ | int tempColorB=lastColorB; | ||
+ | |||
+ | Color tempColor=Color.BLACK; | ||
+ | |||
+ | double stepWidthR = getStepWidth(lastColorR, | ||
+ | double stepWidthG = getStepWidth(lastColorG, | ||
+ | double stepWidthB = getStepWidth(lastColorB, | ||
+ | |||
+ | println(" | ||
+ | println(" | ||
+ | println(" | ||
+ | println(" | ||
+ | println(" | ||
+ | println(" | ||
+ | println(" | ||
+ | |||
+ | |||
+ | println(" | ||
+ | for(int step=1; step< | ||
+ | tempColorR = (int) ((lastColorR + step*stepWidthR)+0.5); | ||
+ | tempColorG = (int) ((lastColorG + step*stepWidthG)+0.5); | ||
+ | tempColorB = (int) ((lastColorB + step*stepWidthB)+0.5); | ||
+ | /* | ||
+ | * HINT: Adding 0.5 to a double and then casting to int is one way to ROUND a double | ||
+ | * to a full number (integer). Because casting a double to an int drops everything | ||
+ | * after the decimal point completly. So for example: casting a " | ||
+ | * casting a 5.4 becomes a " | ||
+ | * drops anything after the decimal point. | ||
+ | * | ||
+ | * So by adding 0.5 to 2.9 it becomes 3.4, casting to int becomes " | ||
+ | * wanted here. Converting a floating point (double) Number into an integer number with | ||
+ | * rounding. | ||
+ | */ | ||
+ | |||
+ | println(" | ||
+ | |||
+ | tempColor = new Color(tempColorR, | ||
+ | square.setFillColor(tempColor); | ||
+ | pause(delay); | ||
+ | |||
+ | } | ||
+ | println(" | ||
+ | |||
+ | // square.setFillColor(nextColor); | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * This Method calculates the required width of a single step to | ||
+ | * get from one starting RGB Value (0-255) to the fiinishing value in | ||
+ | * the desired amount of steps (resolution). | ||
+ | | ||
+ | * @param lastVal Starting value | ||
+ | * @param nextVal End/ | ||
+ | * @param STEPS amount of desired steps to reach the finish (resolution) | ||
+ | * @return required stepWidth as double | ||
+ | */ | ||
+ | private double getStepWidth(int lastVal, int nextVal, final int STEPS) { | ||
+ | |||
+ | double diff = nextVal - lastVal; | ||
+ | double stepWidth = (diff / STEPS); | ||
+ | return stepWidth; | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * Simple random Color generator. | ||
+ | * @return Returns a random Color object | ||
+ | */ | ||
+ | public Color getRandomColor() { | ||
+ | Color newColor; | ||
+ | RandomGenerator randGen = new RandomGenerator(); | ||
+ | newColor = randGen.nextColor(); | ||
+ | return newColor; | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * Gets dimensions of the given canvas and then loads the static Class Variables | ||
+ | */ | ||
+ | private void getCanvasSize() { | ||
+ | canvasWidth=getWidth(); | ||
+ | canvasHeight=getHeight(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | --- // | ||
+ | |||
+ | |||
+ | {{tag> | ||