Graphics problem

I have a class that creates a filled rectangle. The idea is to call this object and create as many of this filled rectagles as possible. But when I create two objects of this class and try to call the paint method of each respective class only one rectangle is drawn.

I will very much appreciate your help guys

Sq2000

[337 byte] By [Sq2000a] at [2007-11-26 18:55:16]
# 1

> I have a class that creates a filled rectangle. The

> idea is to call this object and create as many of

> this filled rectagles as possible. But when I create

> two objects of this class and try to call the paint

> method of each respective class only one rectangle is

> drawn.

I am not sure how you are doing it, but I can only make wild guess without a Short,Self Contained, Compilable and Executable, Example Program, or at least some code.

Also, in the future topics like this should be on the Swing forum.

zadoka at 2007-7-9 20:33:21 > top of Java-index,Java Essentials,Java Programming...
# 2

Ok here is the code

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

import java.awt.image.*;

import java.awt.event.MouseEvent;

import javax.swing.event.MouseInputAdapter;

class MapImage extends JPanel

{private static Livestock livestock;

private static Livestock livestock2;

BufferedImage imageMap;

private static JFrame frame = new JFrame();

private String botswana = "Images/botswana.jpg";

private Rectangle2D.Double square =

new Rectangle2D.Double(0, 0, 350, 350);

private GradientPaint gradient =

new GradientPaint(0, 0, Color.red, 175, 175, Color.yellow,true); // true means to repeat pattern

private static int SQUARE_EDGE_LENGTH = 10;

private static JPanel panel;

private static int xPos;

private static int yPos;

private static int xPos1;

private static int yPos1;

private MapPanel mapPanel;

public MapImage (){

imageMap = getBufferedImage(botswana, this);

xPos = 10;

yPos = 10;

livestock=new Livestock(xPos,yPos);

xPos1 = 187;

yPos1 = 280;

livestock2 =new Livestock(xPos1,yPos1);

}

static MouseInputAdapter mia = new MouseInputAdapter(){

public void mousePressed(MouseEvent e){

xPos=e.getX();

xPos1 = e.getX();

livestock.setXcordinates(xPos);

livestock2.setXcordinates(xPos1);

System.out.println(" The X When mousePressed Cordinates are "+livestock.getXcordinates());

System.out.println(" The X2 When mousePressed Cordinates are "+livestock2.getXcordinates());

yPos= e.getY();

yPos1 = e.getY();

livestock.setYcoordinates(yPos);

livestock2.setYcoordinates(yPos1);

System.out.println(" The Y When mousePressed Cordinates are "+livestock.getYcoordinates());

System.out.println(" The Y2 When mousePressed Cordinates are "+livestock2.getYcoordinates());

frame.repaint();

}

public void mouseDragged(MouseEvent e){

xPos = e.getX();

livestock.setXcordinates(xPos);

xPos1 = e.getX();

livestock2.setXcordinates(xPos1);

livestock.setSQUARE_EDGE_LENGTH(SQUARE_EDGE_LENGTH);

livestock2.setSQUARE_EDGE_LENGTH(SQUARE_EDGE_LENGTH);

System.out.println(" The X When mouseDragged Cordinates are "+livestock.getXcordinates());

yPos = e.getY();

livestock.setYcoordinates(yPos);

System.out.println(" The X2 When mouseDragged Cordinates are "+livestock2.getXcordinates());

yPos1 = e.getY();

livestock2.setYcoordinates(yPos1);

System.out.println(" The Y When mouseDragged Cordinates are "+livestock.getYcoordinates());

System.out.println(" The Y2 When mouseDragged Cordinates are "+livestock2.getYcoordinates());

frame.repaint();

}

};

public void paintComponent (Graphics g){

g.drawImage (imageMap, 0, 0, this);

livestock.drawAnimalObject(g);

livestock.drawAnimalObject1(g);

}

using the method (drawAnimalObject(Graphics g)) from this class

import java.awt.*;

public class Livestock

{

private static int xPos;

private static int yPos;

private String iDNumber;

private String firstName;

private String lastName;

private String bolusNumber;

private String color;

private String sex;

private String brand;

private String brandPosition;

private String region;

private static int SQUARE_EDGE_LENGTH = 10;

public Livestock(int x, int y){

xPos=x;

yPos=y;

}

public void drawAnimalObject(Graphics g){

g.setColor(Color.RED);

g.fillRect(xPos-(SQUARE_EDGE_LENGTH/2), yPos-(SQUARE_EDGE_LENGTH/2), SQUARE_EDGE_LENGTH, SQUARE_EDGE_LENGTH);

return g;

}

Sq2000a at 2007-7-9 20:33:21 > top of Java-index,Java Essentials,Java Programming...
# 3
> Also, in the future topics like this should be on the> Swing forum.Please forget the return type in that method (drawAnimalObject(Graphics g)Sq2000
Sq2000a at 2007-7-9 20:33:21 > top of Java-index,Java Essentials,Java Programming...
# 4
You missed:ShortCompilable and ExecutableMaybe someone else will look at it. But if you won't spend the time to create a simple example that compiles. Then I am not going to spend time looking at it.
zadoka at 2007-7-9 20:33:21 > top of Java-index,Java Essentials,Java Programming...
# 5

> I have a class that creates a filled rectangle. The

> idea is to call this object and create as many of

> this filled rectagles as possible. But when I create

> two objects of this class and try to call the paint

> method of each respective class only one rectangle is

> drawn.

same position?

suparenoa at 2007-7-9 20:33:21 > top of Java-index,Java Essentials,Java Programming...
# 6
> You missed:> Short> Compilable > and > ExecutableThanks man i will keep you postedSq2000
Sq2000a at 2007-7-9 20:33:21 > top of Java-index,Java Essentials,Java Programming...