Results 1 to 7 of 7
  1. #1
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    177
    Rep Power
    2

    Angry Background overlapping images

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class sample{
    	public static void main(String args[]){
    		JFrame frame = new JFrame();
    		frame.setSize(300,300);
    		SampleProblem problem = new SampleProblem();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		problem.init();
    		frame.add(problem);
    		frame.setVisible(true);
    	}
    }
    class SampleProblem extends JApplet implements ActionListener{
    	boolean loaded=false;
    	Timer timer = new Timer(500,this);
    	public void init(){
    		
    	}
    	public void actionPerformed(ActionEvent evt){
    		repaint();
    	}
    	public void paint(Graphics g){
    		if(loaded==false){
    			getContentPane().setBackground(Color.black);
    			g.setColor(Color.blue);
    			g.fillRect(10,10,100,100);
    			loaded=true;
    		}
    	}
    }
    Alright so i wrote this program to show you guys the problem i have been having. To limit the amount of drawing done i want unmoving objects to be painted once. So i set a boolean. When it first load the boolean is false so it draws then after first draw i never want it to draw the objects agian but leave them on the screen. Now in this if you run it you get a black screen. If you didnt have the "getContentPane().setBackground(Color.black)" it would work fine and show a blue brick. Is there something I don't understand about the setBackgound Function that makes it automatically redone even when not called? and if so how do i overwrite this?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Background overlapping images

    You shouldn't set the background from the paint() function.

    If you want something to be visible, you have to paint it, simple as that. If you don't draw something, it will not be visible. You *could* paint to a BufferedImage and then draw that BufferedImage on the screen, but you still have to draw the BufferedImage on the screen every time.

    Your problem is happening because Java is drawing the background, and since you aren't redrawing your rectangle, the rectangle gets "covered up" by the background. Normally you would just draw the rectangle again.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Background overlapping images

    Every time your program redraws the background it is covering up the other images because they aren't being redrawn while the background is being drawn over them.

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Background overlapping images

    What you should do, is either draw the background once in the main method, or draw the background using g.fillRect() and then draw the images over the top of that.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Background overlapping images

    Quote Originally Posted by pj6444 View Post
    What you should do, is either draw the background once in the main method, or draw the background using g.fillRect() and then draw the images over the top of that.
    You shouldn't rely on drawing the background "from the main method". I'm not even sure how you think you would do that.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: Background overlapping images

    Quote Originally Posted by KevinWorkman View Post
    You shouldn't rely on drawing the background "from the main method". I'm not even sure how you think you would do that.
    He's referring to SETTING the background, not drawing it. The drawing is a result of setting it.

    -END-

    @Thread, in no way any kind of correction on what KevinWorkman has said or any kind of assumption on what he knows or doesn't know, but rather just a general observation:

    Basically the flow of what is happening in the OP's code is like this:

    - paint()
    -- set background (okay! I'll repaint soon to make it visible)
    -- paint rectangle
    - repaint happens
    -- background becomes black, rectangle is painted over

    Note: educated guess and my strong opinion, which may still be wrong. But I do believe I'm right ;)
    Last edited by gimbal2; 01-07-2014 at 04:36 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Background overlapping images

    Yes. I meant setting the background. My apologies.

Similar Threads

  1. Replies: 1
    Last Post: 04-07-2012, 09:27 PM
  2. Replies: 3
    Last Post: 02-08-2011, 06:12 AM
  3. frame overlapping
    By sameerk in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-19-2010, 07:45 AM
  4. Replies: 3
    Last Post: 11-10-2009, 10:24 PM
  5. Random Div Background Images
    By mchapple in forum New To Java
    Replies: 2
    Last Post: 01-26-2009, 01:16 AM

Tags for this Thread

Posting Permissions

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