[java] Drawing

Started by
2 comments, last by H_o_p_s 19 years, 1 month ago
Hello everybody!! Wich class you guys use to draw and manipulate images (jpg,gif)...? My paint method is not working, I don't know why...
Long live Rock!!
Advertisement
First thing that you have to do is load the file in to an Image or BufferedImage.

java.net.URL url = getClass().getClassLoader().getResource(FILENAME); java.awt.image.BufferedImage BufferedImage TEMP ImageIO.read(url);java.awt.image.BufferedImage BufferedImage MY_IMAGE = MYFRAME.getGraphicsConfiguration().createCompatibleImage(TEMP.getWidth(),TEMP.getHeight(), TEMP.getColorModel().getTransparency());Graphics2D g = MY_IMAGE.createGraphics();g.drawImage(TEMP,0,0,null);g.dispose();


And then when I actually want to draw something to lets say a frame I do this:
public class PicTest {   static int WIDTH = 400;   static int HEIGHT= 300;   public JFrame f;   public BufferStrategy strategy;   public Graphics2D g;   public void init() {      f = new JFrame("MY TITLE");      f.setBounds(0,0,WIDTH,HEIGHT);      f.setIgnoreRepaint(true);      strategy = f.createBufferStrategy(2);      g = (Graphics2D)strategy.getDrawGraphics();      //LOAD SOME GRAPHICS INTO BUFFERED IMAGES WITH THE ABOVE FUNCTION   }   public void Main_Loop() {      while (true) {         //UPDATE GRAPHICS         //DRAW WHAT YOU WANT WITH:          //g.drawImage(MY_IMAGE, X, Y, null);         strategy.show();         Thread.yeild();      }   }   public static void main(String[] args) {      PicTest p = new PicTest();      p.init();      p.Main_Loop();   }}


Now... haven't tested this code, but thats the general idea ;)

[Edited by - H_o_p_s on March 16, 2005 2:54:09 PM]
BRING BACK THE BLACK (or at least something darker)

Thanks for the hint H_o_p_s!
I solved my problem...
I was making a mistake about setting the ContentPane into the JFrame, but now the things are all rigth...I hope so!

About your code, I couldn´t get the meaning of the method setIgnoreRepaint(true)
Why is it used??

So long!! Bye bye!!
Long live Rock!!
Usually in none intensive applications that you want to have a contolled way of representing images you would just overwrite the paint() method. Since we are using a bufferStrategy it is not needed. BufferStrategy handles are repaints now.
BRING BACK THE BLACK (or at least something darker)

This topic is closed to new replies.

Advertisement