Armand Pette Armand Pette - 6 months ago 14
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 :

http://i.stack.imgur.com/CRmCv.png

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 = ImageIO.read(new File(objetProp.getPath()));
} catch (IOException e1) {
e1.printStackTrace();
}

this.setSize(dim);
this.setLocation(location);
this.setLayout(null);
this.setOpaque(false);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

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)
{
switch(gconfig.getObjetsTab()[i].getType())
{
case ROND:
getListeObjets().add(new Rond(gconfig.getObjetsTab()[i]));
break;
}
}
addToPanel(panel);
}

public void addToPanel(GPanel jeuPanel) {
jeuPanel.setLayout(null);
Iterator iterator = getListeObjets().iterator();
ObjetAnime obj = (ObjetAnime) iterator.next();
jeuPanel.add(obj, obj.location);
while(iterator.hasNext())
{
obj = (ObjetAnime) iterator.next();
jeuPanel.add(obj, obj.location);
}
System.out.println(obj);

}


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 !

Answer
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);
getListeObjets().add(rond);

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.

Comments