hey guys, i'm relatively new to java programming and recently tried to create a program that displays a BufferedImage onto a JPanel. i don't get any compiler errors when i compile, but the image doesn't show up.
import javax.imageio.ImageIO; import java.io.File; import java.awt.image.BufferedImage; import java.awt.*; import javax.swing.*; import java.awt.Graphics; public class ImageEditor{ public static void main(String[] args) throws java.io.IOException { BufferedImage cat = ImageIO.read( new File("/* File */")); JFrame canvas = new JFrame(); canvas.setSize(cat.getWidth()+100,cat.getHeight()+100); canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); canvas.setTitle("It's a cat."); Container pane = canvas.getContentPane(); ColorPanel panel = new ColorPanel(cat); canvas.setVisible(true); } } class ColorPanel extends JPanel{ BufferedImage theCat; public ColorPanel(BufferedImage image){ theCat = image; } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(theCat, null, 50,50); } }
i know i could use ImageIcon for this, but i'm trying to get used to BufferedImage. thanks!