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();
}
}

You have an infinite loop in a MAX_PRIORITY thread (line 215), so it's not surprising that nothing else gets done.

What exactly are you trying to achieve?

I'm trying to draw objects inside the sheet area which is in the center of my frame.I know there is an infinite loop but it wont even call my paint method even once thats where I am confused.
I am just trying to test to see how things work before adding the rest of the code

Your calls to invalidate. repaint etc tell Swing that the GUI needs to be repainted. That adds an item to Swing's event queue.
A separate thread - Swing's event dispatch thread (EDT) - takes items one at a time from the queue and actions them, in this case by calling paint. Your infinite loop is running in a thread with a higher priority than the EDT, so it's anybody's guess when or whether the EDT will ever get to do anything.

What did you intend with that loop?

The infinite loop will contain the my repaint and move(update ball location) functions.I'm also going to add a thread.sleep in there.I'm a little confused about the queue can you give me an example of how I can make everything in the correct order?

Swing's event queue is inaccessible to you. Just call repaint() and trust it.
For updating positions use a java.util.Timer to call your update method at regular intervals, eg 33 miliiseconds. As the last thing in that method call repaint() and Swing will call your paint(Graphics g) as required.

First thank you for your replies and time.
I am still not able to get anything on the center of my frame.

public void run() {
System.out.println("run");
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

while(!pause){
ball.repaint();
ball.move();

try {
    //time delay 
    Thread.sleep(1000 / currentspeed);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}
}
}

We are required to use Thread.sleep for the waiting time.
Have I added the ball into the center correctly?
I'm sorry for all my questions but i'm still new to java and tried many things with no luck.I just need the center of the frame to be drawable and then I can pick up very fast from there

You call repaint on the GUI conponent that needs to be updated. That component's paintComponent method is where the ball gets apinted.

So call repaint from sheet panel?Tried that with no luck

OK, here's a very simple but completely runnable example that shows how to update an animated simulation at a steady speed and update the GUI to match. The important parts are the last two methods. YOUou can use a simple java,util.Timer instead of the ScheduledThreadPoolExecutor if you like.
If you are required to use Thread.sleep then use that instead of the Timer, but don't expect reliable results.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import static java.util.concurrent.TimeUnit.*;
import javax.swing.*;

public class Animation0 extends JPanel {
    // absolutely minimal example of how to structure animation in Swing.
    // updateSimulation steps the simulation model through time
    // paintComponent updates the screen with the latest data

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Animation0::new);
    }

    // this is the "model" - just a single object moving at constant speed
    int xPos = 0, yPos = 35;    // current position of animated object
    int xSpeed = 1, ySpeed = 0; // current speed

    Animation0() {
        // create and display the animation in a JFrame (boring)
        final JFrame frame = new JFrame("Animation 0 (close window to exit)");
        setPreferredSize(new Dimension(400, 150));
        frame.add(this);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        // start scheduled executor to update the model every 30 milliseconds...
        new ScheduledThreadPoolExecutor(1).
                scheduleAtFixedRate(this::updateSimulation, 0, 30, MILLISECONDS);
    }

    public void updateSimulation() {
        // reliably called by executor every 30 milliseconds
        xPos += xSpeed; // update position of object
        yPos += ySpeed;
        repaint(); // notify that screen refresh is now needed
    }

    @Override
    public void paintComponent(Graphics g) { 
        // screen refresh - called by Swing as needed
        // Never update positions etc here because there are no
        // guarantees about when or how many times this will be called.
        super.paintComponent(g); // ensure background etc is painted
        g.fillOval(xPos, yPos, 100, 100); // draw object at current position
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.