Page 2 of 5 FirstFirst 1234 ... LastLast
Results 21 to 40 of 83
Like Tree1Likes

Thread: Images and stuff

  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    a code, where 1 BufferedImage is shown at the screen
    There are lots of code examples on this forum. Search for drawImage

  2. #22
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    ill try now until 4:00 AM.. if i dont managed to do it then, ill come back....
    (where do these pulbic void paint(graphics g) always come from? )

  3. #23
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    you could use a JLabel, add an image to it and then use setIcon to change the image.

  4. #24
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    the problem is .. that these images really pissing my nerves off..
    sorry.. but i hate them from the deepest of my soul.
    they just cant be done , but by some chosen ones..

    there is always some graphics g that just comes from the middle of the nowhere..
    and there are normal Images,BufferedImages and ImageIcons.. what should i now use? ...

    im thinking of just making more buttons with icons..........
    these are so much easier then normal images...

    ps: no code that explains me anything in this forum.. :/ there is a thread about drawImage problems.. but there is again the public void paintComponent(graphics g)
    and those graphics g are defined... surprise: nowhere....
    thx for your help guys though....

    the problem is , that the internet has so much to tell about images, but not 1 example which draws 1 image with 1 defined grafics g 1 path giving me a code i can understand...


    am i that stupid?

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    I guess you didn't try this:
    here are lots of code examples on this forum. Search for drawImage
    I found several examples.

  6. #26
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    Stop hitting your head against a wall and look up "Java Images" Then go the lessons page. Also, you can use drawImage, ImageIcon, Image, BufferedImage(Not suggested). would you like an example?

    Edit: Have you programmed/scripted with anything else besides Java, and how new are you to Java?
    Last edited by kammce; 08-12-2011 at 03:44 AM.

  7. #27
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    yes please..


    edit: looking up things about images does not work.
    you only get methods and 2 lines of code... that does not help me at all...
    ive been coding for about 5-15 hours every day since a week ago and not only tried to do images once...
    but me and my friend are starting to get depressed about this...^^
    Last edited by Coold0wn; 08-12-2011 at 03:46 AM.

  8. #28
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    lol! alright then.

    Make sure you have the following packages imported into your java program:
    Java Code:
    //for ImageIcon
    import javax.swing.ImageIcon;
    //for Image
    import java.awt.Image;
    //Dependencies for Image
    import java.awt.Toolkit;
    import java.net.URL;
    If you want to make things easier for youself you can just do:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    import java.awt.Toolkit;
    import java.net.URL;
    The java.awt package/class holds the Image stuff, and javax.swing hold swing elements like labels, buttons, and ImageIcons

    ImageIcon goes like this (My Favorite):

    Java Code:
    //Create the Image Icon and tell it where the image is
    ImageIcon newImage = new ImageIcon("http://www.java-forums.org/images/test.png");
    //Create a JLabel and append the newImage to it
    JLabel display = new JLabel(newImage);
    //... Here is where you add the JLabel to your container
    container.add(JLabel);
    see, this is very easy.

    Image goes like this
    I use this only when I need to. This function is very versatile but ImageIcon is much easier :
    Java Code:
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    URL url = getClass().getResource("http://www.java-forums.org/images/test.png");
    Image img = toolkit.getImage(url);
    ImageIcon finalpic = new ImageIcon(img);
    JLabel display = new JLabel(finalpic);
    And finally, the great drawImage function.
    Java Code:
    public void paintComponent(Graphics painter)
    {
         Toolkit toolkit = Toolkit.getDefaultToolkit();
         Image img = toolkit.getImage(url);
         //The first parameter is the image object, the second is the x-coord, third is y-coords, and the last is not important for you because you are a bit new to java.
         painter.drawImage(img, 0, 0, this);
    }
    You will have to learn BufferedImage from someone else because I have not need to use BufferedImage for anything pratactical in java, thus I rarely use it, thus I would be a bad teacher on BufferedImage.
    Last edited by kammce; 08-12-2011 at 04:07 AM. Reason: Made some edits and added a section for BufferedImage

  9. #29
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    Do you want me to give you an example java program? So you can download it and the source and see for yourself. It should only take me about a minute to make the whole program.

    Also, You might want to take this a little slower because learning Java is like learning how to breakdance. Go to fast and you actually push yourself to far and end up taking step back besides progressing. The worst part about rushing to learn a programming language is getting discouraged which I can see here.


  10. #30
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    yay thanks :)
    but am i just to stupid or did you again get parameter with graphics g, called it graphics painter, didnt use it anywhere and did not define g/painter anywhere? ^^
    ...


    edit:
    im going too fast consciously.
    ive learned java for 2 years at school.. but this is too slow for me... after two years, my great project was a snake game...
    it wasnt THAT easy .. but .. my project now is much more interesting..
    (i got 15 of 15 points for the project :P)
    in school , we learned like.. objects , classes at first , then integers, then methods , than some more interesting stuff like arrays and then we did the project..
    we dont get into it very much^^
    Last edited by Coold0wn; 08-12-2011 at 04:21 AM.

  11. #31
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    Do you understand how functions work?

    Yes I named Graphics painter. Most people name it 'g' because it makes things much simpler when programming. You could name it swissCheese, armpit, hotChick, blood, or whatever. It is the name of the object that was created when I created the paintComponent function.

  12. #32
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    i know... i just wanted to refer to you as one of all the guys, that do the exact same thing, just in green :P
    why do we even have that method?

  13. #33
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    It services its purpose. There are times when I have to use all of those methods in order to get my desired effect... And you are right, I think you might have been going too slow. You need to have more fun with it. I thought you were a mere script kiddy who was trying his hand at programming games in java. Sorry about that.

  14. #34
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    yeah.. i understand that.. im always a bit on fire if sth doesnt work... xD
    but since i want to study Programming, i have to go get education^^

    so in this example we dont even need the method?
    ... ill just try it, i went to bed yesterday because it was too early in the morning.. :P

  15. #35
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    still not working... i think im gonna cry when i finally get some images on the screen -.-
    Java Code:
    [import javax.swing.ImageIcon;
    import java.awt.Image;
    import java.awt.Toolkit;
    import javax.swing.*;
    
        public class Tile extends JFrame    
    {
            private JFrame MainFrame;
            private Editor editor;
            public Tile(Editor e, JFrame Frame)
            {
                //Create the Image Icon and tell it where the image is
                ImageIcon newImage = new ImageIcon("http://www.java-forums.org/images/test.png");
                //Create a JLabel and append the newImage to it
                JLabel display = new JLabel(newImage);
                //... Here is where you add the JLabel to your container
                editor = e;
                MainFrame = Frame;
                MainFrame.getContentPane().add(display);
            }
            
    }

    class editor where a tile should be build:
    Java Code:
            DrawFrame = new JFrame("Draw");
            DrawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            Tile tile = new Tile(this, DrawFrame);
            
            DrawFrame.setSize(1024, 768);
            DrawFrame.setVisible(true);
            DrawFrame.setResizable(false);
    Last edited by Coold0wn; 08-12-2011 at 02:55 PM.

  16. #36
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    You can not get an image from a website with out doing some extra things. Start simple. Download the image and point the ImageIcon to the downloaded image. Here is how you get an image from a website. And I will throw in a Function I use to easily get any picture I want.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.net.URL;
    import java.awt.Toolkit;
    
    public class ImageTest extends JFrame
    {
    	ImageIcon img = importImage("http://www.sosymbol.com/media//symbols/rabi/web2/rrect/black/128x128.png");
    	JLabel display = new JLabel(img);
    	JPanel container = new JPanel(new FlowLayout());
    
    	public ImageTest()
    	{
    		super("Image Test"); 
    		setSize(200, 200);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    		Container contentArea = getContentPane();
         	contentArea.setBackground(Color.white);
    		container.add(display);
         	contentArea.add(container);
         	setContentPane(contentArea);
    	}
    	public ImageIcon importImage(String getURL) 
    	{
    		Toolkit toolkit = Toolkit.getDefaultToolkit();
    		URL url; ImageIcon pic = new ImageIcon();;
    		try {
    			url = new URL(getURL);
    			Image getPic = toolkit.getImage(url);
    			pic = new ImageIcon(getPic);
    		} catch (java.net.MalformedURLException e) { System.out.println("The URL is wrong."); }
    		return pic;
    	}
    	public static void main(String[] args)
    	{
    		ImageTest run = new ImageTest();
    	}
    }
    This works 100%. Compile, Run, and wait. It will take a while because the program needs to download the file but it still does not take that long.

  17. #37
    Coold0wn is offline Member
    Join Date
    Aug 2011
    Posts
    71
    Rep Power
    0

    Default

    yes! yours is working! THANKS!
    i tried my code with a path to an existing image too, but it didnt work neither, so i changed it back..
    i have some questions still:
    1) contentArea.setBackground(Color.white) - can i put an image in there too somehow? and maybe repeat it? so i have a 32x32 image repeated until JFrame window ends
    2) Do i have to work with a JLabel? or can i just add the images to the JFrame? I would like to not have a Layout , but put the images manually in there.
    3) how can i put the image at a specific place? is there a setBounds method for ImageIcons?

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    is there a setBounds method for ImageIcons?
    For this type of info, the API doc is the place to go: Java Platform SE 6

    GUI is built by adding an object that extends the Component class to an object that extends Container class.
    Many of the objects you are referring to are not Components which means that they can not directly be added to a Container.

  19. #39
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    can i put an image in there too somehow? and maybe repeat it? so i have a 32x32 image repeated until JFrame window ends
    See the API for TexturePaint and the Oracle Java2D tutorial.

    db

  20. #40
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    1) No you cannot put an image into the background using setBackground. You should really visit this website. It is awesome! Its called Google and they have everything there!
    2) You will adding the images to a JLabel and then adding those Images to a JPanel. You have no good reason to try to add the image to JFrame. That would be getting way to complex for you if you start tampering with JFrame!
    3) Yes you can. Look up Java Layouts. With a no Layout (My favorite layout for games) You can set the coordinates, z-index (if you do not know what that is, it layers all of the components you have on your JFrame), and size with it.

    check this out
    Doing Without a Layout Manager (Absolute Positioning) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    Lastly, whats wrong with JLabels? I keep my characters for my games encased in JLabels.

Page 2 of 5 FirstFirst 1234 ... LastLast

Similar Threads

  1. where the stuff goes?
    By Izabela in forum New To Java
    Replies: 16
    Last Post: 08-31-2010, 02:00 PM
  2. Replies: 6
    Last Post: 05-26-2010, 12:17 AM
  3. Table, beginner stuff
    By drwk in forum New To Java
    Replies: 1
    Last Post: 01-24-2010, 07:19 PM
  4. Simple Stuff 0.1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-19-2008, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •