2016-11-05 8 views
-1

私はバッファリングされたイメージを描画しようとしています。私はフレーム上の画像を取得することができますが、それは画像上に描画するdoesntのようです。私はバッファリングされたイメージに描画する

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

を使用する場合、文字列を描画するようだが、私はプロジェクトのために、画像上のいくつかの座標をプロットする必要があるように私は、理想的には、画像上に描画したいと思います。どんな指導も高く評価されます。悪いあなたがのBufferedImageオブジェクトを作成している

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

インデントすみません - 、ImageIOに介して取得した画像を保持している1あなたからGraphicsコンテキストを取得し、上のテキストを描画、およびその他のそのあなたドン'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に経由して、そのGraphicsコンテキストを取得すると言う、が行われたときにグラフィックスを配置し、それを返します。

例えば、あなたのコードで

// 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が記述されていますので、なぜこれを行うのかはわかりません。
  • あなたはペインティングメソッドをオーバーライドしていますが、スーパーチェーンのメソッドを呼び出すことはなく、ペイントチェーンを解除しています。
  • ペイント方法、GUIの応答性に最も大きな影響を与える方法など、不必要に繰り返してファイルI/Oを実行しているため、実行したくないことがあります。 で一度画像を読む変数に格納し、paintComponent内の変数を使用し、描画メソッド内でファイルI/Oを実行しないでください。
  • Java naming conventionsを学び、使用したいと思うでしょう。変数名はすべて小文字で始まり、クラス名は大文字で始める必要があります。これを学んでこれに従うことで、コードをよりよく理解でき、他者のコードをよりよく理解できるようになります。
+0

はい、私は悪いコードatmを知っています。これは単なるテストファイルです。私はそれのまわりで頭を上げようとする前に、バッファリングされたイメージを使用していなかった。しかし、それでもありがとう、ありがとう。もし画像をピクセル化せずに画像のサイズを変更する方法を私に導くことができたなら、それは素晴らしいでしょう – bawa

関連する問題