Page 2 of 2 FirstFirst 12
Results 21 to 39 of 39
Like Tree1Likes

Thread: Setting x and y for String

  1. #21
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Okay here's the code so far:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class gameWorld extends JFrame {
    
    	public static void main(String[] args) {
    		new gameWorld();
    		
    	}
    	gameWorld() {
    		setLayout(new FlowLayout());
    		JLabel jlbConsole = new JLabel("You have spawned!");
    		jlbConsole.setHorizontalAlignment(JLabel.LEFT);
    		jlbConsole.setVerticalAlignment(JLabel.TOP);
    				
    		ImageIcon worldMap = new ImageIcon("MyCoreWorld.jpg");
    		JLabel jlbWorld = new JLabel(worldMap);
    		jlbWorld.setHorizontalAlignment(JLabel.CENTER);
    		jlbWorld.setVerticalAlignment(JLabel.CENTER);
    		add(jlbConsole);
    		add(jlbWorld);
    		this.setSize(700, 700);
    		// pack();
    		setVisible(true);
    		
    	}
    
    }
    But nothings happening

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Setting x and y for String

    nothings happening
    Please explain.
    What happens when you compile and execute the code? What do you expect to happen?
    If you don't understand my response, don't ignore it, ask a question.

  3. #23
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    i expect that the image comes up but only the text comes

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Setting x and y for String

    I get both:
    Attached Thumbnails Attached Thumbnails Setting x and y for String-gameworld.png  
    Last edited by Norm; 02-16-2014 at 07:16 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    maybe it's because the image is 600x600? Ps. do you the the both with the same code

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Setting x and y for String

    Try the code with different sized images.

    Is the code reading the image file? Is the image file in the right folder?
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Is the code reading the image file?
    I don't what you mean by that

    Is the image file in the right folder?
    It's on my computers Pictures folder.

    Try the code with different sized images.
    Okay i'll try that.

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Setting x and y for String

    Statement #16 reads the image file and uses its contents to create an ImageIcon object. The ImageIcon class doesn't throw an exception if the image file is not found. You can quickly test that by making a unique filename by adding "XXX" to the filename and executing the program.

    It's on my computers Pictures folder.
    How does the program know where the image file is located? Is the class file in the Pictures folder with the image file?
    If you don't understand my response, don't ignore it, ask a question.

  9. #29
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Okay i got the image showing up (i just placed the image in the same folder with class but the image is not showing where a wan it to show, it's showing up in the right side of the
    text "You have Spawned" and i want it to show up beneath the text.

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Setting x and y for String

    Time to read up on how to use layout managers:
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    They are what control where components are sized and positioned in a GUI.
    If you don't understand my response, don't ignore it, ask a question.

  11. #31
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Okay i got it working (kinda) but now the text isn't showing up

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class gameWorld extends JFrame {
    
    	public static void main(String[] args) {
    		new gameWorld();
    		
    	}
    	gameWorld() {
    		setLayout(new BorderLayout());
    		JLabel jlbConsole = new JLabel("You have spawned!");				
    		ImageIcon worldMap = new ImageIcon("MyCoreWorld.jpg");
    		JLabel jlbWorld = new JLabel(worldMap);
    		add(jlbConsole, BorderLayout.NORTH);
    		add(jlbConsole, BorderLayout.CENTER);
    		add(jlbWorld, BorderLayout.CENTER);
    		add(jlbWorld, BorderLayout.SOUTH);
    		this.setSize(700, 700);
    		// pack();
    		setVisible(true);
    		
    	}
    
    }
    Ps. I Changed the layout to border
    Last edited by NitroDev; 02-17-2014 at 07:27 PM.

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Setting x and y for String

    That code looks like there was too much copy and paste. Remove some of the duplications and try it again.
    If you don't understand my response, don't ignore it, ask a question.

  13. #33
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Now it works THANKS but now i have a question; first of allwhat does the void do in java and is it possible to add a image on top of one like a player image on top of my worldMap image?
    Last edited by NitroDev; 02-17-2014 at 08:18 PM.

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Setting x and y for String

    add a image on top of one like a player image on top of my worldMap image?
    That is often down by using custom code in the paintComponent() method. Draw the background first and then draw the smaller image on top.
    If you don't understand my response, don't ignore it, ask a question.

  15. #35
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Okay and now is there a way to make the image to to the same java window

  16. #36
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Setting x and y for String

    Probably, but I have no idea what you are asking. Can you give more details?
    If you don't understand my response, don't ignore it, ask a question.

  17. #37
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    never mind

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Setting x and y for String

    If you can't explain your problem to someone else, you probably don't understand it yourself.
    gimbal2 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  19. #39
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Setting x and y for String

    Nah i just figured out whats the easiest way to fix my problem so :P

    But now i made the code bigger and i have another problem so here's the code so far:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class gameWorld extends JFrame {
    
    	public static void main(String[] args) {
    		new gameWorld();
    		
    	}
    	gameWorld() {
    		setLayout(new BorderLayout());
    		JLabel jlbConsole = new JLabel("You have spawned!");				
    		ImageIcon worldMap = new ImageIcon("MyCoreWorld.jpg");
    		JLabel jlbWorld = new JLabel(worldMap);
    		ImageIcon player = new ImageIcon("MCOlongneckRace.png");
    		JLabel jlbPlayer = new JLabel(player);
    		add(jlbPlayer, BorderLayout.SOUTH);
    		add(jlbConsole, BorderLayout.NORTH);
    		add(jlbWorld, BorderLayout.SOUTH);
    		this.setSize(700, 700);
    		// pack();
    		setVisible(true);
    		
    	}
    
    }
    I have made the MCOlongneckRace.png a transparent background image with MS Paint but it doesn't show up on top of MyCoreWorld.jpg
    Last edited by NitroDev; 02-17-2014 at 11:07 PM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 1
    Last Post: 06-29-2011, 06:46 PM
  2. Help, Setting Key Code
    By quafflepunch in forum New To Java
    Replies: 2
    Last Post: 01-05-2011, 02:37 AM
  3. Setting up a String Array
    By AJArmstron@aol.com in forum New To Java
    Replies: 12
    Last Post: 04-16-2010, 01:42 PM
  4. Setting the DSN
    By tim in forum JDBC
    Replies: 1
    Last Post: 02-14-2008, 10:55 PM
  5. Help with setting up please
    By BlitzA in forum New To Java
    Replies: 6
    Last Post: 12-29-2007, 01:54 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
  •