Results 1 to 8 of 8
Thread: background static sprites active
- 07-04-2012, 02:25 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
background static sprites active
not sure if this exact problem has been brought up already or not but here is the basic problem.
I have a static background, a normal IMG, placed on my panel, above that is another set of static textures (about 225 of them for now, will increase this to over 1000 at a later date) like grass and rocks and such, and above that I have an active sprite IMG that jumps around based on where you click on that specific panel.
my problem is when I call the repaint() function since there is a lot of IMG data being processed on the screen, the screen tends to flash.
what I've been trying to do is make it so that the background and the above textures don't get repainted but stay on the screen, and the active sprite is the only thing is does.
imagine layers
layer one being the background, once painted will remain on the screen forever until i decide to repaint that layer.
layer two being all the textures, also once painted will remain on the screen forever until i decide to repaint that layer.
and a third layer that always gets repainted.
I'm not asking for code, just a point in the general area or tips. maybe I'm missing something.
- 07-04-2012, 02:56 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: background static sprites active
Is this a Swing application? If so, bear in mind that repainting is *not* an active process in the sense that you call repaint() and a repaint happens. Rather repaints occur whenever they need and ought to - a process over which you only have so much control. Calling repaint() tells Swing that some element of your application needs to be repainted, and a repaint will occur in the fullness of time. Repaints can happen for other reasons (eg application becomes unminimised) which you don't control, so demanding that some gui element "once painted will remain on the screen forever until i decide to repaint that layer" isn't going to happen.
The painting process is described in the Oracle article "Painting in AWT and Swing".
Swing provides a repaint() method that takes a rectangle argument that says what region needs to be repainted. That may help but, with respect, we can't be sure that the cause of your problem is the amount of "IMG data being processed" (or even what that means) without seeing code. Consider constructing a SSCCE that shows this flashing.since there is a lot of IMG data being processed on the screen, the screen tends to flash.
-----
If this is not a Swing application, say what GUI framework or technology is being used.
- 07-04-2012, 04:03 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Re: background static sprites active
it is a swing application
here is some of the code I have done so far. if you run it using netbeans IDE and a random couple pictures it should show the flashing I'm talking about.
ahh b4 I get to that tho. I have taken a look at the Painting in AWT and Swing link you sent. The rectangle argument seems like it would reduce the burden a lot, but in the future when I have the basics up and running ill have around 10 sprites running around the screen at any given time each doing its own thing (active time bars, battle animations, spell effects etc) based on mouse interactions. so 10 character threads moving fluidly from point a to b repainting all the 15x15 or 30x30 pixel imgs overlapping a 15x15 or 30x30 grid seems like the same problem will occur.
------------------------------------------
----------------------------------------------------Java Code:package game; import java.io.*; import java.util.*; import java.util.logging.*; import java.util.regex.*; import javax.swing.*; public class Game { public static void main(String[] args) throws InterruptedException, FileNotFoundException { BackgroundGeneration bgg = new BackgroundGeneration(); GlobalVariables global = new GlobalVariables(); while (true){ bgg.repaint(); } }
----------------------------Java Code:package game; import java.io.*; public class GlobalVariables { public static int[] x1; //x1 data public static int[] y1; //y1 data public static int[] x2; //x2 data public static int[] y2; //y2 data //public static int[] zone; //the specific zone public static int[] picdata; //what pic is on the zone public static boolean[] passdata; //if the object can be moved on public static int[] effectdata; //what effect does the zone have public static int[] objectdata; //if there is an object on that zone //public static int belongsto; //if the character belong to p1 or p2 or none public static int ClassNum; //the class number public static int CharLevel; //the level of the character public static int hp; //the HP of the character public static int mp; //the MP of the character public static int atk; //the atk of the character public static int def; //the def of the character public static int str; //the str of the character public static int vit; //the vit of the character public static int dex; //the dex of the character public static int spd; //the spd of the character public static int Xpos; public static int Ypos; public GlobalVariables() { x1 = new int[225]; y1 = new int[225]; x2 = new int[225]; y2 = new int[225]; picdata = new int [225]; passdata = new boolean [225]; effectdata = new int [225]; objectdata = new int [225]; for(int j = 0; j<15;j++) { for(int i =0; i<15;i++) { y1[i+j*15] = 60+j*60; y2[i+j*15] = 30+j*60; x1[i+j*15] = 40+i*60; x2[i+j*15] = 70+i*60; //zone [i+j*15] = i+j*15; picdata[i+j*15] = 1; passdata[i+j*15] = false; effectdata[i+j*15] = 0; objectdata [i+j*15] = 0; //System.out.println("" + picdata[i]); } } }
------------------------Java Code:package game; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.*; //This class will be used to generate all the backgroud //textures public class BackgroundGeneration extends JFrame { GlobalVariables global = new GlobalVariables(); Zone zone = new Zone(); public static JPanel MainPanel; public static Image mainpanelbg; //All the images used to make the main panel //grass = 1 //stone = 2 //grass-stone = 3 public static Image image; public static Image image2; public static int value = 1; public BackgroundGeneration() { super("Game Name"); MainPanel = new JPanel(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(1000,1000); setLocationRelativeTo(null); setVisible(true); setResizable(false); add(MainPanel); Handlerclass handler = new Handlerclass(); MainPanel.addMouseListener(handler); MainPanel.addMouseMotionListener(handler); //run(); } public class Handlerclass implements MouseListener, MouseMotionListener { public void mouseClicked (MouseEvent event) { GlobalVariables.Xpos = event.getX(); GlobalVariables.Ypos = event.getY(); System.out.println("" + GlobalVariables.Xpos + " " + GlobalVariables.Ypos); } public void mousePressed(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } public void mouseDragged(MouseEvent event) { } public void mouseMoved(MouseEvent event) { } } public void paint (Graphics g) { mainpanelbg = new ImageIcon("E:\\game\\Game\\mainpanelbg.png").getImage(); image2 = new ImageIcon("E:\\game\\Game\\KnightStandingTest2.png").getImage(); g.drawImage(mainpanelbg, 0, 0, null); for (int i = 0; i<225;i++) { value = GlobalVariables.picdata[i]; System.out.println(value); if (value == 1){ image = new ImageIcon("E:\\game\\Game\\grass1.png").getImage(); } else if (value == 2) { image = new ImageIcon("E:\\game\\Game\\grass1.png").getImage(); } else if (value == 3) { image = new ImageIcon("E:\\game\\Game\\grass2.png").getImage(); } else if (value == 4) { image = new ImageIcon("E:\\game\\Game\\grass3.png").getImage(); } g.drawImage(image, GlobalVariables.x1[i], GlobalVariables.y1[i], null); g.drawImage(image2, GlobalVariables.Xpos, GlobalVariables.Ypos, null); } } }
sorry wasnt quite sure how to post this all in those scrolly windows.
and yes im quite new to java so excuse my bad habits >.<Last edited by leggoeggo; 07-04-2012 at 04:39 AM.
- 07-04-2012, 04:27 AM #4
Member
- Join Date
- Jul 2012
- Posts
- 27
- Rep Power
- 0
Re: background static sprites active
click "Go Advanced" and click the # button and put code inside the 2 CODE things. I'm new to these forums also.
-
Re: background static sprites active
You need to read the standard Swing drawing tutorials before trying to do this stuff, and I can see from your post that you haven't yet done this yet. For one, you should do all your drawing inside of a component that extends from JComponent such as JComponent itself or JPanel, you should draw inside of its paintComponent method, not its paint method, you should not be reading in images or doing any file i/o inside of paint or paintComponent, not unless you want to create a *very* unresponsive GUI,... etc...
Also as an aside, none of your variables should be static as you will lose all the benefits of object oriented programming if you do this.
- 07-04-2012, 11:28 PM #6
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Re: background static sprites active
read through it, doesn't help, other then pointing out my bad coding practices which i can easily fix at a later time.
even there example does the exact same thing i'm trying to avoid and there is only a single object and text. it has a refresh rate effect which shows up in the animation.
I am trying to make it as fluid as possible. You don't see other java games running with jittery animations that cut to white screens and back after almost every repaint.
which gets me thinking that there is a better way to draw animations. layers (doubt that will work tho since a single red box 20x20 jitters)? another graphics method? must be something there doing right
- 07-05-2012, 12:30 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: background static sprites active
Seriously, you should consider each of the suggestions Fubarable made in the first paragraph of #5. In fact, try them! even if you don't consider they'll help. After all your gui flickers, Fubarable's don't.
And, since it's the animation that's proving difficult, concentrate on that. Remove all code except that which causes a bunch of objects to fly about the screen.Last edited by pbrockway2; 07-05-2012 at 12:33 AM.
- 07-10-2012, 05:28 PM #8
Re: background static sprites active
One thing that might help with the flickering is to load all of the images to memory before painting them. If you do it whilst repainting, you will have to wait for the data to be put into RAM before your program can access them and that takes more time then just grabbing them right off of the RAM stack.
My API:Java Code:cat > a.out || cat > main.class
Similar Threads
-
Non static method printBoard(boolean) cannot be referenced from a static context
By warp in forum New To JavaReplies: 3Last Post: 05-31-2012, 07:56 PM -
Drawing monochrome/alpha channel sprites in different colors
By kjkrum in forum Java GamingReplies: 1Last Post: 02-18-2012, 08:47 PM -
Sprites - My Game (Sort of)
By David M. in forum Java 2DReplies: 2Last Post: 04-25-2011, 11:00 PM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks