DSP tutorial: Image loading

This simple example loads an image file and displays it in a window using a pixel buffer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class ImageLoading extends JPanel {

    private static final long serialVersionUID = 1L;
    static Color displayedImage[][] = new Color[320][256];
    static BufferedImage image = null;
   
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // drawing the image to the screen
        for (int y = 0; y < 256; y++) {
            for (int x = 0; x < 320; x++) {
                g.setColor(displayedImage[x][y]);
                g.drawLine(x,y,x,y);
            }
        }
    }
   
    public static void main(String[] args) {
        // loading the image
        try {
            image = ImageIO.read(new File("csaj2.png"));
        } catch (IOException e) {
        }

        JFrame frame = new JFrame("ImageLoading");
        frame.setSize(350, 300);
        // making the app close when the app window gets closed
        frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); }});
        frame.getContentPane().add(new ImageLoading());
        frame.setVisible(true);

        // copying the loaded image to the displayed image buffer
        for (int y = 0; y < 256; y++) {
            for (int x = 0; x < 320; x++)
                displayedImage[x][y] = new Color(image.getRGB(x, y));
        }
    }
}

download (mirror2) (97.4 kb, 132 dls, today: 0)

Name (required)
E-mail (required - never shown publicly)
Webpage URL
Comment:
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> in your comment.

Trackback responses to this post

About me

Nonoo
I'm Nonoo. This is my blog about music, sounds, filmmaking, amateur radio, computers, programming, electronics and other things I'm obsessed with. ... »

Twitter

Listening now

My favorite artists