Armand Pette Armand Pette - 8 months ago 22
Java Question

Java Swing : Graphic component only appears in top-left corner zone

I am currently trying to realise a simple animation in Java.
I have one panel in which I would like to print one BufferedImage. I set the image location at one point and I set its dimensions.

However it is as if I was placing the "print zone" of my image in the top-left with its dimensions, and only the part of my image that belongs to that print zone is drawn as you can see on the folowing picture :

I guess it is propably one basic stuff I am missing however I cannot find any answer corresponding to my problem and I am a beginer in java so..

Here is the part of my code that is related to this problem :

public abstract class ObjetAnime extends JPanel{
protected Point location;
protected Dimension dim;
protected BufferedImage picture;

ObjetAnime(ObjetsPropriete objetProp)
location = new Point(10,10);
dim = objetProp.getDimension();
try {
picture = File(objetProp.getPath()));
} catch (IOException e1) {


protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;

if (picture != null) {
g.drawImage(picture, location.x, location.y, this);

And in the super class I have :

public ListeObjets(GConfig gconfig, GPanel jeuPanel)
setListeObjets(new ArrayList<ObjetAnime>());
panel = jeuPanel;
for( int i = 0; i < gconfig.getObjetsTab().length; ++i)
case ROND:
getListeObjets().add(new Rond(gconfig.getObjetsTab()[i]));

public void addToPanel(GPanel jeuPanel) {
Iterator iterator = getListeObjets().iterator();
ObjetAnime obj = (ObjetAnime);
jeuPanel.add(obj, obj.location);
obj = (ObjetAnime);
jeuPanel.add(obj, obj.location);


And the object properties are :

public ObjetsPropriete()
path = new String("./Images/Boule.png");
dim = new Dimension(32,32);
type = Objet.ROND;

Thanks a lot and have a nice day !

location = new Point(10,10);

All your objects have a location of (10, 10), so I would guess they point over top of one another. So get rid of that statement.

jeuPanel.add(obj, obj.location);

The second parameter means nothing to the add(...) method. It does not specify the location of the component. Get rid of the second parameter.

(if I understand your code) and you want to specify the location then you need to do something like:

//getListeObjets().add(new Rond(gconfig.getObjetsTab()[i]));
Rond rond = new Rond(gconfig.getObjetsTab()[i]);
rond.setLocation(32*i, 32*i);

The above code would position each object diagonally in the panel.

The key point is you need to set each component to a unique location.