I am trying to update the color of a circle when the user move sliders using java awt standalone (homework).I have to use a Frame. I can see that my values update form the slider but I can get those values to the inner class circle in the paint method to be repainted and update the color of the circle(the objective) thanks for any help!! this is my code so far:
package cs111b;
import java.awt.*;
import java.awt.event.*;
public class ColorFactory extends Frame implements ActionListener, AdjustmentListener
{
private static final long serialVersionUID = 1L;
static Dimension t = Toolkit.getDefaultToolkit().getScreenSize();
Panel pS,cP,rB;
Scrollbar rScroll, gScroll, bScroll;
Checkbox hex, dec, oct,bin;
int[] Red = new int[4];
int[] Green = new int[4];
int[] Blue = new int[4];
int[] rgb = new int[3];
Color cl = new Color(rgb[0],rgb[1],rgb[2]);
Dimension Cr = new Dimension((int)(t.width*.70),(int)(t.height*.60));
int w = 300;
int x = (Cr.width/2)-(w/2);
int y = (Cr.height/2)-(w/2);
class Circle extends Canvas
{
private static final long serialVersionUID = 1L;
Dimension Cr = new Dimension((int)(t.width*.70),(int)(t.height*.60));
int w = 300;
int x = (Cr.width/2)-(w/2);
int y = (Cr.height/2)-(w/2);
//Color c;// = new Color(rgb[0],rgb[1],rgb[2]);
public Circle(int[] c)
{
cl = new Color(rgb[0],rgb[1],rgb[2]);
setSize(Cr);
}
@Override
public void paint(Graphics g)
{
super.paint(g);
//c = new Color(rgb[0],rgb[1],rgb[2]);
g.setColor(cl);
g.fillOval(x, y, w, w);
}
}
class Rect extends Canvas
{
private static final long serialVersionUID = 1L;
public Rect()
{
}
public Rect(int x,int y,int w, int h)
{
Red[0] = x;
Red[1] = y;
Red[2] = w;
Red[3] = h;
}
@Override
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.WHITE);
g.fillRect(Red[0], Red[1], Red[2], Red[3]);
g.setColor(Color.GREEN);
g.fillRect(Green[0], Green[1], Green[2], Green[3]);
g.setColor(Color.BLUE);
g.fillRect(Blue[0], Blue[1], Blue[2], Blue[3]);
}
}
public ColorFactory()
{
addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setSize(t);
setTitle("Color Factory");
setLayout(new FlowLayout());
//*****************Creating Label
Label l = new Label("Color Factory");
l.setAlignment(Label.CENTER);
l.setPreferredSize(new Dimension(t.width,(int)(t.height*.1)));
l.setFont(new Font("Seriff",Font.BOLD,35));
l.setForeground(Color.ORANGE);
add(l);
//******************Creating Scrolls
rScroll = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255);
gScroll = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255);
bScroll = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255);
rScroll.setPreferredSize(new Dimension(255,20));
gScroll.setPreferredSize(new Dimension(255,20));
bScroll.setPreferredSize(new Dimension(255,20));
//******************Creating Panel
pS = new Panel();
pS.setPreferredSize(new Dimension(t.width, (int)(t.height*.10)));
pS.setBackground(Color.BLUE);
//rScroll.setPreferredSize(new Dimension((int)(this.getWidth()*.2),(int)(this.getHeight()*.02)));
rScroll.setBackground(Color.WHITE);
gScroll.setBackground(Color.WHITE);
bScroll.setBackground(Color.WHITE);
rgb[0]= rScroll.getValue();
rgb[1]= gScroll.getValue();
rgb[2]= bScroll.getValue();
pS.add(rScroll);
pS.add(gScroll);
pS.add(bScroll);
add(pS);
//Creating Circle
cP = new Panel();
cP.setBackground(Color.BLUE);
cP.setPreferredSize(new Dimension((int)(t.width*.70), (int)(t.height*.60)));
Circle C = new Circle(rgb);
C.setBackground(Color.DARK_GRAY);
//C.setForeground(Color.cyan);
C.setPreferredSize(new Dimension((int)(t.width*.70), (int)(t.height*.60)));
cP.add(C);
add(cP);
//creating radio buttons panel
rB = new Panel();
rB.setPreferredSize(new Dimension((int)(t.width*.27), (int)(t.height*.60)));
rB.setBackground(Color.RED);
//Creating CheckBoxes
CheckboxGroup cg = new CheckboxGroup();
hex = new Checkbox("Hex",cg,true);
dec = new Checkbox("Dec",cg,true);
oct = new Checkbox("Oct",cg,true);
bin = new Checkbox("Bin",cg,true);
rB.add(hex);
rB.add(dec);
rB.add(oct);
rB.add(bin);
//creating Rectangles
Red[0] = (int)(rB.getWidth()*.20);Red[1]= (int)(rB.getHeight()*8);Red[2] = 20; Red[3]= rScroll.getValue();
Green[0] = (int)(rB.getWidth()*.20)+40;Green[1]= (int)(rB.getHeight()*8);Green[2] = 20; Red[3]= rScroll.getValue();
Blue[0] = (int)(rB.getWidth()*.20)+80;Blue[1]= (int)(rB.getHeight()*8);Blue[2] = 20; Blue[3]= rScroll.getValue();
Rect r = new Rect();
r.setSize(this.getWidth(), this.getHeight());
rB.add(r);
add(rB);
rScroll.addAdjustmentListener(this);
gScroll.addAdjustmentListener(this);
bScroll.addAdjustmentListener(this);
setVisible(true);
}
/*
public void paint(Graphics g)
{
super.paint(g);
//g.setXORMode(cl);
g.setColor(cl);
g.fillOval(x, y, w, w);
}
*/
public static void main(String[] args)
{
new ColorFactory();
}
@Override
public void actionPerformed(ActionEvent e)
{
}
@Override
public void adjustmentValueChanged(AdjustmentEvent e)
{
if(rScroll.getValueIsAdjusting())
{
Red[3]= e.getValue();
rgb[0]= e.getValue();
cP.setForeground(new Color(rgb[0],rgb[1],rgb[2]));
cP.repaint();
}
if(gScroll.getValueIsAdjusting())
{
rgb[1] = e.getValue();
cP.repaint();
}
if(bScroll.getValueIsAdjusting())
{
rgb[2] = e.getValue();
cP.repaint();
}
rB.repaint();
}
}