Results 1 to 12 of 12
  1. #1
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Question Paint everything at once?

    Yello, I've made a simple program that paints black fillRect with the size of the window and and then it paints an image
    I've made thread that makes the image move to the right, but the problem is that sometimes the image blinks which I think that it's noticeable how the image paints in the black rectangle.. Well basically I can't figure out how do I make it render everything at once so the image would be clearly seen that it's moving.

    (Program is two classes Window(main) and Photo)

    So here how I made it:
    Window:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Window implements Runnable{
    	public static Dimension wSize = new Dimension(1024, 600);
    	public static Photo photo = new Photo();
    	public static double charX = 30.0; 
    	public static double charY = 30.0; 
    	public void start(){
    		new Thread(this).start();
    	}
    	public static void main(String[] args){
    		Window window = new Window();
    		JFrame w = new JFrame();
    		w.setSize(wSize);
    		w.setVisible(true);
    		w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		w.setResizable(false);
    		w.add(photo);
    		window.start();
    	}
    	public void create(){
    		Graphics g = photo.getGraphics();
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, wSize.width, wSize.height);
    		charX += 5;
    		photo.render(g);
    	}
    	public void run() {
    		while(true){
    			create();
    			try {
    				Thread.sleep(10);
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    And Photo:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    
    public class Photo extends JPanel{
    	private static final long serialVersionUID = 1L;
    	public static BufferedImage myImage;
    	
    	public Photo() {
    		System.out.println("User working directory: " + System.getProperty("user.dir"));
    		try{
    			myImage = ImageIO.read(new File("house.png"));
    		}catch(Exception e){ 
    			System.out.println("ERROR");
    			e.printStackTrace();
    		}
    	}
    	public void render(Graphics g){
    		if (myImage != null) {
    			g.drawImage(myImage,(int) Window.charX,(int) Window.charY,50,50,null);
    		}
    	}
    }
    (Again, sorry for posting the full code, but it's not so huge..)


    UPDATE: as they say it called "flickering problem"
    Last edited by Lionlev; 10-13-2012 at 07:50 PM.
    WARNING I am Russian so it's possible that I wont understand you correctly...

  2. #2
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Paint everything at once?

    Anyone?
    WARNING I am Russian so it's possible that I wont understand you correctly...

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

    Default Re: Paint everything at once?

    Please don't bump your thread. It's only been a couple hours, and now people will see that your thread has a reply and will assume you already received help.

    You don't want to specifically get a Graphics instance from your JPanel like that. Instead, override paintComponent() and use that to do all your painting. Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Paint everything at once?

    Quote Originally Posted by KevinWorkman View Post
    Please don't bump your thread. It's only been a couple hours, and now people will see that your thread has a reply and will assume you already received help.

    You don't want to specifically get a Graphics instance from your JPanel like that. Instead, override paintComponent() and use that to do all your painting. Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Ok sorry..
    But I can't override paintcomponent because I am planning to do multiple classes with graphics methods..
    WARNING I am Russian so it's possible that I wont understand you correctly...

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

    Default Re: Paint everything at once?

    Quote Originally Posted by Lionlev View Post
    Ok sorry..
    But I can't override paintcomponent because I am planning to do multiple classes with graphics methods..
    I'm not sure exactly what you mean, but you can call other render methods from paintComponent and pass the Graphics instance around that way.
    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
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Paint everything at once?

    Quote Originally Posted by KevinWorkman View Post
    I'm not sure exactly what you mean, but you can call other render methods from paintComponent and pass the Graphics instance around that way.
    Ok, but that doesn't solve the flickering problem...
    WARNING I am Russian so it's possible that I wont understand you correctly...

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Paint everything at once?

    Quote Originally Posted by Lionlev View Post
    Ok, but that doesn't solve the flickering problem...
    Are you sure? Have you actually tried it? If so, you're going to have to post an updated SSCCE that demonstrates exactly what you're doing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Paint everything at once?

    Quote Originally Posted by KevinWorkman View Post
    Are you sure? Have you actually tried it? If so, you're going to have to post an updated SSCCE that demonstrates exactly what you're doing.
    Here, I changed the Photo class
    Java Code:
    public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, Window.wSize.width, Window.wSize.height);
    		g.setColor(Color.WHITE);
    		g.drawString("LOL", 300, 100);
    		if (myImage != null) {
    			g.drawImage(myImage,(int) Window.charX,(int) Window.charY,50,50,null);
    		}
    		
    	}
    And changed the main class (Window):
    Java Code:
    public void create(){
    		Graphics g = photo.getGraphics();
    		charX += speedX;
    		charY += speedY;
    		photo.paintComponent(g);
    	}
    	public void run() {
    		while(true){
    			create();
    			try {
    				Thread.sleep(mSec);
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    Still same problem :P
    WARNING I am Russian so it's possible that I wont understand you correctly...

  9. #9
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Paint everything at once?

    Quote Originally Posted by KevinWorkman View Post
    Are you sure? Have you actually tried it? If so, you're going to have to post an updated SSCCE that demonstrates exactly what you're doing.
    HOLD ON ONE MINUTE! I think it's not flickering anymore because I changed photo.paintComponent(g); to photo.repaint();
    Thanks! (and tell me if I did the right thing or not)
    WARNING I am Russian so it's possible that I wont understand you correctly...

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Paint everything at once?

    Yeah, you don't want to call getGraphics() on a component. You just want to call repaint() and let Swing take care of it for you. Calling getGraphics() and using that is working against Swing, which is what's causing the flickering.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Paint everything at once?

    Ohh, ok thanks!
    WARNING I am Russian so it's possible that I wont understand you correctly...

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Paint everything at once?

    Moved from Advanced Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JFrame cannot paint?
    By pandaman0212 in forum AWT / Swing
    Replies: 7
    Last Post: 05-04-2013, 07:19 AM
  2. Paint is invalid type for variable paint.
    By minibronya in forum New To Java
    Replies: 3
    Last Post: 05-25-2012, 05:52 AM
  3. JFrame and paint()
    By ninjaturtlez in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 03:33 AM
  4. Paint
    By ninjaturtlez in forum AWT / Swing
    Replies: 4
    Last Post: 12-17-2011, 04:15 AM
  5. Paint????
    By seanfmglobal in forum New To Java
    Replies: 3
    Last Post: 02-15-2011, 08:00 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
  •