-
Applet - Make a changing color for rectangle
Sorry to bother u all with a silly question...
1. Write an applet which outputs a 4 X 4 red-and-blue grid as shown below.
2. Modify the above applet so that each time it is started (in other words, it receives a start() message), it changes the colour of the grid cells according to the sequence shown in the diagram below:
I cannot upload the desired picture here. Sorry about that.
I'm already finish the first question. The problem is the second one.I post both question so that you all know what the question is.
Here are my coding so far for the second question...you can compile it and as usual use html code to see the output or just use appletviewer...
The problem is I dont know how to make the rectangle change the colour according to the sequence given in question. I have try all possible method like using while or for loop... but it seem not happen exactly what I want
Any ideas or help... I would be apprecited it...thank you so much...
import javax.swing.*;
import java.awt.*;
public class appletGrid2 extends JApplet
{
public Color color1;
public Color color2;
public void init(){
}
public void start(){
//I think i need to make some changes here
// The color mention is the sequence of colour for the rectangle
color1 = Color.blue;
color2 = Color.red;
color1 = Color.green;
color2 = Color.white;
color1 = Color.gray;
color2 = Color.yellow;
color1 = Color.blue;
color2 = Color.black;
}
public void paint(Graphics g){
for(int j=0; j<=120; j+=40){
for(int i=0; i<=120; i+=40){
if(j == i){
g.setColor(color1);
g.fillRect(10+j,10+i,40,40);
}
else if(j==0 && i==80){
g.setColor(color1);
g.fillRect(10+j,10+i,40,40);
}
else if(j==80 && i==0){
g.setColor(color1);
g.fillRect(10+j,10+i,40,40);
}
else if(j==120 && i==40){
g.setColor(color1);
g.fillRect(10+j,10+i,40,40);
}
else if(j==40 && i==120){
g.setColor(color1);
g.fillRect(10+j,10+i,40,40);
}
else{
g.setColor(color2);
g.fillRect(10+j,10+i,40,40);
}
}//end i
}//end j
}//end paint
public void stop(){
}
public void destroy(){
}
}//end class
Similar Threads
-
Replies: 1
Last Post: 03-26-2006, 02:02 PM
-
By crash75uk in forum Java
Replies: 1
Last Post: 04-20-2005, 03:32 PM
-
Replies: 2
Last Post: 04-05-2005, 10:53 PM
-
By tibbs in forum Architecture and Design
Replies: 9
Last Post: 03-01-2002, 02:12 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|