Java Game Development – 7 – Loading Images

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

47 responses to “Java Game Development – 7 – Loading Images”

  1. Ishaan Chauhan Avatar

    the pinnacle of photoshop

  2. Christian Waldmann Avatar

    How do you make all of the drawn elements fit the resized screen? s there not an easier way than using the many parameters in drawImage()?

  3. Aniket Singh Avatar

    The background image does not comes up. How to fix this issue?

  4. A. K. Avatar

    For me this sounds a bit stupid. You wont be able to load these pictures anywhere else than on this computer. why dont you load them from a resource folder in your project?

  5. Ziya Keskin Avatar

    For mac users – /Users/"Your username"/Desktop/test/back.jpg – for the address of pictures.

  6. Ender Luna Avatar

    Do I have to use photoshop

  7. Dim The Quadraforce Owner Avatar

    didn't worked.
    |
    here |
    /
    import javax.swing.*;
    import javax.swing.ImageIcon;
    import java.awt.*;

    public class Image extends JFrame {
    public static void main(String[] args) {
    Thread t1 = new Thread(new Apple("Slava"));
    Thread t2 = new Thread(new Apple("Dima"));
    Thread t3 = new Thread(new Apple("Mom"));
    t1.start();
    t2.start();
    t3.start();

    DisplayMode dm = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
    Image i = new Image();
    i.run(dm);
    }

    private Screen s;
    private java.awt.Image bg;
    private java.awt.Image pic;
    private boolean loaded;

    public void run(DisplayMode dm){
    setBackground(Color.PINK);
    setForeground(Color.RED);
    setFont(new Font("Arial", Font.PLAIN, 24));
    loaded = false;
    s = new Screen();
    try{
    s.setFullScreen(dm, this);
    loadpics();
    try{
    try {
    Thread.sleep(10000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }catch(Exception e){}
    }finally{
    s.restoreScreen();
    }

    }

    private void loadpics() {
    bg = new ImageIcon("res\back.png").getImage();
    pic = new ImageIcon("res\face.png").getImage();
    loaded = true;
    repaint();
    }

    public void paint(Graphics g){
    if(g instanceof Graphics2D){
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    }
    if(loaded){
    g.drawImage(bg, 0, 0 ,null);
    g.drawImage(pic, 170, 180, null);
    }
    }
    }

    can anybody help me?

  8. FusedAtoms Avatar

    For those having problems with the background not resizing to fullscreen change the:
    g.drawImage(bg,0,0,null);

    to the following code:
    g.drawImage(bg,0,0,s.getFullScreenWindow().getWidth(),s.getFullScreenWindow().getHeight(),0,0,bg.getWidth(null),bg.getHeight(null),null);

    it should now resize the background to fit your screen.

  9. OverloadedCore Avatar

    the "bg" comes up as a mini image and not the background

  10. Jake Avatar

    is is worth it to stick through this playlist if each video has errors due to java updates?

  11. Filip Dobrinic Avatar

    the issue why it won't work for some of you is probably pathing. If you have a folder "workspace" which Eclipse makes itself, go in there, enter the Project name of your Java game developement, paste images there and change pathing to just :
    bg = new ImageIcon("background800by600.jpg").getImage();
    pic = new ImageIcon("bicon100by100.png").getImage();

  12. JT Tran Avatar

    died when you said swine flu

  13. Joshua Domingo Avatar

    Its not working for me :/
    Maybe I don't have the right path setup because I'm running Ubuntu 15.04
    All I see is the pink background

  14. dzagIT Avatar

    Works everything fine. I installed jdk 1.8.0_45 instead 1.7 and changed default location of JRE in Eclipse to jre1.8.0_45.

  15. Wolf Pup Avatar

    "Sorry. Swine flu."

  16. Izuru15 Avatar

    All I get is a black screen, why is this???

  17. aldabomb69 Avatar

    im getting an error when i do the private Screen s;
    did i forget to import something or am i just doing something wrong?

  18. bagotman Avatar

    i need some help! when i run the program is gives me this error:
    Exception in thread "main" java.lang.NullPointerException
    at Images.run(Images.java:38)
    at Images.main(Images.java:13)
    also that i don't have the restoreScreen function so i substituted it for returnScreen

  19. Tavy Octav Avatar

    another ways to load images:
    bg = Toolkit.getDefaultToolkit().getImage("back.jpg");  // put image in the project folder
    bg = new ImageIcon(getClass().getResource("back.jpg")).getImage(); // put image in src / <name_of_project> /

  20. Cory Harrington Avatar

    no syntax errors but just the pink screen :/

  21. rafalpilat145 Avatar

    This is a bit outdates. Can you do an updated series?

  22. James Price Avatar

    Awesome, this worked brilliantly. We were given a template to work from but I didnt really understand most of it. I much rather this method were I can understand how it works a lot easier

  23. SgtDrPeppers Avatar

    Why do people pronounce png as ping? There's no i in there. If anything it should be pong.

  24. Brian R Avatar

    "Image" is coming back as an unknown type.  I did include ImageIcon, so I don't see what the problem is…………

  25. Wircea Avatar

    I have a question.

    For some reason, even if the code is 100% same as Bucky's , I get errors as certain lines such as "s.setFullScreen(dm,this);" (-line 27).

    I re-wrote all error lines like 5 times then I rewrote the entire code but the error persists.

    I tried to remove the line but when I press "run" it tells me there are errors but shows an empty list.

    Any ideas?

  26. Paul Gatto Avatar

    My Background image (800×600, png) isn't going to full screen. Is this normal?

    P.S.: Bucky RULES!

  27. Games. Avatar

    how to make an image without background 

  28. Games. Avatar

    Don't foget to put the projectmap in the loacation
    C:\map\projectmap\image.jpg
    NOT
    C:\map\image.jpg

  29. predvcecerom Avatar

    Just spend about 2 days trying to load those stupid images then I finally came back here and bang!  in less then a minute my spaceship was visible THX once again if I had spare money I would send you some 😀

  30. Jeffrey Monaco Avatar

    I just wanted to repost something that was said by Tom Christensen that helped me solve my exception error.
     
    I got a nullpointerexception error. I changed the private screen s to Screen s = new Screen(); and it worked fine.

  31. kanishka garg Avatar

    I am facing a problem that screen is blinking a lot. How can I fix this issue?

  32. smartbro678 Avatar

    Thank you sir, it works on my netbeans from my mac 🙂

  33. Irakli K. Avatar

    love your videos!

  34. Jack Patterson Avatar

    Please any one how do I insert my .PNG image when Im on mac

  35. James Hughes Avatar

    Nah it's like gif, people pronounce it with a soft and hard g, so gif or jiff. Since these are acronyms anyway, there's not really a universally accepted way to pronounce them.
    I pronounce it pee en gee though.

  36. Westycoot Avatar

    just put .jpg instead, it read it correctly for me then.

  37. koec12 Avatar

    jpeg not supported? -.-

  38. MasterGrooveProducts Avatar

    a ping?? why don't you call it a P(ee) e(N) G(ee) ??
    or is this the way to pronounce it in america?
    -I'm from belgium and we pronounce PNG by saying each letter..

  39. Alvano Calvillo Avatar

    i had the same problem with my mac eclipse. i switched "\" for "//"

  40. cbrez101 Avatar

    Comment the whole class out or remove the main method

  41. jbarton727 Avatar

    wouldn't it be easier to do:
    public void loadpics(Image i, String path) {
    i = new ImageIcon(path).getImage()
    }

  42. WasdGamer01 Avatar

    Using png or gif image format, you can create a transparent background. Don't think you can with jpeg

  43. WasdGamer01 Avatar

    If you are using netbeans you can create a new package and then store them in that. If you have your images stored a level above the class you are using to load them you need to use ".." before the first "" to step up a level.

  44. Bomber Avatar

    sorry..its in my res folder still its not working

Leave a Reply

Your email address will not be published. Required fields are marked *