Java中重写JPanel来画入图片,可是怎么也画不出来

酷呐么踏踏 发布于 2016/05/08 19:18
阅读 562
收藏 0

//添加拼图图片与图片预览
private void addPreviewImage() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1,2));

PictureCanvas canvas = new PictureCanvas();
canvas.setBorder(new TitledBorder("拼图区"));

PicturePreview preview = new PicturePreview();
preview.setBorder(new TitledBorder("图片预览区"));

panel.add(canvas, BorderLayout.WEST);
panel.add(preview, BorderLayout.EAST);

this.add(panel,BorderLayout.CENTER);


}

//图片预览区


public class PicturePreview extends JPanel {

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//指定图片路径
String filename = "picture\\1.jpg"; 
//通过图片路径,获取对应图片信息
ImageIcon icon = new ImageIcon(filename);
Image image = icon.getImage();
//把图片绘制到预览区面板中
g.drawImage(image, 20, 20, 450, 600, this);

}
}
加载中
返回顶部
顶部