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]

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