Results 1 to 10 of 10
Thread: Background image
- 03-07-2009, 08:40 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
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.
-
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
- 03-07-2009, 11:54 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
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
- 03-08-2009, 12:11 AM #4
Member
- Join Date
- Mar 2009
- Posts
- 15
- Rep Power
- 0
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); } }
- 03-08-2009, 01:24 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
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?
-
- 03-08-2009, 02:19 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
I'm only using the Graphics methods (drawOval, fillOval, Color etc) that comes with awt and displaying with awt.
- 03-08-2009, 08:05 AM #8
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)
- 03-08-2009, 08:08 AM #9
Member
- Join Date
- Mar 2009
- Posts
- 15
- Rep Power
- 0
first:
and then:Java Code:JButton bt1 = new JButton( "Button1" ); bt1.addActionListener(new ButtonListener()); add( bt1 );
Java Code:class ButtonListener implements ActionListener { ButtonListener() { } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Button1")) { //place some stuff here... }
- 03-08-2009, 05:49 PM #10
Member
- Join Date
- Jan 2009
- Posts
- 31
- Rep Power
- 0
^^^ 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
-
Background Image of Applet
By BangJava in forum Java AppletsReplies: 8Last Post: 11-24-2010, 05:48 AM -
how i can put image in background of frame??
By ahmed13 in forum NetBeansReplies: 4Last Post: 01-15-2009, 05:47 PM -
Image as background
By Java.child in forum AWT / SwingReplies: 2Last Post: 10-02-2008, 11:37 PM -
Background image in java
By toby in forum AWT / SwingReplies: 1Last Post: 07-29-2007, 07:15 AM -
Why this image background is black ?
By samson in forum Java 2DReplies: 1Last Post: 07-17-2007, 04:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks