Results 1 to 3 of 3
  1. #1
    Snoman is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default The Strangest Problem (Swing)

    Java Code:
    package mandrakeRoot;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    
    public class StartMenu extends JPanel {
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -915973316710108964L;
    	ArrayList<JButton> buttons;
    	JLabel title;
    	JLabel titlebot;
    	Image background;
    	Image titlebg;
    	CustomImageButton test;
    	
    	public StartMenu () {
    		
    		buttons = new ArrayList<JButton>();
    		
    		buttons.add(new JButton("New Game"));
    		buttons.add(new JButton("Load Game"));
    		buttons.add(new JButton("Options"));
    		
    		title = new JLabel("Project", SwingConstants.CENTER);
    		titlebot = new JLabel("mandrakeRoot", SwingConstants.CENTER);
    		test = new CustomImageButton("C:\\Users\\FlyingBear\\workspace\\mandrakeRoot\\NewGameButton.jpg");
    		background = new ImageIcon("C:\\Users\\FlyingBear\\workspace\\mandrakeRoot\\title.jpg").getImage();
    		titlebg = new ImageIcon("C:\\Users\\FlyingBear\\workspace\\mandrakeRoot\\NewGameButton.gif").getImage();
    		
    		this.setLayout(null);
    		
    		int num = 0;
    		
    		for (JButton x : buttons) {
    			
    			this.add(x);
    			x.setSize(150, 50);
    			x.setLocation(75 + 250 * num, 400);
    			num++;
    			
    		}
    		
    		this.add(title);
    		this.add(titlebot);
    		
    		title.setLocation(300, 100);
    		title.setSize(200, 100);
    		
    		titlebot.setLocation(250, 150);
    		titlebot.setSize(300, 100);
    		
    		title.setFont(new Font("Matura MT Script Capitals", Font.PLAIN, 36));
    		title.setForeground(Color.GRAY);
    		
    		titlebot.setFont(new Font("Matura MT Script Capitals", Font.PLAIN, 36));
    		titlebot.setForeground(Color.GRAY);
    		
    		
    		test.setSize(200, 100);
    		test.setLocation(50, 50);
    		this.add(test);
    		
    	}
    	
    	public void paintComponent(Graphics g) {
    		
    		g.drawImage(background, 0, 0, null);
    		g.drawImage(titlebg, 250, 126, null);
    		
    	}
    	
    	public static void main (String[] args) {
                    GameWindow gw = new GameWindow();
    		StartMenu sm = new StartMenu();
    		
    		gw.setContentPane(sm);
    		
    	}
    
    }
    Java Code:
    public static void main (String[] args) {
    		StartMenu sm = new StartMenu();
    		GameWindow gw = new GameWindow();
    		
    		gw.setContentPane(sm);
    		
    	}
    Ok, so I've just started working with swing and GUI's in general. I've run into a problem. I found a solution to the problem that makes the program work, but I don't understand why it makes the program work. I was wondering if someone might look at my code and tell me why.

    The problem I was having was very strange. Every time the program ran it would either work fine or display a gray window. Nothing being changed in the code the result of running the program would differ seemingly randomly. The problem occurs when the gamewindow instantiation is above the startmenu instantiation but not when startmenu is above gamewindow.

    Here is Gamewindow if you feel you need to look at it:
    Java Code:
    package mandrakeRoot;
    
    import java.awt.Color;
    import javax.swing.JFrame;
    
    public class GameWindow extends JFrame {
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -4921182521329913757L;
    
    	public GameWindow () {
    		
    		super("Project mandrakeRoot");
    		this.setSize(800, 600);
    		this.setResizable(false);
    		this.setBackground(Color.BLACK);
    		this.setVisible(true);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    	}
    
    }
    I've been trying to think of a reason for this for a while now and can't come up with anything. If someone would be so kind as to explain to me why this happens it would be greatly appreciated.

    Thanks,
    Snoman

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: The Strangest Problem (Swing)

    1. Your code doesn't respect Swing's single threaded rule. Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    2. Don't unnecessarily set a window visible and then add or change its content. Do it the other way round.

    3. Custom painting code that doesn't paint every pixel of a Component should invoke the super implementation. Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Additionally, StartMenu is IMO a bad choice of class name for an extended JPanel. A casual reader might assume the class housed a menu structure.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: The Strangest Problem (Swing)

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Simple Swing problem
    By Arom in forum AWT / Swing
    Replies: 1
    Last Post: 02-11-2012, 05:50 PM
  2. Swing applet problem
    By link6790 in forum New To Java
    Replies: 9
    Last Post: 05-24-2011, 02:31 PM
  3. Java Swing Problem
    By mohitgupta1989 in forum Advanced Java
    Replies: 7
    Last Post: 06-23-2010, 07:46 AM
  4. Problem In Swing Components
    By SANDY_INDIA in forum AWT / Swing
    Replies: 1
    Last Post: 07-19-2008, 10:23 PM
  5. SWT & Swing Bridge problem
    By roshithmca in forum AWT / Swing
    Replies: 0
    Last Post: 03-26-2008, 01:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •