使用ImageIO将BufferedImage载入到JFrame中,用什么办法?
原源代码publicclassBufferedImageTestextendsBufferedImage{publicBufferedImageTest(intwidth...
原源代码
public class BufferedImageTest extends BufferedImage{
public BufferedImageTest(int width, int height, int imageType) {
super(width, height, imageType);
}
public static void main(String[] args){
JFrame f = new JFrame("Test");
Container c = f.getContentPane();
c.add(new BufferedImageTest(16,16,BufferedImage.TYPE_INT_BGR).new CanvasPanel());
f.setSize(660,445);
f.setVisible(true);
}
public class CanvasPanel extends JPanel{
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g){
super.paint(g);
BufferedImage output = new BufferedImage(16,16,BufferedImage.TYPE_INT_BGR);
try {
output.createGraphics().drawImage(ImageIO.read(new FileInputStream("E:\\MyDownloads\\Download\\eclipse-jee-mars-1-win32-x86_64\\eclipse\\pngs\\bedrock.png")),0,0,16,16,null);
} catch (Exception e) {
e.printStackTrace();
}
}
public CanvasPanel(){
this.paint(getGraphics());
}
}
}
这样子画不出来,求大神帮忙 展开
public class BufferedImageTest extends BufferedImage{
public BufferedImageTest(int width, int height, int imageType) {
super(width, height, imageType);
}
public static void main(String[] args){
JFrame f = new JFrame("Test");
Container c = f.getContentPane();
c.add(new BufferedImageTest(16,16,BufferedImage.TYPE_INT_BGR).new CanvasPanel());
f.setSize(660,445);
f.setVisible(true);
}
public class CanvasPanel extends JPanel{
private static final long serialVersionUID = 1L;
@Override
public void paint(Graphics g){
super.paint(g);
BufferedImage output = new BufferedImage(16,16,BufferedImage.TYPE_INT_BGR);
try {
output.createGraphics().drawImage(ImageIO.read(new FileInputStream("E:\\MyDownloads\\Download\\eclipse-jee-mars-1-win32-x86_64\\eclipse\\pngs\\bedrock.png")),0,0,16,16,null);
} catch (Exception e) {
e.printStackTrace();
}
}
public CanvasPanel(){
this.paint(getGraphics());
}
}
}
这样子画不出来,求大神帮忙 展开
1个回答
展开全部
你的Java程序我帮你改完了,现在可以显示图片了,你看看吧.
import java.awt.Container;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BufferedImageTest{
public static void main(String[] args){
JFrame f = new JFrame("Test");
Container c = f.getContentPane();
c.add(new BufferedImageTest().new CanvasPanel());
f.setSize(660,445);
f.setVisible(true);
}
class CanvasPanel extends JPanel{
@Override
public void paint(Graphics g){
super.paint(g);
try {
BufferedImage output=ImageIO.read(new FileInputStream("E:\\MyDownloads\\Download\\eclipse-jee-mars-1-win32-x86_64\\eclipse\\pngs\\bedrock.png"));
g.drawImage(output,0,0,256,256,null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询