Results 1 to 5 of 5
  1. #1
    mjgood91 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Question Applet not repainting?

    Hi all :)

    So here's what's going on. I'm working on building an Entrapment game, and I started off by simply building an application, with the intent of converting it to an applet later. All went well, and my java program worked fine, so I decided to convert it to an applet.

    Here is where I started having problems. So far I have not been able to get the darned program to do anything! Everything compiles fine, but when I run the applet it simply says 'applet loaded', and nothing shows up.

    I think that the problem is that it's not repainting, however I have no idea why. As you can see, I have a System.exit line in the repaint method. This should cause the program to generate an error as soon as it is hit during run-time; however no errors are given during either compilation or run-time. What I'm wondering here is why the program doesn't initiate the paint method when repaint() is called?

    Sorry for the messy code... look at the init() method and the paint() method and you'll see what I'm talking about...

    Java Code:
    //import resources
    import java.util.Scanner;			//use to get input from various sources
    import javax.swing.*;				//use in basic GUI programs where the user types input into basic windows
    import java.text.DecimalFormat;	//use to format text
    import java.util.Random;			//use to generate random numbers
    import java.io.*;						//use to read/write files
    import java.awt.*;					//use for interactive/media
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    import java.awt.image.*;
    import javax.imageio.*;
    
    public class Main extends JApplet
    {
    	public Arrow arrow		= null;
    	
    	public void init()
    	{
    		try
    		{
    			offscreen	= /*createImage(720,480);*/ImageIO.read(new File("background.png"));
    		} catch (IOException e) {System.out.println("sprite not found!");}
    		g	= offscreen.getGraphics();
    		
    		setBackground(Color.black);
    		
    		repaint();
    		
    		startGame();
    	}
    	
    	public void startGame()
    	{
    		Game newGame	= new Game();
    		
    		while (newGame.lives>0)
    		{
    			score += newGame.Run(this);
    			newGame.level++;
    		}
    	}
    	
    	//	**************************** METHODS ***************************
    	
    	private int x;
    	private int y;
    	private Image sprite;
    	private Image offscreen;
    	private Graphics g;
    	private int number		= 0;
    	private int indxa			= 0;
    	private int indxb			= 0;
    	public Wall walls			= null;
    	private Ball balls		= null;
    	public int pointsRct[]	= new int[5];
    	public int timeBonus		= 1000;
    	public int score			= 0;
    	
    	public void Draw(String path, int X, int Y)
    	{
    		try{
    		x					= X;
    		sprite			= ImageIO.read(new File(path));
    		y					= Y;
    		indxa++;
    		}catch (IOException e) {System.out.println("no sprite found!");}
    	}
    	
    	public void paint(Graphics finalDraw)
    	{
    		System.exit(0);
    		//draw the wall being built
    		g.clearRect(0,0,720,480);
    		g.setColor(Color.CYAN);
    		g.fillRect(pointsRct[1],pointsRct[2],pointsRct[3],pointsRct[4]);
    		
    		//draw the walls already constructed
    		walls.wallsPhilled = -182; //	this is done so as to accurately count the walls, excluding those given
    		g.setColor(Color.BLUE);
    		for (int a = 0; a<walls.wall.length; a++)
    		{
    			for (int b = 0; b<walls.wall[a].length; b++)
    			{
    				if(walls.wall[a][b] == true)
    				{
    					g.fillRect(a*8,b*8,8,8);
    					walls.wallsPhilled++;
    				}
    			}
    		}
    		
    		//draw the balls
    		g.setColor(Color.RED);
    		for (int a = 0; a<balls.balls; a++)
    		{
    			g.fillOval((int)balls.x[a],(int)balls.y[a],8,8);
    		}
    		
    		//draw the text
    		if (walls.wallsPhilled<4000)
    		{
    			g.drawString(walls.wallsPhilled + " / 4000", 335, 12);
    		}
    		else
    		{
    			g.drawString("Level cleared! Click to continue", 250, 12);
    			g.drawString(walls.wallsPhilled + " / 4000", 450, 12);
    		}
    		g.drawString("Score: " + score, 10, 12);
    		g.drawString("Time Bonus: " + timeBonus, 600, 12);
    		
    		if (timeBonus>0)
    			timeBonus --;
    		
    		while (indxb<=indxa)
    		{
    			g.drawImage(sprite,x,y,null);
    			indxb++;
    		}
    		indxa			= 0;
    		indxb			= 0;
    		
    		super.paint(finalDraw);
    		finalDraw.drawImage(offscreen,0,0,null);
    	}
    	
    	public void update(Graphics g2)
    	{
    		paint(g2);
    	}
    	
    	public void wallHandleIs(Wall WH)
    	{
    		walls	= WH;
    	}
    	
    	public void ballHandleIs(Ball BH)
    	{
    		balls	= BH;
    	}
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    It helps to break the problem down and post a short compilable example the demos the problem. As is, I'd guess you are entering a loop in your startGame method which ties of the current thread and prevents the paint method from even being entered

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by doWhile View Post
    It helps to break the problem down and post a short compilable example the demos the problem. As is, I'd guess you are entering a loop in your startGame method which ties of the current thread and prevents the paint method from even being entered
    You're not guessing; you're right on the money (+1 rep). His while (true) loop is freezing the main Swing thread, the EDT (event dispatch trhead) preventing it from painting or getting user input. I suggest that the OP read up on use of Swing Timers and use this instead of a while (true) loop.

    Also, what the heck is System.exit(0) doing in the paint method?

    Also, read up on doing graphics in a JPanel's paintComponent method, not the JApplet's paint method.

    Also the while loop inside of the paint method looks suspect, as if you're trying to have a game loop in the paint method?

  4. #4
    mjgood91 is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    thanks guys you really helped point me in the right direction here! I was calling repaint() as you would any other method, which was working fine before I ran the program as an applet. What I have it doing now is calling a new runGame() method from the end of the paint() method in order to loop the game instead of the while loop, and it's working much better now.

    Fubarable - the System.exit(0) line was to try to make the program crash on runtime... it was something I was using to test if the program was even going through the paint method. I will also be sure to look into the paintComponent method as well.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by mjgood91 View Post
    What I have it doing now is calling a new runGame() method from the end of the paint() method in order to loop the game instead of the while loop, and it's working much better now.
    Please do not do this. Again, please do your game loop in a Swing Timer. Do not use paint or paintComponent for your app's logic, just for drawing, period. Otherwise it will fail spectacularly.

Similar Threads

  1. JPanel and JLabels Not Repainting
    By phosphide in forum AWT / Swing
    Replies: 6
    Last Post: 11-13-2010, 03:17 AM
  2. repainting more efficiently
    By imorio in forum AWT / Swing
    Replies: 2
    Last Post: 08-24-2010, 04:24 AM
  3. repainting a jframe containing two jpanels
    By musasabi in forum New To Java
    Replies: 0
    Last Post: 05-12-2010, 04:36 AM
  4. Repainting From Another Class
    By habester in forum New To Java
    Replies: 1
    Last Post: 11-13-2009, 02:29 AM
  5. [SOLVED] Repainting in a thread
    By Kephix in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-19-2009, 06:13 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
  •