Stefan Surkamp
Stefan Surkamp

Reputation: 992

Draw image on AWT Canvas

I'm trying to draw an image on an AWT Canvas.

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...

Maybe someone's got a little hint for me here?

Upvotes: 1

Views: 18996

Answers (3)

tommybee
tommybee

Reputation: 2551

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

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();

}

enter image description here

Upvotes: 2

Deepanshu J bedi
Deepanshu J bedi

Reputation: 1530

getImage()
drawImage()

There are many methods of drawing graphics in java.it will get you started

Upvotes: 0

msrd0
msrd0

Reputation: 8371

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.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:

public void paint (Graphics g) {
    g.drawImage(img, 0,0, null);
}

Upvotes: 4

Related Questions