Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have a school project I am stuck 2

Status
Not open for further replies.

SPrelewicz

Programmer
Jul 16, 2001
124
US
I have a school project I am stuck on. The program is supposed to produce three scroll bars at the bottom of the
screen.
The scroll bars are for red, green, and blue. When you slide the bar for a
color, the color should appear in the center part of the screen.

Compile the program. The scroll bars appear. I can't figure out how to get
the colors to appear as the bar slides. I know the code below needs work.
Here's the code:

//ScrollBarDemo: This program uses scrollbars to select the forground color
// for a label. Three horizontal scrollbars are used for
// selecting red, green, and blue components of the color.
//
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.TitledBorder;

public class ScrollBarDemo extends JFrame implements AdjustmentListener, ItemListener
{
//declare scrollbars
private JScrollBar jscRed;
private JScrollBar jscGreen;
private JScrollBar jscBlue;

//declare labels
private JLabel l1;
private JLabel l2;
private JLabel l3;

//main method
public static void main(String[] args)
{
ScrollBarDemo frame = new ScrollBarDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);
frame.setVisible(true);

//get the dimension of the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;

//get the dimension of the frame
Dimension frameSize = frame.getSize();
int x = (screenWidth - frameSize.width)/2;
int y = (screenHeight - frameSize.height)/2;

//center the frame
frame.setLocation(x,y);
}

//default constructor
public ScrollBarDemo()
{
setTitle("Show Colors");

int redValue=0;
int blueValue=0;
int greenValue=0;

//create panel 1
JPanel p1 = new JPanel();
p1.setSize(500,300);
p1.setLayout(new GridLayout(3,2));
p1.setBackground(Color.lightGray);
p1.setBorder(new TitledBorder("Choose colors"));
l1=new JLabel("Red");
l2=new JLabel("Green");
l3=new JLabel("Blue");


// 0=horizontal,current value,10 is the visible amt, range is 0-255
jscRed = new JScrollBar(0,redValue,10,0,255);
jscGreen = new JScrollBar(0,greenValue,10,0,255);
jscBlue = new JScrollBar(0,blueValue,10,0,255);

p1.add(l1);
p1.add(jscRed);
p1.add(l2);
p1.add(jscGreen);
p1.add(l3);
p1.add(jscBlue);


//create panel 2
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
//p2.setBackground(Color.white);

Color color = new Color(100,0,0);
p2.setBackground(color);


//place p1 and p2 in the frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1,BorderLayout.SOUTH);
getContentPane().add(p2,BorderLayout.CENTER);


//register the listeners for the scrollbars
jscRed.addAdjustmentListener(this);
jscGreen.addAdjustmentListener(this);
jscBlue.addAdjustmentListener(this);


/*
//register the listeners for the scrollbars
jscRed.addItemListener(this);
jscGreen.addItemListener(this);
jscBlue.addItemListener(this);
*/

}

//handle scrollbar adjustment actions
public void adjustmentValueChanged(AdjustmentEvent e)
{
int rvalue=jscRed.getValue();
System.out.println(rvalue);
int gvalue=jscGreen.getValue();
System.out.println(gvalue);
int bvalue=jscBlue.getValue();
System.out.println(bvalue);

Color color = new Color(rvalue, gvalue, bvalue);

}


//handle scrollbar events
public void itemStateChanged(ItemEvent e)
{
}


}

---------------------------------------------------------
I have a panel called p2. I was thinking I could do something like this:
p2.setBackground(color);
Well, it complains about missing symbol p2.
I know it's asking alot for nothing, but Any help on this will be greatly appreciated.


 
ask your teacher, that's what they're there for. Their job is to teach you how to figure out computer problems, not to teach you the specific solutions to a few problems. if we were to just give you the answer, it wouldn't help you in the long run. we could take the time to walk through it with you, so that we were sure that you understood how to get at the answer to problems like this, but that's really not what this forum is for.

good luck. "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Where are you trying to access p2? If it is not in the Constructor then it is going to thrown a compilation error because p1 and p2 are declared as local variables to the constructor not as instance variables of the class.

Make sense?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top