Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java Image path question.

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Image path question.

    Hello fellow peeps and citizens of Lordaeron,

    A certain question has popped in my mind. A certain problem to be faced.

    Gyazo - e5663e8da42ac46a642895d72836b933.png

    As you can see I have specified a file path, as it is needed to draw the image.
    The image "sun" is in a certain folder inside the directory of my jar file.

    But If I were to send the whole package to my teacher to review.
    The file path would still remain the same, local to my computer making the image, unloadable.
    How were I to fix the problem?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Image path question.

    I almost deleted this because I thought it was spam. Please post an MCVE instead of asking us to go to external websites.

    If you're asking how to load resources from a jar, then googling "java load resource from jar" returns a bunch of results that explain it much better than a forum post can. But the gist is this: stuff inside a jar is no longer a file, so it can't be accessed by the File class. You have to load it as a resource instead using the getResource... methods.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Image path question.

    I see, but I have another question.

    What I am creating is a clock. That goes round n round using "getcalendar".
    It's realtime, but the clock has a constant resolution of 800x800.
    I have carefully placed numbers from 1-12 on the clock using that resolution placing them pixel by pixel.
    Now if I resize the window, the clock resizes but the text, it fades away out of the screen because the resolution is too small.

    This is how I draw them.

    g2.drawString(six, 380, 750);
    g2.drawString(twelve, 375, 70);
    g2.drawString(nine, 30, 415);
    g2.drawString(three, 725, 415);
    g2.drawString(one, 570, 120);
    g2.drawString(two, 690, 250);
    g2.drawString(four, 685, 575);
    g2.drawString(five, 570, 700);
    g2.drawString(seven, 200, 710);
    g2.drawString(eight, 80, 585);
    g2.drawString(ten, 80, 235);
    g2.drawString(eleven, 205, 115);

    I understand why this is happening, obviously because the strings are in a certain place just can't understand how do I make them scaleable?
    How were I to solve the problem? Is there an universal formula? Or do I have to draw every string relevant to some mathematical equation regarding to the screen size. For example : g2.drawString(twelve, diameter / 2, screenheight / 3);
    It might be hard to understand without pictures, what you guys are afraid to click on for some reason.
    Last edited by Trolltrain; July 4th, 2014 at 06:01 AM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Image path question.

    There isn't a magical formula. You have three options:

    1- Use a layout that does the scaling for you, and use JLabels instead of custom painting. It would probably be a mix of a few different layouts.
    2- Use the getWidth() and getHeight() methods to draw the Strings at a position relative to the size of the component.
    3- Create a single image that contains all of the numbers and then just scale that to whatever size you want.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Image Doesn't Display with Qualified Image Path????
    By swaginator in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2012, 12:29 AM
  2. How to convert local path drive to UNC path
    By krisswift in forum Object Oriented Programming
    Replies: 2
    Last Post: November 17th, 2011, 08:40 AM
  3. Relative path issue with Context path struts
    By chinnu in forum Web Frameworks
    Replies: 1
    Last Post: March 31st, 2011, 10:17 AM
  4. java applet program question (on getting path fails)
    By hellocheese in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 30th, 2011, 04:34 PM
  5. how to get full path name from realtive path
    By priyanka3006 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: August 10th, 2009, 04:28 AM