| | |
scrollbar applet
![]() |
•
•
Join Date: Jul 2005
Posts: 2
Reputation:
Solved Threads: 0
Hi folks, I have to answer the following question:
"Write an applet which contains a panel whose background colour varies as the elevators of three Scrollbar objects are varied. Each Scrollbar object affects one colour constituent."
I think I'm pretty close, but I can't figure out why only one of the scrollbars changes colour. They should change red, green, and blue respectively.
I'd be really grateful for any help on this one. Thanks!
/*applet which demonstrates the use of a canvas*/
import java.awt.*;
public class u extends java.applet.Applet
{
Canvas can;
Scrollbar s;
Color bg;
int b;
int g;
int r;
public void init()
{
setLayout(new GridLayout(1, 2, 10, 10));
bg = new Color(0, 0, 255);
can = new Canvas();
s = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(can);
add(s);
bg = new Color(0, 255, 0);
s = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(s);
bg = new Color(255, 0, 0);
s = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(s);
}
public boolean handleEvent(Event evt)
{
if (evt.target instanceof Scrollbar)
{
r = s.getValue();
g = s.getValue();
b = s.getValue();
bg = new Color(r, g, b);
can.setBackground(bg);
can.repaint();
}
return true;
}
}
"Write an applet which contains a panel whose background colour varies as the elevators of three Scrollbar objects are varied. Each Scrollbar object affects one colour constituent."
I think I'm pretty close, but I can't figure out why only one of the scrollbars changes colour. They should change red, green, and blue respectively.
I'd be really grateful for any help on this one. Thanks!
/*applet which demonstrates the use of a canvas*/
import java.awt.*;
public class u extends java.applet.Applet
{
Canvas can;
Scrollbar s;
Color bg;
int b;
int g;
int r;
public void init()
{
setLayout(new GridLayout(1, 2, 10, 10));
bg = new Color(0, 0, 255);
can = new Canvas();
s = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(can);
add(s);
bg = new Color(0, 255, 0);
s = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(s);
bg = new Color(255, 0, 0);
s = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(s);
}
public boolean handleEvent(Event evt)
{
if (evt.target instanceof Scrollbar)
{
r = s.getValue();
g = s.getValue();
b = s.getValue();
bg = new Color(r, g, b);
can.setBackground(bg);
can.repaint();
}
return true;
}
}
hi bigriggers,
as server crash had mentioned u have only one instance.
I have attached a running version. Hope this is what you are looking for:
I have made this into a awt standalone application
import java.awt.*;
import java.awt.event.*;
public class u extends Frame
{
Canvas can;
Scrollbar s1,s2,s3;
Color bg;
int b;
int g;
int r;
public u()
{
setLayout(new GridLayout(1, 2, 10, 10));
bg = new Color(0, 0, 255);
can = new Canvas();
s1 = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(can);
add(s1);
bg = new Color(0, 255, 0);
s2 = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(s2);
bg = new Color(255, 0, 0);
s3 = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(s3);
setSize(300,300);
setVisible(true);
}
public boolean handleEvent(Event evt)
{
if (evt.target instanceof Scrollbar)
{
r = s1.getValue();
g = s2.getValue();
b = s3.getValue();
bg = new Color(r, g, b);
can.setBackground(bg);
can.repaint();
}
return true;
}
public static void main(String args[])
{
new u();
}
}
as server crash had mentioned u have only one instance.
I have attached a running version. Hope this is what you are looking for:
I have made this into a awt standalone application
import java.awt.*;
import java.awt.event.*;
public class u extends Frame
{
Canvas can;
Scrollbar s1,s2,s3;
Color bg;
int b;
int g;
int r;
public u()
{
setLayout(new GridLayout(1, 2, 10, 10));
bg = new Color(0, 0, 255);
can = new Canvas();
s1 = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(can);
add(s1);
bg = new Color(0, 255, 0);
s2 = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(s2);
bg = new Color(255, 0, 0);
s3 = new Scrollbar(Scrollbar.VERTICAL, 0, 0, 0, 255);
can.setBackground(bg);
add(s3);
setSize(300,300);
setVisible(true);
}
public boolean handleEvent(Event evt)
{
if (evt.target instanceof Scrollbar)
{
r = s1.getValue();
g = s2.getValue();
b = s3.getValue();
bg = new Color(r, g, b);
can.setBackground(bg);
can.repaint();
}
return true;
}
public static void main(String args[])
{
new u();
}
}
![]() |
Similar Threads
- Applet size (Java)
- Java Applet [Move Image] (Java)
- help!! My applet can't run... Urgent!! (Java)
- applet crashed (Windows NT / 2000 / XP)
- Applet or Application? (Java)
Other Threads in the Java Forum
- Previous Thread: "Cannot Resolve Symbol"
- Next Thread: Reading Mail
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class client code compile compiler component converter database developmenthelp eclipse error fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html ide image input int integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying number online problem program programming project qt recursion scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






