如何创建bufferedimage的实例
展开全部
drawimage都是对Image对象处理,和组件的绘制一点关系也没;
把newImage的图再画到image里面去;
实例代码如下:
public class Tank extends JFrame { private Image img = null; boolean fi = false; BufferedImage bi; public Tank() { this.addKeyListener(new KeyMonitor()); this.setBounds(300, 300, 300, 300); this.setVisible(true); this.setDefaultCloseOperation(Tank.EXIT_ON_CLOSE);} class KeyMonitor extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case 37: { img = getToolkit().createImage("res/TankPic/pre.GIF"); fi = true; System.out.println(img); repaint(); break; } } } } public void paint(Graphics g) { super.paint(g); bi = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB); bi.getGraphics(); if (fi) { g = img.getGraphics(); g.drawImage(bi, 50, 50, 40, 40, this); }}public static void main(String[] args) { new Tank(); }}
把newImage的图再画到image里面去;
实例代码如下:
public class Tank extends JFrame { private Image img = null; boolean fi = false; BufferedImage bi; public Tank() { this.addKeyListener(new KeyMonitor()); this.setBounds(300, 300, 300, 300); this.setVisible(true); this.setDefaultCloseOperation(Tank.EXIT_ON_CLOSE);} class KeyMonitor extends KeyAdapter { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case 37: { img = getToolkit().createImage("res/TankPic/pre.GIF"); fi = true; System.out.println(img); repaint(); break; } } } } public void paint(Graphics g) { super.paint(g); bi = new BufferedImage(getSize().width, getSize().height, BufferedImage.TYPE_INT_ARGB); bi.getGraphics(); if (fi) { g = img.getGraphics(); g.drawImage(bi, 50, 50, 40, 40, this); }}public static void main(String[] args) { new Tank(); }}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询