【问题标题】:Add image to a java awt Canvas background将图像添加到 java awt Canvas 背景
【发布时间】:2012-10-26 17:15:57
【问题描述】:

我正在尝试向 java awt canvas 添加背景图像,但仍然无法通过。任何机构都可以解决这个问题或解决这个问题的任何代码。代码如下,

谢谢。

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

这是一个绘图画布。默认背景颜色为白色..我想将其更改为图像...?

【问题讨论】:

  • 你能提供不适合你的代码吗?
  • 1) DiagramEditor extends Canvas Uggh.. 这是第三个千年。是时候迁移到使用 Swing 了。部分原因是大多数人从未使用过 AWT,而我们其他人则忘记了细节。不过,还有其他很好的理由。 2) 为了尽快获得更好的帮助,请发布SSCCE。 (例如,该示例需要 main(String[]) 才能将其显示在屏幕上。)

标签: java image awt java-canvas


【解决方案1】:

下面一行:

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

应该变成这样:

try {
  offscreen = ImageIO.read(new File("path/to/image"));
} catch (IOException e) {
  e.printStackTrace();
}

【讨论】:

  • 我不知道“不工作”是什么意思。如果您需要更多帮助,请发布一些我可以编译和运行的代码。
【解决方案2】:

你能不能只在画布上添加一个 JLabel,然后以这种方式添加背景图像?你用的是java编译器吗?

【讨论】:

  • 是的,你能给我方法,我的代码也添加到帖子中......?
猜你喜欢
  • 2015-11-06
  • 2012-04-09
  • 2011-06-01
  • 2017-01-14
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多