Results 1 to 10 of 10
  1. #1
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default Background image

    Is it possible to use an image (jpg, png, etc) as a background for a program? I am making a mini game where an alien I drew out using simple java graphics moves around by pressing keys. Currently i only have the alien and the methods for movement. I want to have a background to make it less plain.

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

    Default

    If you are talking about a Swing application (and you should include this information in the initial post), then have a look at this link that explains how to use a background image on a JPanel:

    JavaRanch - Background Image On J Panel

  3. #3
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    no swing is involved in this program, I'm using just the graphics and color variable. This is the code I used to draw the alien.
    Java Code:
    /* the fields of the object defined here as static to make the
        interface as simple as possible to start*/
        static int x = 100, y = 100;
        static int size = 50;
        static Color clr = Color.green;
        static boolean showing = false;
        // paints Zorx on the screen Note this method is not part of Interface
        static void paint (Graphics g)
        {
            g.setColor (clr);
            g.fillRect (x, y, size / 4, size);
            g.fillOval (x - (3 * size / 8), y - (size / 2), size, size / 2);
            g.drawLine (x - 3 * size / 8, y + size, x - 3 * size / 8 + size, y + size);
            g.drawLine (x, y + size / 2, x - size, y);
            g.drawLine (x - size, y, x - size, y - size / 4);
            g.drawOval (x - size - size / 8, y - size / 4 - size / 8, size / 8, size / 8);
            g.setXORMode (Color.white);
            g.fillOval (x - size / 4, y - size / 3, size / 12, size / 12);
            g.fillOval (x + size / 4, y - size / 3, size / 12, size / 12);
            g.setPaintMode ();
        } // end of paint method

  4. #4
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default

    This is also a workaround for your question, I found it long time ago somewhere on the sun website ( I think...)
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
     
    public class BackgroundImage extends JFrame
    {
    	JScrollPane scrollPane;
    	ImageIcon icon;
     
    	public BackgroundImage()
    	{
    		icon = new ImageIcon("yourbackground.jpg");
     
    		JPanel panel = new JPanel()
    		{
    			public void paintComponent(Graphics g)
    			{
    				g.drawImage(icon.getImage(), 0, 0, null);
    				setOpaque( false );
    				super.paintComponent(g);
    			}
    		}; 
    		scrollPane = new JScrollPane( panel );
    		setContentPane( scrollPane );
    	}
     
    	public static void main(String [] args)
    	{
    		BackgroundImage frame = new BackgroundImage();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(300, 300);
    		frame.setVisible(true);
    	}
    }

  5. #5
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    so you're saying just to create a JFrame and run the whole program in it? Another question that doesn't have to do with this one but its part of same program. I'm trying to add a key listener to the program by "addKeyListener(this);" However, it gives me this error:
    no applicable overload for method named "addKeyListener" was found in java.awt.Components. Perhaps you wanted the overloaded version "void addKeyListener (java.awt.event.KeyListener $1);" instead?

  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 leiferouis View Post
    no swing is involved in this program, I'm using just the graphics and color variable.
    Then what are you displaying it with? AWT? Your quote doesn't make much sense, to be honest.

  7. #7
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    I'm only using the Graphics methods (drawOval, fillOval, Color etc) that comes with awt and displaying with awt.

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you will need to use drawImage() from Graphics. but then you will need to pass in an Image. how to do that with awt alone, i have no idea.

    i use ImageIcon from swing. take a look at blackstormattack's code for more ideas (on swing).
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default

    first:
    Java Code:
            JButton bt1 = new JButton( "Button1" );
            bt1.addActionListener(new ButtonListener());
            add( bt1 );
    and then:
    Java Code:
    class ButtonListener implements ActionListener {
      ButtonListener() {
      }
    
      public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Button1")) 
        {
          //place some stuff here...
          
        }

  10. #10
    leiferouis is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default

    ^^^ isn't that for button listeners? My problem doesn't have anything to do with buttons, I'm not using any buttons for this program. I'm having trouble with keyListener and using a background image.

    As for the background image, can I just use drawImage then draw the rest on top of it? If having a background image in awt is too much trouble I think i will just make a loop to fill the background with white dots to make it seem like space. Only problem with that is Im afraid that the white dots will overlap with the alien.
    Last edited by leiferouis; 03-08-2009 at 05:54 PM.

Similar Threads

  1. Background Image of Applet
    By BangJava in forum Java Applets
    Replies: 8
    Last Post: 11-24-2010, 05:48 AM
  2. how i can put image in background of frame??
    By ahmed13 in forum NetBeans
    Replies: 4
    Last Post: 01-15-2009, 05:47 PM
  3. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2008, 11:37 PM
  4. Background image in java
    By toby in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 07:15 AM
  5. Why this image background is black ?
    By samson in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:24 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
  •