views:

151

answers:

4

Hi

Im trying to get to grips wth java 2d graphics

Ive basically got a JPanel with a backgrounfd image in it like so:

public MapFrame(Plotting pl){
    this.pl =pl;
    this.setPreferredSize(new Dimension(984,884));
    this.setBorder(BorderFactory.createEtchedBorder());
    try {
          getFileImage("stars.jpg");
        }
        catch (Exception ex) {

        }

    this.addMouseMotionListener(this);
    this.addMouseListener(this);
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);    
    g.drawImage(bg, 0, 0, null);
    Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(new Color(0x756b48));
            g2d.drawLine(0,0,0,100);
}

private void getFileImage(String filePath) throws InterruptedException, IOException {
        FileInputStream in = new FileInputStream(filePath);
        byte [] b=new byte[in.available()];
        in.read(b);
        in.close();
        bg=Toolkit.getDefaultToolkit().createImage(b);
        MediaTracker mt=new MediaTracker(this);
        mt.addImage(bg,0);
        mt.waitForAll();
     }

In paint component I want to overlay small images 12x12 pixels in a loop at various xy points that ill get from some xml.

Cant seem to get an image to overlay over my first one

Im a bit lost here and v rusty

Any help would b gr8

A: 
public void paintComponent(Graphics g) {
    g.drawImage(bg, 0, 0, null);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(new Color(0x756b48));
    g2d.drawLine(0,0,0,100);

    for(SomeXMLObject o : yourXMLSource) {
        g.drawImage(yourImage, o.x, o.y, null);
    }
}

Please specify clearer how your XML is parsed, if you already did that. Then, you'd also need to load the "12x12" image. SomeXMLObject is a structure containing x and y variables, extracted from your XML.

If you call g.drawImage(...) after the background: it will be painted after the background, and thus overlay. Be sure you load a png-24 image, to enable translucency-areas, if you'd want that.

Pindatjuh
XmL parsing isnt a problem Ive definently got all my filenames and can iterate thru them.My first image is painted on to a jPanel as a background no problem.I then want to overlay smaller images at certain points (which I know and have fine from my xml) in front of the background
Yes, that's what I've understood from your question. Still; nobody can help you if you don't provide enough information.For instance; what kind of XML parsing do you use, how are the coordinates stored: as an object?Two other guys have essentially said the same: use <code>g.drawImage(...);</code>, but... you're the programmer! So you have to program something for it to work, right.
Pindatjuh
A: 
Samuel Sjöberg
A: 

You probably want to use use the ImageIO library to load your image. If you have an image filename all you need to do to load it is

BufferedImage bimg = ImageIO.load(new File(filename));

That's a little easier then the code you have above.

After that, like other people said you can use the g.drawImage(bimg,x,y,this); to actually draw the images.

Chad Okere
A: 

Oh Dear

Id formatted the filenames of my resources wrong

what a donkey I am

All good advice I think though guys