2016-11-05 98 views
-1

我想繪製緩衝圖像。我能夠在框架上獲得圖片,但它似乎不能在圖像上繪製。如果我使用在緩衝圖像上繪圖

BufferedImage bufferedImage = new BufferedImage(1280, 800,BufferedImage.TYPE_INT_RGB);

那麼似乎繪製字符串,但我想在圖像上繪製的理想,因爲我需要繪製圖像上的一些座標的項目。任何指導將不勝感激。請問壞壓痕

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class drawTest extends JPanel { 

public void paint(Graphics g) { 
    Image img = createImageWithText(); 
    g.drawImage(img, 20,20,this); 
} 

private Image createImageWithText(){ 
    BufferedImage bufferedImage = new BufferedImage(1280, 800,BufferedImage.TYPE_INT_RGB); 
// BufferedImage bufferedImage = new BufferedImage() 
    Graphics g = bufferedImage.getGraphics(); 

    try { 
    bufferedImage = ImageIO.read(getClass().getResource("Unknown.jpg")); 

    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    g.drawString("Point is here", 20,20); 


    return bufferedImage; 
} 

    public static void main(String[] args) { 
    JFrame frame = new JFrame(); 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    double width = screenSize.getWidth(); 
    double height = screenSize.getHeight(); 
    frame.getContentPane().add(new drawTest()); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
// frame.setSize(200, 200); 

    frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    System.out.println(height + " " + width); 
    frame.setVisible(true); 
} 
} 

回答

3

你正在創建 BufferedImage的對象 - 一個你從得到的圖形上下文和借鑑的文本,以及其他持有ImageIO的通過獲得的畫面,你唐't繪製文字。你返回後者,所以圖片沒有新的文字是有道理的。

// BufferedImage Object ONE 
    BufferedImage bufferedImage = new BufferedImage(1280, 800, BufferedImage.TYPE_INT_RGB); 
    Graphics g = bufferedImage.getGraphics(); // Graphics for the first object only 

    try { 
     // BufferedImage object TWO 
     bufferedImage = ImageIO.read(getClass().getResource("Unknown.jpg")); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    // draw with the graphics context for the first object 
    g.drawString("Point is here", 20, 20); 

    return bufferedImage; // but return the second 

解決方案:不這樣做,只能創建一個BufferedImage的,說通過ImageIO的,得到它的顯卡方面,用它畫畫,處置圖形完成後,並將其返回。

例如,

// have method accept the image path and 
// have it throw an exception if the path is bad 
private Image createImageWithText2(String resourcePath) throws IOException { 

    // create one and only one BufferedImage object. 
    // If this fails, the exception will bubble up the call chain 
    BufferedImage bufferedImage = ImageIO.read(getClass().getResource(resourcePath)); 

    // get the Graphics context for this single BufferedImage object 
    Graphics g = bufferedImage.getGraphics(); 

    g.drawString("Point is here", 20, 20); 

    g.dispose(); // get rid of the Graphics context to save resources 

    return bufferedImage; 
} 

的其他問題與您的代碼是在這裏:

public void paint(Graphics g) { 
    Image img = createImageWithText(); 
    g.drawImage(img, 20,20,this); 
} 

的問題包括:

  • 你覆蓋了錯誤的畫法。你應該重寫paintComponent,而不是繪畫,事實上你的問題提到了paintComponent,所以我不確定你爲什麼要這樣做。
  • 你壓倒一種繪畫方法,但沒有調用super的方法,打破了繪畫鏈。
  • 您在繪畫方法中不必要地重複執行文件I/O,這種方法對GUI的感知響應性有最大影響,所以您不想做的事情。一旦讀取圖像將其存儲到一個變量,使用paintComponent內的變量,並且從不在繪畫方法中執行文件I/O。
  • 你會想學習和使用Java naming conventions。變量名應該全部以小寫字母開頭,而類名則以大寫字母開頭。瞭解這一點,並遵循這一點將使我們能夠更好地理解您的代碼,並讓您更好地理解其他代碼。
+0

是的,我知道它是一個不好的代碼atm。這只是一個測試文件。我之前沒有使用緩衝圖像,所以試圖讓我的頭在附近。謝謝你,但仍然有幫助。如果還可以指導我如何調整圖像的大小而不使其像素化,那會很棒 – bawa