Results 1 to 8 of 8
Thread: Stubborn Window
- 06-18-2011, 02:56 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
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----------------------------------------------------------
- 06-18-2011, 03:54 AM #2
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 05:10 AM. Reason: Poorly worded (and incomplete)
- 06-18-2011, 04:38 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
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).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.
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 05:08 AM.
- 06-18-2011, 04:57 AM #4
Note: JFrame doesn't have a paintComponent() method.
- 06-18-2011, 05:01 AM #5
Moved here from 'New To Java'
db
- 06-18-2011, 03:16 PM #6
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
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.
- 06-18-2011, 03:39 PM #7
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.
- 06-19-2011, 01:23 AM #8
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Getting the Window ID of a Window or Frame
By Toll in forum Advanced JavaReplies: 6Last Post: 05-14-2011, 07:08 PM -
Need to pass a value from a parent window to a pop up or child window
By blackpanther in forum Advanced JavaReplies: 4Last Post: 01-10-2010, 07:48 AM -
change url in parent window from child window
By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-09-2009, 12:17 PM -
how can i move one frame window to another window
By santhosh_el in forum AWT / SwingReplies: 8Last Post: 06-10-2009, 03:36 PM -
Stubborn code
By tim in forum EntertainmentReplies: 5Last Post: 01-26-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks