SPrelewicz
Programmer
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.
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.