Results 1 to 13 of 13
  1. #1
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default How to display Images?

    Hello,

    I am building a car game currently and I am having troubles placing images.

    Game initilises

    Java Code:
    	mainScreen = new JFrame("Kario Mart");
    		mainScreen.getContentPane().add(new javax.swing.JLabel(new javax.swing.ImageIcon("alien.png")));
    		mainScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		mainScreen.setSize(Game.xDim,Game.yDim + 60);	
    		intro = new Intro();	
    		screen = new GameComponent();
    		mainScreen.getContentPane().add(screen);	
    		mainScreen.setVisible(true);
    Then the intro starts
    Java Code:
    	clear();
    		Graphics2D g2 = (Graphics2D) g;	
    
    		// Flashing text, based on arcade machines, could possibly be annoying
    		if (counter >= 20)
    		{
    			
    			g2.setColor(Color.black);
    			g2.scale(2, 2);
    			g2.drawString("Spaces goes fowards, Backspace goes backwards", 79, 340);	
    			g2.scale(0.5, 0.5);			
    
    			
    		}
    		
    
    			
    		id = new ImageDisplay();
    		
    		id.draw(KarioMart.game, KarioMart.screen);
    		
    		
    
    		if (stage <= 1)
    		{
    ;
    			g2.drawImage(new ImageIcon("alien.png").getImage(), 0, 0, null);
    			paint(g);
    			
    			
    		}
    And no image is drawn, I would like to know which methods I should use to place/draw an image onto a frame, using g.drawImage or jframe.add(comp) or something.

    I have looked up 10 different tutorials and I just feel so stupid that I can't get this one thing right.

    Are there any limitations on images being displayed? Anything I have forgotten? Thanks,

    Rectal Exambot

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default

    Most drawing of images is down in a JPanel. Extend the JPanel class and override its paintComponent method and add your drawImage method call there.
    Add the class to the container where you want the image to show.
    You should get the image one time outside of the paintComponent method, not every time you draw it.

    For sample code, Search on the forum for paintComponent.

  3. #3
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    
    public class ImageDisplay extends JPanel
    {	
    	int x = 0, y = 0;
    	ImageIcon logo;
    	Image img;
    	
    	public ImageDisplay()
    	{
    		logo = new ImageIcon("alien.png");
    		img = logo.getImage();		
    	}
    	public void paintComponent(Graphics g)
    	{		
    		Graphics2D g2 = (Graphics2D) g;
    		g2.drawImage(img, 10, 10, null);		
    	}
    }
    id = new ImageDisplay();
    Screen.getContentPane().add(id);
    id.paintComponent(screen.getGraphics());

    and then id.repaint(); is recalled, is this in the correct order? What do I need to change? Yes I am looking up tutorials
    Last edited by Rectal Exambot; 10-08-2010 at 03:57 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default

    That looks about right. Have you written a small test program to see if it works.

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

    Default

    Never never never use getGraphics of a Component. That way lies grief.

    Never never never attempt to invoke paintComponent from your code. Call repaint() and let the RepaintManager look after the rest.

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Never never never use getGraphics of a Component. That way lies grief.
    With some exceptions -- as Darryl knows but likely wants to keep his answer succinct and avoid confusing the OP. But for the benefit of lurkers who may be into advanced graphics techniques, the exceptions I refer to are best summarized in the highly recommended book "Filthy Rich Clients" by Haase and Guy

  7. #7
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    That looks about right. Have you written a small test program to see if it works.
    Sort of, I added it to my old program and no image shows and I am unsure as what order and ill just go through everything just for clarification. It currently doesn't work.

    This was my first ever game, and I had been working on it for a while for an assignment, and just implementing new features but this is how it works.
    Java Code:
    public static void main(String[] args) 
    {
    	mainScreen = new JFrame("Kario Mart");		
    	raceGame = new KarioMart();
    	raceGame.introScreen();
    		
    }
    Java Code:
    public KarioMart()
    	{
    		
    		mainScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		mainScreen.setSize(Game.xDim,Game.yDim + 60);	
    		intro = new Intro();	
    		id = new ImageDisplay();
    		screen = new GameComponent();
    		mainScreen.getContentPane().add(screen);
    		mainScreen.setVisible(true);
    
    	}
    Java Code:
    public class GameComponent extends JComponent 
    {
    	
    	BufferedImage buffI;	
    
    	public GameComponent() 
    	{
    
    		this.setFocusable(true);
    		setPreferredSize(new Dimension((int) Game.xDim,(int) Game.yDim + 60));
    		buffI = new BufferedImage((int) Game.xDim,(int) Game.yDim + 60, 
    										BufferedImage.TYPE_INT_ARGB);
    	}
    		
    	public void paint(Graphics g) 
    	{
    		g.drawImage(buffI, 0, 0, null);
    	}
    
    	public void clear() 
    	{
    		// TODO Auto-generated method stub
    		Graphics g = buffI.getGraphics();
    		g.setColor(new Color(1.0f,1.0f,1.0f));
    		g.fillRect(0, 0, (int) Game.xDim + 100, (int) Game.yDim + 100);
    		
    	}
    	
    	public Graphics2D getGraphics2D() 
    	{
    		// TODO Auto-generated method stub
    		return buffI.createGraphics();
    	}
    
    	public void drawImage(Image image, int i, int j) 
    	{
    		// TODO Auto-generated method stub
    		
    		Graphics g = buffI.getGraphics();
    		g.drawImage(image, i, j, null);
    	}
    
    }
    public class Intro extends JComponent implements KeyListener

    The intro handles keypresses during the intro, allows drawing of text and the selection of tracks/vehicles.

    The main problems I have been having:

    What adds what? Do I add the ImageDisplay to the frame? Or to the GameComponent? Where should I recall the repaint().

    If I want to get graphics how do I do so without using the getGraphics? Should it be the Frame.getGraphics()?


    (Sorry if the tutorial answered any questions asked, but I am off to bed, I will look at it tommorrow)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default

    If I want to get graphics
    For what context do you want a Graphics object . The paintComponent method is passed a Graphics object for the GUI that you are to use in that method.
    For the buffered image that you are building, you currently get a graphics object for it so you can draw on it.

    Call repaint() when you have made a change to the variables that control what is displayed on the GUI and want the JVM to call the paintComponent method so that those changes can be drawn.

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

    Default

    Quote Originally Posted by Fubarable View Post
    With some exceptions -- as Darryl knows
    Actually, I don't.
    but likely wants to keep his answer succinct and avoid confusing the OP.
    So much for that :rolleyes: ;)
    But for the benefit of lurkers who may be into advanced graphics techniques, the exceptions I refer to are best summarized in the highly recommended book "Filthy Rich Clients" by Haase and Guy
    Ah. I don't have nor have read any books, my learning has been from the tutorials and on the forums.

    db

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Actually, I don't.
    I had assumed that you had read it since you know so dang much about graphics. What the text states is pretty much what you do but specifically that you do not want to call getGraphics on a component for rendering purposes, but that you may sometimes call the method to query rendering attributes. You also may sometimes want to override a JComponent's paint method rather than paintComonent when you "want to alter the graphics state for all of the taht component's rendering". They then give a great example with a translucent button.

    Great, great book and highly recommended!

  11. #11
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Okay, messing around a little, but how do I add an image? I currently just have an alien.png file inside the source, I can get text to display but not the alien, any ideas?

    I have commented out all the other stuff but I keep getting this:

    javax.imageio.IIOException: Can't read input file!
    javax.imageio.IIOException: Can't read input file!

    I have tried PNGs and JPEGS and can't seem to find where to store the images

    Edit: I am using Eclipse, apparently I need to use the Jar File if I want to see the image... now I am mad
    Last edited by Rectal Exambot; 10-11-2010 at 04:15 AM.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If your images are in a Jar file then you can't retrieve the images as files but rather as resources. Note that ImageIO.read() has an overload that accepts an InputStream as a parameter, and you can get your BufferedImage that way.

  13. #13
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    I tried running the game and no images were showing, I then exported as a runnable Jar and everything works as I would hope for. Images and everything I wanted are showing up.

Similar Threads

  1. JavaHelp how to display images
    By fossildoc in forum New To Java
    Replies: 0
    Last Post: 04-04-2010, 06:42 AM
  2. How to display Images from MS access using JSP
    By redmaverick in forum JDBC
    Replies: 3
    Last Post: 11-30-2009, 09:28 AM
  3. display images
    By prof.deedee in forum AWT / Swing
    Replies: 10
    Last Post: 11-12-2009, 09:08 PM
  4. Dynamic display of images
    By gixerino in forum NetBeans
    Replies: 7
    Last Post: 01-15-2009, 02:55 PM
  5. Replies: 0
    Last Post: 03-25-2008, 12:45 AM

Posting Permissions

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