Question

Hello i was studying about 2d graphics and wanted to make something like this http://i60.tinypic.com/71tm37.png

my code for the frame and declaration of the Jpanel is this :

public class Animation extends JPanel {
private ArrayList<BouncingCircle> circles;
Animation() {
this.setSize(320, 240);
this.setOpaque(true);
this.setBackground(new java.awt.Color(102, 255, 102));      
circles = new ArrayList<BouncingCircle>();
}

public void paint(Graphics g) {
    Image dbImg = createImage(getWidth(), getHeight());
    Graphics dbg = dbImg.getGraphics();
    draw(dbg);
    g.drawImage(dbImg, 0, 0, this);
}

public void draw(Graphics g) {
    for (int i = 0; i < circles.size(); i++) {
        BouncingCircle bc = circles.get(i);
        bc.draw(g);
    }
    repaint();
}

private void addCircle() {
    BouncingCircle bc = new BouncingCircle();
    circles.add(bc);
    Thread t = new Thread(bc);
    t.start();
}
public static void main(String[] args) {
  JFrame frame  = new JFrame("Game");
  frame.setVisible(true);
  frame.setSize(320,240);
  frame.setResizable(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Animation a = new Animation();
  frame.getContentPane().add(a);
    for (int i = 0; i < 5; i++) {
        a.addCircle();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            System.err.println("Error: Thread Interrupted.");
        }
    }
   }

the BouncingCircle class here:

public class BouncingCircle implements Runnable {
private int x;
private int y;
private int xVelocity;
private int yVelocity;

BouncingCircle() {
    x = 0;
    y = 0;
    xVelocity = 2;
    yVelocity = 2;
}

public void run() {
    while (true) {
        move();
        try {
            Thread.sleep(5);
        } catch (InterruptedException ex) {
            System.err.println("Error: Thread Interrupted.");
        }
    }
}

private void move() {
x += xVelocity;
y += yVelocity;
if (x < 0)
    xVelocity = 2;
if (x > 320)
    xVelocity = -2;
if (y < 0)
    yVelocity = 2;
if (y > 240)
    yVelocity = -2;
}

void draw(Graphics g) {
    g.setColor(Color.RED);
    g.fillOval(x, y, 10, 10);
  }
 }

but it doesnt show the backgroundcolor i tried with frame.add(a) but still not working

Was it helpful?

Solution

The probable cause is you've overridden one of the paint methods and not called its equivalent super method.

Remember paint does a lot of important things, like panting the background. The probable cause is you've overridden one of the paint methods and not called its equivalent super method.

Remember paint does a lot of important things, like panting the background.

Take a look at Performing Customing Painting for more details

Updated

This is your problem right here...

public void paint(Graphics g) {
    Image dbImg = createImage(getWidth(), getHeight());
    Graphics dbg = dbImg.getGraphics();
    draw(dbg);
    g.drawImage(dbImg, 0, 0, this);
}

The basic problem is, you've broken the paint chain, meaning that all the important work that paint does, like paint the background, isn't getting done.

Also, Swing components are already double buffered, so you don't need to use createImage and you shouldn't be overriding paint, but instead paintComponent

So, get rid of paint and use...

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    draw(g);
}

Instead... Take a look at Performing Customing Painting for more details

OTHER TIPS

Verified, works just fine!

import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Animation extends JPanel {
    // private ArrayList<BouncingCircle> circles;

    Animation() {
        this.setSize(320, 240);
        this.setBackground(new java.awt.Color(102, 255, 102));
        // circles = new ArrayList<BouncingCircle>();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Game");
        frame.setVisible(true);
        frame.setSize(320, 240);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Animation a = new Animation();
        frame.getContentPane().add(a);
        for (int i = 0; i < 5; i++) {
            // a.addCircle();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                System.err.println("Error: Thread Interrupted.");
            }
        }
    }
}

This'll work:

    public void paint(Graphics g) {
        super.paint(g);
        Image dbImg = createImage(getWidth(), getHeight());     
        Graphics dbg = dbImg.getGraphics();
        draw(dbg);
        g.drawImage(dbImg, 0, 0, null);
    }

    public void draw(Graphics g) {
        g.setColor(new java.awt.Color(102, 255, 102));
        g.fillRect(0, 0, getWidth(), getHeight());
        for (int i = 0; i < circles.size(); i++) {
            BouncingCircle bc = circles.get(i);
            bc.draw(g);
        }
        repaint();
    }

If you painting a full-size image over your panel, you should fill image's background then.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top