Java 在 AWT Canvas 上绘制图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24958366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw image on AWT Canvas
提问by Stefan Surkamp
I'm trying to draw an image on an AWT Canvas.
我正在尝试在 AWT 画布上绘制图像。
The only line I've got this far is:
我到目前为止唯一的一条线是:
Canvas canvas = new Canvas();
I'm not sure which method to use to add an image to the canvas now... I tried createImage()
but didn't know how to continue with the ImageProducer...
我不确定现在使用哪种方法将图像添加到画布...我尝试过createImage()
但不知道如何继续使用 ImageProducer...
Maybe someone's got a little hint for me here?
也许有人在这里给我一点提示?
回答by msrd0
You have several ways to do this. If you don't want to create a subclass of Canvas
, add this line (where img
is a subclass java.awt.Image
):
您有几种方法可以做到这一点。如果您不想创建 的子类Canvas
,请添加以下行(其中img
是子类java.awt.Image
):
canvas.getGraphics().drawImage(img, 0,0, null);
This will draw your Image to position 0,0 on the Canvas. If you create a subclass of canvas, overwrite the paint
-Method:
这会将您的图像绘制到画布上的 0,0 位置。如果您创建画布的子类,请覆盖paint
-Method:
public void paint (Graphics g) {
g.drawImage(img, 0,0, null);
}
回答by Deepanshu J bedi
getImage()
drawImage()
There are many methods of drawing graphics in java.it will get you started
java中绘制图形的方法有很多种。它会让你开始
回答by tommybee
This is several steps how to draw image on the canvas.
这是如何在画布上绘制图像的几个步骤。
Make your own canvas on a frame object.
在框架对象上制作自己的画布。
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Display {
private JFrame jframe;
private static Canvas canvas;
private String title;
private int width, height;
public Display(String tuade, int rong, int dai) {
this.title = tuade;
this.width = dai;
this.height = rong;
initCanvas();
}
private void initCanvas() {
jframe = new JFrame(title);
jframe.setSize(width, height);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setResizable(false);
jframe.setVisible(true);
jframe.setLocationRelativeTo(null);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setMinimumSize(new Dimension(width, height));
jframe.add(canvas);
jframe.pack();
}
public Canvas getCanvas() {
if(canvas == null)
{
System.out.println("Canvas is null");
return null;
}
return canvas;
}
}
Then, draw an image on the Display class
然后,在 Display 类上绘制图像
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class DrawImageOnCavas implements Runnable {
private Display Display;
private Thread t;
private boolean running;
public int dai, rong;
private String tuade;
private BufferStrategy bs;
private Graphics g;
private BufferedImage testImage;
public DrawImageOnCavas(String tuade, int dai, int rong) {
this.dai = dai;
this.rong = rong;
this.tuade = tuade;
}
@Override
public void run() {
init();
System.err.println("run..." + running);
while (running) {
//System.err.println("run..." + running);
tick();
render();
}
//stop();
}
private void render() {
bs = Display.getCanvas().getBufferStrategy();
if (bs == null) {
System.out.println("bs is null....");
Display.getCanvas().createBufferStrategy(3);
return;
}
g = Display.getCanvas().getGraphics();
g.drawImage(testImage, 20, 20, null);
}
private void tick() {
}
private static final class ImageLoader
{
static BufferedImage loadImage(String fileName)
{
BufferedImage bi = null;
//System.err.println("....setimg...." + fileName);
try {
bi = ImageIO.read(new File(fileName));
} catch (IOException e) {
e.printStackTrace();
System.out.println("Image could not be read");
System.exit(1);
}
return bi;
}
}
private void init() {
Display = new Display(tuade, dai, rong);
testImage = ImageLoader.loadImage("texture/Lilong.png");
}
public synchronized void start() {
if (running) return;
running = true;
t = new Thread(this);
t.start();
}
public synchronized void stop() {
if (!running)
return;
running = false;
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
The entry method is as follows,
进入方法如下,
public static void main(String[] args) {
DrawImageOnCavas game = new DrawImageOnCavas("draw image", 400, 400);
game.start();
}