Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Need help adding background image (noob here)

  1. #1
    Junior Member OpX316's Avatar
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help adding background image (noob here)

    Hello fine sirs. I was wondering how a gentlemen like myself would go upon adding a background image into my Java GUI program. I have absolutely no clue as to where I should start


  2. #2
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    Well, there are a couple of places that you could start. What do you have so far?

  3. #3
    Junior Member OpX316's Avatar
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    I don't have any progress on the background image however the majority of my program is finished.

    Here it is:
    Removed due to one of the "helpers" only having one damn post. Don't want kids to steal my hard work. I guess I'll find help elsewhere.
    Last edited by OpX316; June 27th, 2011 at 04:04 PM.

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    I am going to assume that GBFrame is extending JFrame. Correct me if I am wrong.

    Take a look at Container (Java 2 Platform SE v1.4.2) This is the container class which contains the all-powerful paint() function.

    paint(Graphics g) is passed a graphics object.
    See Graphics (Java 2 Platform SE v1.4.2)

    Notice that the second link lists a ton of methods for the Graphics object. I'm sure there is one in there for drawing an image

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    Here it is:
    Removed due to one of the "helpers" only having one damn post. Don't want kids to steal my hard work. I guess I'll find help elsewhere.[/QUOTE]

    Whoa. Hey now. Don't be making assumptions. Although I made this account today, I have been programming in Java for quite some time.

  6. #6
    Junior Member OpX316's Avatar
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    Quote Originally Posted by Jams View Post
    I am going to assume that GBFrame is extending JFrame. Correct me if I am wrong.

    Take a look at Container (Java 2 Platform SE v1.4.2) This is the container class which contains the all-powerful paint() function.

    paint(Graphics g) is passed a graphics object.
    See Graphics (Java 2 Platform SE v1.4.2)

    Notice that the second link lists a ton of methods for the Graphics object. I'm sure there is one in there for drawing an image
    Thank you. I don't know if it is an extension or not. I'll be sure to try out the Container.

    Edit: Sorry to offend but you made the account today D: My defense mode tends to go up when stuff like that happens.

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    No offense taken.

    Let me know if you need further help.

  8. #8
    Junior Member OpX316's Avatar
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    Quote Originally Posted by Jams View Post
    No offense taken.

    Let me know if you need further help.
    Okay I am a bit confused to where I would put code like this
    public abstract boolean drawImage(Image img,
                                      int x,
                                      int y,
                                      int width,
                                      int height,
                                      ImageObserver observer)

  9. #9
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    Do you remember the paint() function?

    In the end it should look something like:

    paint (Graphics g)
    {
    g.drawImage(parameters)
    }

    You should just be able to use null in place of the image observer. Other wise, x and y are the location of the upper right hand corner of the image. Width and Height are just the dimensions of the image.

  10. #10
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Need help adding background image (noob here)

    What's the problem with receiving replies from people who have only just joined? Experienced Java programmers join here every day. The amount of posts/time joined doesn't dictate the skill level.

    If you want quality replies then you need to keep your code posted for us all to see. Thanks for your help Jams.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  11. #11
    Junior Member OpX316's Avatar
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    Quote Originally Posted by JavaPF View Post
    What's the problem with receiving replies from people who have only just joined? Experienced Java programmers join here every day. The amount of posts/time joined doesn't dictate the skill level.

    If you want quality replies then you need to keep your code posted for us all to see. Thanks for your help Jams.
    Fine sir. Here is my code. I still haven't found a way that works. I am definitely doing something wrong.

    import java.awt.*;    
    import BreezyGUI.*;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.awt.Graphics;
    import java.awt.Image;
    //import java.net.InetAddress;
    //import java.net.UnknownHostException;
    //import java.io.IOException;
     
    public class Mainwin extends GBFrame 
    {
     
       	private Label iplabel;
       	private Label statuslabel;
       	private Label bsizelabel;
       	private Label dossetlabel;
       	private Label firstoplabel;
       	private Label ncountlabel;
       	private Label secondoplabel;
     
       	private TextField ipfield;
       	private TextField response;
       	private TextField response2;
       	private TextField firstopfield;
       	private TextField secondopfield;
       	private TextField ncountfield;
       	private TextField bsizefield;
     
       	private Button pingem;
       	private Button slapem;
       	private Button clear;
       	private Button exit;
       	private Button defaultdos;
     
       	Mainwin ()
       	{	
       		iplabel = addLabel ("IP / URL:",1,1,2,1);
       		statuslabel = addLabel ("Status:",3,1,1,1);
       		dossetlabel = addLabel ("Denial of Service Settings:",5,1,2,1);
       		bsizelabel = addLabel ("Size of buffer packet:",9,1,2,1);
       		firstoplabel = addLabel ("First option (t,a):",6,1,2,1);
       		secondoplabel = addLabel ("Second option (t,a):",7,1,2,1);
       		ncountlabel = addLabel ("Number of echo requests:",8,1,2,1);
     
       		ipfield = addTextField ("",1,2,2,1);
       		response = addTextField ("",3,2,2,1);
       		response2 = addTextField ("",4,2,2,1);
       		firstopfield = addTextField ("",6,3,1,1);
       		secondopfield = addTextField ("",7,3,1,1);
       		ncountfield = addTextField ("",8,3,2,1);
       		bsizefield = addTextField ("",9,3,2,1);
     
       		pingem = addButton ("Ping em!",2,1,1,1);
       		slapem = addButton ("DoS em!",10,1,1,1);
       		defaultdos = addButton ("Recommended DoS Settings",10,2,1,1);
       		clear = addButton ("Clear all fields",2,2,1,1);
       		exit = addButton ("Exit",10,4,1,1);
       	}
     
       	public void buttonClicked(Button buttonObj)
       	{
       		String ip,firstop,secondop,ncount,bsize;
     
       		if (buttonObj == pingem)
       		{
       		 response.setText("");
       		 response2.setText("");
     
             try
             {
            	 String[] command =  new String[4];
            	 ip = ipfield.getText();
                 String ping = ip;
            	 command[0] = "cmd";
            	 command[1] = "/C";
            	 command[2] = "ping";
            	 command[3] = ping;
     
            	 Process p = Runtime.getRuntime().exec(command);
     
            	 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
     
            	 BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
     
     
             	String s = null;
             	while ((s = stdInput.readLine()) != null) {
             		response.setText(s);
             	}
     
             	while ((s = stdError.readLine()) != null) {
             		response.setText(s);
             	}
             	response2.setText("Host is alive. Ping successful.");
             }
     
     
             catch(Exception e){  
            	 		response2.setText("Fatal Error! Please clear all values now!");
    			 	}
       		}	
     
     
       		 if (buttonObj == slapem)
     		 {
       			response.setText("");
       	   		response2.setText("");
       			ip = ipfield.getText();
       			firstop = firstopfield.getText();
       			secondop = secondopfield.getText();
       			bsize = bsizefield.getText();
       			ncount = ncountfield.getText();
     
     
       			try
                {
               	 String[] command = new String[8];
               	 ip = ipfield.getText();
                    String ping = ip;
               	 command[0] = "cmd";
               	 command[1] = "/C";
               	 command[2] = "ping";
               	 command[3] = "-" + firstop;
               	 command[4] = "-" + secondop;
               	 command[5] = ping;
               	 command[6] = "-l " + bsize;
               	 command[7] = "-n " + ncount;
     
               	 Process p = Runtime.getRuntime().exec(command);
     
               	 BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
     
               	 BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
     
     
     
                	String s = null;
                	while ((s = stdInput.readLine()) != null) {
                		response.setText(s);
     
                	}
     
     
                	while ((s = stdError.readLine()) != null) {
                		response.setText(s);
                	}
                	response2.setText("Host is alive and is being DOS'd.");
                }
     
     
                catch(Exception e){  
       					response2.setText("Fatal Error! Please clear all values now!");
       			 	}
     
     		 }
     
       		 if (buttonObj == clear)
       		 {
       			ipfield.setText("");
       			response.setText("");
       			response2.setText("");
       			ipfield.setText("");
       	   		response.setText("");
       	   		response2.setText("");
       	   		firstopfield.setText("");
       	   		ncountfield.setText("");
       	   		bsizefield.setText("");
       		 }
     
       		 if (buttonObj == defaultdos)
       		 {
       			firstopfield.setText("t");
       	   		ncountfield.setText("1000000");
       	   		bsizefield.setText("65500");
       		 }
     
       		 if (buttonObj == exit)
      		 {
       			System.exit(0);
      		 }
     
     
       	}
     
     
     
    }

  12. #12
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Need help adding background image (noob here)

    I don't see any code there that tries to set a background - no wonder it's not working!

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Need help adding background image (noob here)

    Quote Originally Posted by OpX316 View Post
    Removed due to one of the "helpers" only having one damn post. Don't want kids to steal my hard work. I guess I'll find help elsewhere.
    I don't want to be a jerk, but if you don't know how to paint a background, chances are your code doesn't really contain much "hard work" worth stealing- especially upon looking at all of the bad habits in the code you did post. Now's a good time to check your ego. If you're really that worried about people stealing your work, then hire a private tutor. You can maybe even find one in the Paid Jobs forum.

    But if you want to proceed, carefully read through this tutorial: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    And if you still have questions after that, post an SSCCE that demonstrates what you've done that you think should display a background, without any extra code, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Junior Member OpX316's Avatar
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    Quote Originally Posted by KevinWorkman View Post
    I don't want to be a jerk, but if you don't know how to paint a background, chances are your code doesn't really contain much "hard work" worth stealing- especially upon looking at all of the bad habits in the code you did post. Now's a good time to check your ego. If you're really that worried about people stealing your work, then hire a private tutor. You can maybe even find one in the Paid Jobs forum.

    But if you want to proceed, carefully read through this tutorial: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    And if you still have questions after that, post an SSCCE that demonstrates what you've done that you think should display a background, without any extra code, and we'll go from there.
    Well for a beginner who just learned some Java syntax 4-ish days ago, I'd say I have created something worthwhile on my level. As for my "ego" I don't have one. The reason I said that was because so many people tend to steal things on certain cracking forums so I didn't want to give anyone here the chance to do so either. So yes, I do agree with you that my code doesn't contain "hard work" but I am trying. I am new to Java. So instead of knocking me down and saying what I have produced is shit, you could do what not only the thread title says, but what is also a common theme in this forum, and help me out a little. My goal coming here wasn't to brag or insult people. I am sorry if my attitude has offended you but to often has things been taken from individuals who post things on forums like these(not this particular one but SIMILAR). Anyway, thanks for the link. I'll read that.

    Quote Originally Posted by dlorde View Post
    I don't see any code there that tries to set a background - no wonder it's not working!
    Yes, I was reposting what I removed earlier. I had no idea where to start, that is why I came here asking on this forum.

    EDIT: I would like to stress again that I am new to Java. Just as the title suggests. So please go easy on me gentlemen. I am fragile Here is an image that will surely warm the hearts and tickle the fancies
    Last edited by OpX316; June 28th, 2011 at 08:33 AM.

  15. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Need help adding background image (noob here)

    I did not say that your code was "crap", just that you didn't really have anything to worry about people stealing. It's a pretty common theme for people to be strangely protective of their code- people worry about obfuscation and anti-piracy before they even have any real code written, for example. But that kind of attitude comes off as rude or arrogant, not to mention making it impossible to help you. Think about it- you're asking us to give you code, examples, or tutorials (which for many of us took years to figure out, not four days) for free, in our spare time, but you refused to post anything in return. That's going to rub a lot of people the wrong way.

    My entire point is that you need to make it easier for people to help you- by checking the attitude at the door, by providing an SSCCE (if you were worried about people "stealing your code", why didn't you take out any of the extra stuff that had nothing to do with drawing a background?), and by being polite to the people who would have helped you had you followed the advice in the "how to ask smart questions" link in my signature.

    Read the links I gave you. Try something out. Post an SSCCE.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,266
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Need help adding background image (noob here)

    Removed due to one of the "helpers" only having one damn post. Don't want kids to steal my hard work. I guess I'll find help elsewhere.
    We do ask that you refrain from removing code once it is posted. There are reasons for this, the first of which is that doing so defeats the purpose of these forums, which is to help each other learn (imaging trying to search the forums for a solution to your problem, only to find all code stripped from posts - how can one use the vast resource of the archives in this manner?) An extension to that logic - doing so can be considered selfish, as the poster who removes code denies their contribution to helping others learn once their needs are met - this may not be the original intention, but it can be perceived as such (and thus not help your chances of receiving help in the future). Of course there are exceptions to these rules, but they are few and far between so we ask that you please do not delete code once it is posted.

  17. #17
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    Quote Originally Posted by JavaPF View Post

    If you want quality replies then you need to keep your code posted for us all to see. Thanks for your help Jams.
    Anytime. I am willing to help whenever I can.

    --Jams

  18. #18
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help adding background image (noob here)

    Hey dude I just solved this problem for myself. Here is a cutout from one of my programs. see what you can make of it.

    the code concerning your problem is in the (MUHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAH AHAHA) section.

    Basically I made a JFrame then added a background panel(1) and set its layout to "OverlayLayout". then created a second panel(2) where I loaded the background image. lastly I made a panel(3) that would contain all of my components. I then added 2 and 3 to 1. and voila.

    PS there is a little problem concerning the buttons and Labels disapearing. Im following it up and as soon as I have the solution ill let you know.

    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;

    public class HomeScreen extends JPanel
    {
    //public variables
    JFrame home;
    ImageIcon icon;

    public HomeScreen()
    throws IOException
    {
    initframe();
    components();
    }

    void initframe()
    throws IOException
    {
    home = new JFrame();
    home.setSize(600,400);
    home.setTitle("Program");
    home.setVisible(true);
    home.setResizable(true);
    home.setLocationRelativeTo(null);
    home.setDefaultCloseOperation(javax.swing.WindowCo nstants.DISPOSE_ON_CLOSE);

    }

    void components()
    throws IOException
    {


    //panels & setting the background (MUHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAH AHAHA)
    JPanel backg = new JPanel();
    LayoutManager layout = new OverlayLayout(backg);
    backg.setLayout(layout);
    home.add(backg);

    JPanel backimage = new JPanel();
    ImageIcon image = new ImageIcon("saflag.jpg");
    backimage.add(new JLabel(image));
    backg.add(backimage);

    JPanel pcomp = new JPanel();
    pcomp.setLayout(new GridLayout(2,2,5,5));
    home.add(backg);
    backg.add(pcomp);

    //images and rescaling
    ImageIcon icon1 = new ImageIcon("ENTER ANY IMAGE HERE");
    Image image1 = icon1.getImage().getScaledInstance(200, 100, Image.SCALE_SMOOTH);
    icon1.setImage(image1);

    ImageIcon icon2 = new ImageIcon("ENTER ANY IMAGE HERE");
    Image image2 = icon2.getImage().getScaledInstance(200, 100, Image.SCALE_SMOOTH);
    icon2.setImage(image2);

    // labels
    JLabel lab1 = new JLabel(" ARE YOU SOUTH AFRICAN?");
    lab1.setBounds(0,0,200,40);
    pcomp.add(lab1);

    JLabel lab2 = new JLabel(" ARE YOU ENGLISH?");
    lab2.setBounds(0,0,200,40);
    pcomp.add(lab2);
    //buttons
    JButton b1 = new JButton(icon1);
    b1.setBounds(0,0,500,300);
    pcomp.add(b1);

    JButton b2 = new JButton(icon2);
    b2.setBounds(0,0,300,300);
    pcomp.add(b2);
    }
    public static void main(String[] args)
    throws IOException
    {
    new HomeScreen();
    }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  19. #19
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Need help adding background image (noob here)

    Quote Originally Posted by Pydra View Post
    PS there is a little problem concerning the buttons and Labels disapearing. Im following it up and as soon as I have the solution ill let you know.
    That's not exactly a "little" problem. Your approach is much more complicated than it should be, and it's not surprising at all that you're having problems. Please follow the advice I gave you in your other thread. I appreciate you trying to help, but this is the wrong approach.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Is it possible to make a background to in image transparent
    By Zein in forum Java Theory & Questions
    Replies: 10
    Last Post: June 13th, 2011, 04:18 AM
  2. Background Workjer
    By fortune2k in forum Threads
    Replies: 3
    Last Post: October 26th, 2010, 09:19 AM
  3. helloworld noob
    By LostFury2012 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: July 14th, 2010, 06:29 AM
  4. background
    By b109 in forum AWT / Java Swing
    Replies: 0
    Last Post: May 24th, 2010, 06:37 AM
  5. Background image on GUI
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 3
    Last Post: March 5th, 2010, 11:10 AM