Results 1 to 12 of 12
Thread: Paint everything at once?
- 10-13-2012, 04:58 PM #1
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
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:
And Photo: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(); } } } }
(Again, sorry for posting the full code, but it's not so huge..)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); } } }
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...
- 10-13-2012, 06:30 PM #2
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Re: Paint everything at once?
Anyone?
WARNING I am Russian so it's possible that I wont understand you correctly...
- 10-13-2012, 06:52 PM #3
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!
- 10-13-2012, 07:49 PM #4
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
- 10-13-2012, 08:12 PM #5
Re: Paint everything at once?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 10-13-2012, 08:20 PM #6
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
- 10-13-2012, 08:22 PM #7
Re: Paint everything at once?
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!
- 10-13-2012, 08:43 PM #8
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Re: Paint everything at once?
Here, I changed the Photo class
And changed the main class (Window):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); } }
Still same problem :PJava 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(); } } }WARNING I am Russian so it's possible that I wont understand you correctly...
- 10-13-2012, 08:46 PM #9
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Re: Paint everything at once?
WARNING I am Russian so it's possible that I wont understand you correctly...
- 10-14-2012, 01:03 AM #10
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!
- 10-14-2012, 09:50 AM #11
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Re: Paint everything at once?
Ohh, ok thanks!
WARNING I am Russian so it's possible that I wont understand you correctly...
- 10-15-2012, 03:01 AM #12
Similar Threads
-
JFrame cannot paint?
By pandaman0212 in forum AWT / SwingReplies: 7Last Post: 05-04-2013, 07:19 AM -
Paint is invalid type for variable paint.
By minibronya in forum New To JavaReplies: 3Last Post: 05-25-2012, 05:52 AM -
JFrame and paint()
By ninjaturtlez in forum New To JavaReplies: 2Last Post: 12-27-2011, 03:33 AM -
Paint
By ninjaturtlez in forum AWT / SwingReplies: 4Last Post: 12-17-2011, 04:15 AM -
Paint????
By seanfmglobal in forum New To JavaReplies: 3Last Post: 02-15-2011, 08:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks