Results 1 to 8 of 8

Thread: Stubborn Window

  1. #1
    Jayayoh is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default Stubborn Window

    Hello. I am new to both this forum, and java programming in general. I have created a swing window, adding a JPanel to it, and adding a JLabel to that. I have some simple code that my computer tech teacher at my school helped me with, that creates an animation. The animation however, is not appearing. This is less of a problem as it has appeared in previous applications. My real problem, is that when resized or minimized, the window turns black. in the case of it being resize, it turns black the portion that was resized. This has happened in several of my applications, and it is really bugging me. Some advice would be appreciated.

    *You will notice that the setResizable(false); function has been commented out*

    Java Code:
    /*  Created By John Omeljaniuk
    	Wednesday June 15 2011
    	First official Game
    */
    //-------------------------Imports-----------------------------------------------------------------
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    //-------------------------Main Program-------------------------------------------------------------
    class diceman extends JFrame
    {
    //--------------------------containers-----------------------------------------------------------
    	JPanel lvl1 = new JPanel();
    //--------------------------Images------------------------------------------------------------------
    //standing dice man
    	ImageIcon standing_die[] = {
    	new ImageIcon("C:\\TAoDM\\diceman_stand_1.bmp"),
    	new ImageIcon("C:\\TAoDM\\diceman_stand_2.bmp"),
    	new ImageIcon("C:\\TAoDM\\diceman_stand_3.bmp"),
    	new ImageIcon("C:\\TAoDM\\diceman_stand_4.bmp"),
    	new ImageIcon("C:\\TAoDM\\diceman_stand_5.bmp"),
    	new ImageIcon("C:\\TAoDM\\diceman_stand_4.bmp"),
    	new ImageIcon("C:\\TAoDM\\diceman_stand_3.bmp"),
    	new ImageIcon("C:\\TAoDM\\diceman_stand_2.bmp")
    	};
    	JLabel diceman_stand = new JLabel(standing_die[0]);
    //--------------------------variables---------------------------------------------------------------
    	int i = 0;
    	boolean motion = false;
    	timer t = new timer();
    //--------------------------Main Method-------------------------------------------------------------
    	public static void main(String[] args)
    	{
    		diceman window = new diceman();
    	}
    //--------------------------Class Constructor--------------------------------------------------------
    	public diceman()
    	{
    		super("The Adventures of Dice Man");
    		setSize(500,500);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		add(lvl1);
    		lvl1.setBackground(Color.cyan);
    		setVisible(true);
    		//setResizable(false);
    		lvl1.add(diceman_stand);
    		t.start();
    
    	}
    //--------------------------painting method-----------------------------------------------------------
    	public void paint(Graphics g)
    	{
    		if(motion == false)
    		{
    			diceman_stand.setIcon(standing_die[i]);
    			i++;
    			if(i == standing_die.length)
    			{
    				i = 0;
    			}
    		}
    	}
    //--------------------------Timer Thread--------------------------------------------------------------
    class timer extends Thread
    {
    	public void run()
    	{
    		while(true)
    		{
    			try
    			{
    				sleep(100);
    			}
    			catch(Exception e)
    			{
    			}
    			repaint();
    		}
    	}
    }
    //--------------------------end of timer---------------------------------------------------------------
    }
    //---------------------------end of main class----------------------------------------------------------

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    A problem I see is you're setting the window visible before all the components have been added.
    Last edited by Norm; 06-18-2011 at 06:10 AM. Reason: Poorly worded (and incomplete)

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    My real problem, is that when resized or minimized, the window turns black. in the case of it being resize, it turns black the portion that was resized.
    The main problem is probably because you don't invoke super.paint(...) at the start of the method. However, you should never override the paint() method of a top level container (JFrame, JDialog, JWindow or JApplet).

    Custom painting is done by overriding the paintComponent() method of a Swing component like JPanel or JComponent and then you add the component to the JFrame (or other top level container). See: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Animation should be done by using a Swing Timer, NOT by uising a Thread and sleep(). See: How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    Last edited by camickr; 06-18-2011 at 06:08 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Note: JFrame doesn't have a paintComponent() method.

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

    Default

    Moved here from 'New To Java'

    db

  6. #6
    Jayayoh is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Thank you so very much. My window and all images are added perfectly now, however, I am still attempting to figure out the swing timer. It is rather confusing. For example: where do I put the action event method, and where do I innitialize the timer? I have tried different locations in the code, but none of them seem to work.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    There are many code samples of timer use on the forum. Do a Search for examples.

    When you are having problems, post the code and the error messages here for help.

    In the code you posted earlier, the paint() method doesn't do any painting. You don't need to put code in it. Put the code in paint() in the timer action method.

  8. #8
    Jayayoh is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    WOOOOOOOT! after about two hours of working it out, My animation works...With a swing timer!Thank you so much, everyone who contributed to this post. You have my eternal gratitude.

Similar Threads

  1. Getting the Window ID of a Window or Frame
    By Toll in forum Advanced Java
    Replies: 6
    Last Post: 05-14-2011, 08:08 PM
  2. Replies: 4
    Last Post: 01-10-2010, 08:48 AM
  3. change url in parent window from child window
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-09-2009, 01:17 PM
  4. how can i move one frame window to another window
    By santhosh_el in forum AWT / Swing
    Replies: 8
    Last Post: 06-10-2009, 04:36 PM
  5. Stubborn code
    By tim in forum Entertainment
    Replies: 5
    Last Post: 01-26-2008, 10: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
  •