CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2008
    Posts
    1

    Odd Java Problem (Runs on some computers)

    I programmed this short assignment in class today, but it has a very odd problem. The objective was to make a small program that a user could enter in the name of a shape, and it would draw the shape onto a window, and draw out the name of the shape.

    It runs fine on the computer I programmed it on, and on my home computer I'm using now. However when it runs on anyone else's computer that I've tested (including the other computers in the lab running the same software/configuration). It will only print one or two shapes before not printing any more. It had my professor stumped, and I was hoping you guys would be able to shed some light on it.

    I appreciate any response!

    Code:
     
    
    import java.awt. *;
    import javax.swing. *;
    
    //Font Setup
    public class FontsAndShapes extends JFrame
    {
    		public FontsAndShapes()
    		{
    		
    	
    	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE );
    	setSize(400,500);
    	setVisible(true);
    		}
    
    
    public void paint (Graphics g)
    {
    	
    	String enterGuess = "Woot";
    		enterGuess = JOptionPane.showInputDialog("Enter a shape");
    	while (true)
    	{
    	
    	if (enterGuess.equalsIgnoreCase("Square"))
    	{
    	//Draw and Write Sq
    	g.setColor (new Color (255, 0, 0));
    	g.setFont (new Font ( "Monospaced", Font.BOLD, 12) );
    	g.drawString ( "Square!" , 100, 100);
       	g.fillRect (30, 50, 25,25);
    	enterGuess = JOptionPane.showInputDialog("Enter a shape");
    	}
    	else if(enterGuess.equalsIgnoreCase("Rectangle"))
    	{
    	//Draw and Write Rec
    	g.setColor (new Color (238, 238, 0));
    	g.setFont (new Font ( "SansSerif", Font.BOLD, 12) );
       	g.fillRect (30, 100, 25, 50);
    	g.drawString ( "Rectangle!" , 100, 150);
    	enterGuess = JOptionPane.showInputDialog("Enter a shape");
    	}
    	else if(enterGuess.equalsIgnoreCase("Circle"))
    	{
    		//Draw and Write Circle
    	g.setColor (new Color (153, 238, 0));
       	g.fillOval (30, 150, 25, 25);
    	g.setFont (new Font ( "Serif", Font.BOLD, 12) );
    	g.drawString ( "Circle!" , 100, 200);
    	enterGuess = JOptionPane.showInputDialog("Enter a shape");
    	}
    	else if(enterGuess.equalsIgnoreCase("Oval"))
    	{
    		//Draw and Write Oval
    	g.setColor (new Color (85, 222, 117));
       	g.fillOval (30, 200, 25, 50);
    	g.setFont (new Font ( "Gigi", Font.BOLD, 12) );
    	g.drawString ( "Oval!" , 100, 250);
    	enterGuess = JOptionPane.showInputDialog("Enter a shape");
    	} 
    	else
    	{
    	g.setFont (new Font ( "Times New Roman", Font.BOLD, 12) );
    	g.drawString ( "You entered an invalid shape!" , 25, 300);
    	enterGuess = JOptionPane.showInputDialog("Enter a shape");
    	}
    	
    	}
    	
    	
    
    	
    }
    
    public static void main (String [] arg)
    {
    	
                           
    	FontsAndShapes Demo = new FontsAndShapes ();
    }
    }

  2. #2
    Join Date
    Jan 2006
    Posts
    90

    Re: Odd Java Problem (Runs on some computers)

    Works for me. The jpanel where the shapes are drawn doesn't have a background for me.

    .

    Maybe different JREs?

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Odd Java Problem (Runs on some computers)

    It had my professor stumped, and I was hoping you guys would be able to shed some light on it.
    If your professor thought this was good Java code then he/she clearly doesn't know much about GUI programming in java.

    1. You shouldn't override a swing components paint() method, you should override its paintComponent() instead.

    2. You shouldn't be overriding a JFrames paint()/paintComponent method anyway, you should add another component such as a JPanel whose paintComponent() method is overridden.

    3. And most importantly, you shouldn't have dialogs displaying in a paint()/paintComponnet() method. These methods are supposed to complete quickly as they can be (and frequently are) called many times.

    My guess it that, on the computers that it's not working on, the event thread is deadlocking due to requests to redraw whilst the event thread is trying to display a modal dialog. If you run it many times you'll probably find that it sometimes fails on all computers.

  4. #4
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Odd Java Problem (Runs on some computers)

    Quote Originally Posted by keang
    If your professor thought this was good Java code then he/she clearly doesn't know much about GUI programming in java.

    1. You shouldn't override a swing components paint() method, you should override its paintComponent() instead.

    2. You shouldn't be overriding a JFrames paint()/paintComponent method anyway, you should add another component such as a JPanel whose paintComponent() method is overridden.

    3. And most importantly, you shouldn't have dialogs displaying in a paint()/paintComponnet() method. These methods are supposed to complete quickly as they can be (and frequently are) called many times.

    My guess it that, on the computers that it's not working on, the event thread is deadlocking due to requests to redraw whilst the event thread is trying to display a modal dialog. If you run it many times you'll probably find that it sometimes fails on all computers.
    Agree 100% and stated as much in this guys cross-post yesterday in the sun Java forums. But, obviously he didn't agree or listen.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured