Hello Everyone
Im trying to call my paint method inside my ball class but cant seem to get it to work.Repaint wont call paint nor update
Any help would be greatly appreciated
public class Bounce2 extends Applet implements ActionListener,AdjustmentListener, Runnable {
//Create and initialize objects
private static final long serialVersionUID = 10L;
private Graphics page;
private Ballc ball;
private Thread theThread;
Scrollbar speed;
Scrollbar size;
Button start;
Button shape;
Button tail;
Button clear;
Button quit;
Label speed1;
Label size1;
int MAXSPEED=200;
int MIN=1;
int MINSIZE=30;
int speed2=3;
int currentspeed=5;
int currentsize=50;
//initial height and width of object
int Height=50;
int Width=50;
boolean done=false;
boolean pause=false;
boolean tail2=false;
boolean shape1=true;
int step=1;
public void init(){
//buttons and labels
start=new Button("START");
shape=new Button("SHAPE");
tail=new Button("Tail");
clear=new Button("Clear");
quit=new Button("Quit");
speed1=new Label("Speed");
size1=new Label("Size");
int bWidth=50;
int bHeight=50;
//create gridbaglayout for buttons and scrollbar
GridBagLayout displ=new GridBagLayout();
setBackground(Color.white);// set background color
//set scrollbar settings
speed= new Scrollbar(Scrollbar.HORIZONTAL);
speed.setMaximum(MAXSPEED);
speed.setMinimum(MIN);
speed.setUnitIncrement(step);
speed.setBlockIncrement(step*10);
speed.setValue(currentspeed);
size= new Scrollbar(Scrollbar.HORIZONTAL);
size.setMaximum(MAXSPEED);
size.setMinimum(MINSIZE);
size.setUnitIncrement(step);
size.setBlockIncrement(step*10);
size.setValue(currentsize);
//initilize wights
double colWeight[]={1,1,1,1,1};
double rowWeight[]={1,1};
int colWidth[]={1,1,1,1,1};
int rowHight[]={1,1};
//apply weights
displ.rowHeights=rowHight;
displ.rowWeights=rowWeight;
displ.columnWeights=colWeight;
displ.columnWidths=colWidth;
ball=new Ballc(bWidth,bHeight);//,page);
GridBagConstraints c=new GridBagConstraints();
//using a borderlayout
setLayout(new BorderLayout());
Panel control = new Panel();
Panel sheet =new Panel();
sheet.setVisible(true);
// page= getGraphics();
//use gridbaglayout to place buttons in the panel
control.setLayout(displ);
//Add constraints to objects
c.fill=GridBagConstraints.BOTH;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=0;
c.gridy=1;
displ.setConstraints(speed1,c);
c.fill=GridBagConstraints.BOTH;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=0;
c.gridy=2;
displ.setConstraints(size1,c);
c.fill=GridBagConstraints.BOTH;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=1;
c.gridy=1;
displ.setConstraints(speed,c);
c.fill=GridBagConstraints.BOTH;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=1;
c.gridy=2;
displ.setConstraints(size,c);
c.fill=GridBagConstraints.BOTH;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=3;
c.gridy=1;
displ.setConstraints(tail,c);
c.fill=GridBagConstraints.BOTH;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=2;
c.gridy=1;
displ.setConstraints(start,c);
c.fill=GridBagConstraints.BOTH;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=4;
c.gridy=1;
displ.setConstraints(shape,c);
c.fill=GridBagConstraints.BOTH;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=2;
c.gridy=2;
displ.setConstraints(clear,c);
c.fill=GridBagConstraints.BOTH;
c.weightx=0;
c.weighty=0;
c.gridheight=1;
c.gridwidth=1;
c.gridx=3;
c.gridy=2;
displ.setConstraints(quit,c);
//add our buttons to a panel
control.add(size);
control.add(speed);
control.add(size1);
control.add(speed1);
control.add(start);
control.add(shape);
control.add(clear);
control.add(quit);
control.add(tail);
sheet.add(ball);
//add listeners
start.addActionListener(this);
clear.addActionListener(this);
tail.addActionListener(this);
shape.addActionListener(this);
quit.addActionListener(this);
size.addAdjustmentListener(this);
speed.addAdjustmentListener(this);
//add the panel to the south part of the layout
this.add(control, BorderLayout.SOUTH);
this.add(sheet,BorderLayout.CENTER);
setVisible(true);
}
public void adjustmentValueChanged(AdjustmentEvent e) {
}
public void actionPerformed(ActionEvent e) {
}
public void start()
{
if (theThread == null)
{
System.out.println("start thread");
theThread = new Thread(this);
theThread.start();
}
}
public void run() {
System.out.println("run");
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
while(true)
{
//ball.repaint(); doesnt work as well
ball.fill();
}
}
}
class Ballc extends Canvas
{
Image buffer;
int width;
int height;
Graphics g;
private static final long serialVersionUID = 1L;
public Ballc(int bWidth, int bHeight){//, Graphics page) {
width=bWidth;
height=bHeight;
System.out.println("ball constructed");
}
public void paint(Graphics cg){
System.out.println("paint");
update(cg);
}
public void update(Graphics cg)
{
//
System.out.println("update");
//g.drawOval(30, 30, width, height);
//System.out.println("Works2s");
buffer=createImage(600,400);
g=buffer.getGraphics();
g.setColor(Color.black);
g.drawRect(20,20,50,50);
cg.drawImage(buffer,0,0,this);
}
public void fill(){
validate();
repaint();
}
}