2014-12-05 63 views
-2

如何創建BufferedImage。在BufferedImage上繪製圖像並顯示該圖像?如何繪製緩衝圖像,然後將緩衝圖像繪製到JFrame? [Java的1.7 NoLibs-的Eclipse];

例子:

 BufferedImage screen = new BufferedImage(200, 200, 
       BufferedImage.TYPE_BYTE_INDEXED); 


    Graphics2D graphic = screen.createGraphics(); 

      graphic.drawImage(sprite.Coalblock, 0, 0, screen.getHeight, screen.getWidth, null); 
      g2d.dispose(); 

要做到這一點爲什麼:

所以我可以創建一個系統,我的屏幕可以調整所有的屏幕尺寸。通過更改所有圖像顯示的圖像大小。

圖像必須是可控的,可以實現並顯示在屏幕上。任何可靠的系統來做到這一點?

+0

不,一個?這很難過 – 2014-12-05 16:55:16

+0

只有27分鐘。您在JFrame內繪製一個JPanel。您不直接繪製到JFrame。 – 2014-12-05 16:59:24

+0

對不起,我不認爲你理解。但感謝您的建議。我可以顯示圖像。但是,這是通過緩衝圖像>屏幕。我希望它像:緩衝圖像>緩衝圖像>屏幕 – 2014-12-05 17:01:23

回答

0

我找到了。我熬夜了:這裏是:

public BufferedImage screen = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 

public void render() { 

    BufferStrategy bs = this.getBufferStrategy(); 
    if(bs == null){ 
     createBufferStrategy(3); 

     return; 
    } 

    g = (Graphics2D) bs.getDrawGraphics(); 



    s = screen.createGraphics(); 


    s.setColor(Color.red); 

    s.fillRect(0, 0, WIDTH * SCALE, HEIGHT * SCALE); 
    s.drawImage(im.stoneTile, 50, 50, 400, 400, null); 

    g.drawImage(screen, 0, 0, WIDTH, HEIGHT, null); 
    g.dispose(); 
    bs.show(); 


} 

這就是你如何繪製到bufferedimage然後顯示它。 :)

0

下面是一個簡單的Java應用程序,該應用程序在BufferedImage上繪製一個圓圈,並將該圓圈每2秒重新繪製一個隨機位置。

package com.ggl.testing; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.util.Random; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class CircleSprite implements Runnable { 

    private BufferedImage circle; 

    private DrawingPanel drawingPanel; 

    private JFrame frame; 

    @Override 
    public void run() { 
     circle = createCircle(); 

     frame = new JFrame("Circle Sprite"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     drawingPanel = new DrawingPanel(circle); 
     frame.add(drawingPanel); 

     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 

     new Thread(new RandomDraw(drawingPanel)).start(); 
    } 

    private BufferedImage createCircle() { 
     BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); 
     Graphics g = image.getGraphics(); 

     g.setColor(Color.WHITE); 
     g.fillRect(0, 0, 100, 100); 
     g.setColor(Color.BLUE); 
     g.fillOval(10, 10, 80, 80); 
     g.dispose(); 

     return image; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new CircleSprite()); 
    } 

    public class DrawingPanel extends JPanel { 

     private static final long serialVersionUID = -4603711384104715819L; 

     private int x; 
     private int y; 

     private BufferedImage image; 

     public DrawingPanel(BufferedImage image) { 
      this.image = image; 
      this.x = 0; 
      this.y = 0; 
      setPreferredSize(new Dimension(500, 500)); 
     } 

     public void setPoint(int x, int y) { 
      this.x = x; 
      this.y = y; 
      repaint(); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawImage(image, x, y, null); 
     } 

    } 

    public class RandomDraw implements Runnable { 

     private DrawingPanel drawingPanel; 

     private Random random; 

     public RandomDraw(DrawingPanel drawingPanel) { 
      this.drawingPanel = drawingPanel; 
      this.random = new Random(); 
     } 

     @Override 
     public void run() { 
      while (true) { 
       sleep(); 
       int x = random.nextInt(400); 
       int y = random.nextInt(400); 
       drawingPanel.setPoint(x, y); 
      } 
     } 

     private void sleep() { 
      try { 
       Thread.sleep(2000L); 
      } catch (InterruptedException e) { 

      } 
     } 

    } 

} 
+0

感謝tring!但這不是我所需要的。仍然喜歡整潔的代碼。 – 2014-12-05 17:04:50

+0

等一下,這可能是我試驗過的。一秒。 – 2014-12-05 17:05:38

+0

線程「main」中的異常java.lang.Error:未解決的編譯問題: \t at CircleSprite.main(Something.java:50) :/ – 2014-12-05 17:07:54