Java by Example - loops, advanced color functions
Here it is demonstrated how to use loops for a repeating task, here in order to draw graphics primitives several times.
For our loop we use the "for-loop" - the syntax is:
for (initialization; test-expression; update-expression)
body
Just study the code and you will see what is meant. Of the three loop variants (for, while, do-while) this is the one you will need most and it's the shortest and most elegant. The variable for the incrementation (in most cases by convention called i / j / k - or x / y, if you need it for coordinates) can be defined inside the head of the loop, if you need it only once. If you need it several times inside the same method, you should define a local variable in your method: int i;.
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project13 extends Applet
{
public void paint (Graphics g)
{
int x;
int rightBorder=220;
//paints a row of red Squares
g.setColor(Color.red);
for(x=0;x<rightBorder;x+=20)
g.fillRect(x,20,18,18);
//many (hollow) blue Circles
g.setColor(Color.blue);
for(x=0;x<rightBorder;x+=30)
g.drawOval(x,80,28,28);
//lots of black lines
g.setColor(Color.black);
for(x=0;x<rightBorder;x+=5)
g.drawLine(x,140,x,280);
//a vertical row of yellow squares with round corners
g.setColor(Color.yellow);
for(int y=20;y<280;y+=30)
g.fillRoundRect(250,y,40,25,15,15);
}
}
|
Here we use two nested loops to draw an array of graphical elements. The outer loop is for the horizontal row, the inner loop for the vertical. The fill3DRect and draw3DRect functions enable you to easily draw rectangles with a raised or lowered impression, depending on the last boolean parameter.
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project14 extends Applet
{
public void paint (Graphics g)
{
//draw tiles with circles inside
//we use two nested loops to do this:
for(int x=0;x<300;x+=30)
for(int y=0;y<300;y+=30)
{
g.setColor(Color.lightGray);
g.fill3DRect(x,y,30,30,true);
g.setColor(Color.red);
g.drawOval(x+2,y+2,25,25);
g.setColor(Color.lightGray);
g.draw3DRect(x+7,y+7,15,15,false);
}
}
}
|
This example demonstrates some of the more advanced graphics function of Java, showing how easy it is to use them. The five filled circles use user-defined colors, four of them are global variables (accessible for every function of the class), and the first circle uses a new color object that is defined locally right inside the function: g.setColor(new Color(255,255,0));. If you need a color only once, this is the shorter way, but if you need it more than once, you should define a variable for it. If it is used only in one function, you can define it locally in that function, otherwise globally. The pink blocks in the row at the bottom show the ease of making a darker or lighter variant of a given color with brighter() and darker(). The gradients are not just as easy to achieve, just copy and modify the code if you ever need something like it.
//Sourcecode
import java.awt.*;
import java.applet.*;
public class Project15 extends Applet
{
Color myGreen = new Color(110,220,160);
Color myRed = new Color(255,90,100);
Color myYellow = new Color(240,240,180);
Color myBlue = new Color(140,190,250);
public void paint (Graphics g)
{
//paint background black
g.setColor(Color.black);
g.fillRect(0,0,size().width,size().height);
g.setColor(new Color(255,255,0));
g.fillOval(10,10,50,50);
g.setColor(myGreen);
g.fillOval(65,10,50,50);
g.setColor(myRed);
g.fillOval(120,10,50,50);
g.setColor(myYellow);
g.fillOval(175,10,50,50);
g.setColor(myBlue);
g.fillOval(230,10,50,50);
float color;
int i;
int WIDTH=300;
//the three color gradients: red, green, blue
for(i=0; i<WIDTH; i++)
{
color=i*(float)255/WIDTH;
g.setColor(new Color((int)color,0,0));
g.drawLine(i,70,i,90);
g.setColor(new Color(0,(int)color,0));
g.drawLine(i,95,i,115);
g.setColor(new Color(0,0,(int)color));
g.drawLine(i,120,i,140);
}
//the rainbow color spectrum
for(i=0; i<WIDTH; i++)
{
color=(float)i/WIDTH;
g.setColor(Color.getHSBColor(color,1.0f,1.0f));
g.drawLine(i,150,i,220);
}
Color myPink = new Color(240,100,150);
g.setColor(myPink.darker().darker());
g.fillRect(15,240,45,40);
g.setColor(myPink.darker());
g.fillRect(70,240,45,40);
g.setColor(myPink);
g.fillRect(125,240,45,40);
g.setColor(myPink.brighter());
g.fillRect(180,240,45,40);
g.setColor(myPink.brighter().brighter());
g.fillRect(235,240,45,40);
}
}
|
|