0

I am trying to add a background image to java awt canvas but still couldn't make it through. can any body have the solutions for this or any piece of code for this problem. the code is given below,

thanks.

import java.awt.*;
import java.applet.*;
import java.util.Vector;
import java.util.Enumeration;

public class DiagramEditor extends Canvas {

private Vector diagrams = new Vector(16);
Diagram currentDiagram;
DiagramEditorControls controls;
Tool tool;
Image offscreen;
public final static int RECTANGLE = 0;

public final static int SELECTION = 3;
public String toolNames[] = {"Rectangle", "", "", "Selection"};

public DiagramEditor() {
    setBackground(Color.white);
    newDiagram();
}

public void setControls(DiagramEditorControls c) {
    controls = c;
}

public void setTool(int t) {
    switch (t) {
        case RECTANGLE:
            tool = new WrectangleTool(currentDiagram);
            break;

        case SELECTION:
            tool = new SelectionTool(currentDiagram);
            break;
    }
    repaint();
    if (controls != null) {
        controls.toolChoice.select(t);
    }
}

public void paint(Graphics g) {
    update(g);
}

public void update(Graphics g) {
    Dimension canvasSize = size();
    if (offscreen == null) {
        offscreen = this.createImage(canvasSize.width, canvasSize.height);
    }
    Graphics og = offscreen.getGraphics();
    og.setColor(getBackground());
    og.fillRect(0, 0, canvasSize.width, canvasSize.height);
    og.setColor(Color.black);
    og.drawRect(0, 0, canvasSize.width - 1, canvasSize.height - 1);
    og.setColor(Color.blue);
    currentDiagram.draw(og);
    tool.draw(og);
    g.drawImage(offscreen, 0, 0, this);
}

public void deleteElements() {
    tool.delete();
    repaint();
}

public void nextDiagram() {
    if (currentDiagram == diagrams.lastElement()) {
        currentDiagram = (Diagram) diagrams.firstElement();
    } else {
        int diagramIndex = diagrams.indexOf(currentDiagram);
        currentDiagram = (Diagram) diagrams.elementAt(diagramIndex + 1);
    }
    setTool(RECTANGLE);
}

public void newDiagram() {
    currentDiagram = new Diagram();
    diagrams.addElement(currentDiagram);
    setTool(RECTANGLE);
}

public boolean mouseDown(Event e, int x, int y) {
    tool.press();
    repaint();
    return true;
}

public boolean mouseDrag(Event e, int x, int y) {
    tool.move(new Point(x, y));
    repaint();
    return true;
}

public boolean mouseMove(Event e, int x, int y) {
    tool.move(new Point(x, y));
    repaint();
    return true;
}

public boolean mouseUp(Event e, int x, int y) {
    tool.release();
    repaint();
    return true;
}
}

this is a drawing canvas. default the background color is white ..i want it to be changed to an image...?

Jarrod
  • 9,349
  • 5
  • 58
  • 73
Thusira Dissanayake
  • 153
  • 1
  • 4
  • 15
  • 1
    Could you provide the code that isn't working for you? – brimble2010 Nov 06 '12 at 17:38
  • 1) `DiagramEditor extends Canvas` Uggh.. it is the 3rd millennium. Time to migrate to using Swing. Part of the reason is that most people have never used AWT, while the rest of us forget the details. There are other good reasons though. 2) For better help sooner, post an [SSCCE](http://sscce.org/). (E.G. that example would need a `main(String[])` to put it on-screen.) – Andrew Thompson Nov 07 '12 at 05:29

2 Answers2

1

The line below:

offscreen = this.createImage(canvasSize.width, canvasSize.height);

should become something like:

try {
  offscreen = ImageIO.read(new File("path/to/image"));
} catch (IOException e) {
  e.printStackTrace();
}
Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • 1
    I have no idea what "not worked" means. Please post some code that I can compile and run if you need more help. – Dan D. Nov 06 '12 at 19:31
0

Could you not just add a JLabel to the canvas, and add a background image that way? Are you using a java compiler?

JavaProdigy
  • 43
  • 4
  • 11