Jump to content
  • Advertisement
Sign in to follow this  
kino

[java] Drawing

This topic is 4906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

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!!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!