在AWT画布上绘制图像 [英] Draw image on AWT Canvas
本文介绍了在AWT画布上绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在AWT画布上绘制图像。
I'm trying to draw an image on an AWT Canvas.
我到目前为止唯一的一条线是:
The only line I've got this far is:
Canvas canvas = new Canvas();
我不确定现在用哪种方法将图像添加到画布......我试过 createImage()
但不知道如何继续使用ImageProducer ......
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...
也许某人有一个在这里给我一点暗示?
Maybe someone's got a little hint for me here?
推荐答案
这是如何在画布上绘制图像的几个步骤。
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;
}
}
然后,在Display类上绘制图像
Then, draw an image on the Display class
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();
}
}
}
输入方法如下,
public static void main(String[] args) {
DrawImageOnCavas game = new DrawImageOnCavas("draw image", 400, 400);
game.start();
}
这篇关于在AWT画布上绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文