Results 1 to 7 of 7
  1. #1
    Neoco is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Import images is messing with my buttons

    I'm trying to import images into my summative but every time I get the code for importing an image, my buttons stop showing.

    Without code for importing images
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.imageio.*;
    import java.io.*;
    
    public class appletsummative extends JApplet implements ActionListener
    {
    	//Set up Boxes
    	Box menu = new Box (BoxLayout.Y_AXIS);
    	Box sidebar = new Box (BoxLayout.Y_AXIS);
    	Box sidebar2 = new Box (BoxLayout.X_AXIS);
    	Box application = new Box (BoxLayout.Y_AXIS);
    	Box application2 = new Box (BoxLayout.X_AXIS);
    	
    	//Set up Buttons and TextFields
    	JButton variables = new JButton ("Variables");
    	JButton fractions = new JButton ("Fractions");
    	JButton expanding = new JButton ("Expanding");
    	JButton factoring = new JButton ("Factoring");
    
    	JButton next = new JButton ("Next >");
    	JButton previous = new JButton ("< Previous");
    	JButton mmenu = new JButton ("Main Menu");
    
    	JTextField input = new JTextField ("answer", 10);
    	JButton enter = new JButton ("Enter");
    	
    	//Set up images and diagrams
    	//BufferedImage testimage = null;
    
    	//The page number is used to coordinate which components and graphics to display
    	int pgnumber = 0;
    
    	Box[] panelarray = new Box [99];
    
    	JButton[] buttonarray = new JButton [99];
    
    	public void init ()
    	{
    		//Set up window resolution and background colour
    		setSize (1000, 600);
    		setBackground (Color.lightGray);
    
    		//Organize components into arrays
    		panelarray [0] = menu;
    		panelarray [1] = sidebar;
    
    		buttonarray [0] = variables;
    		buttonarray [1] = fractions;
    		buttonarray [2] = expanding;
    		buttonarray [3] = factoring;
    		buttonarray [4] = next;
    		buttonarray [5] = previous;
    		buttonarray [6] = mmenu;
    		
    		//Load images and diagrams
    		//try
    		//{
    		//	testimage = ImageIO.read (new File ("strawberry.jpg"));
    		//}
    		//catch (IOException e)
    		//{
    			
    		//}
    
    		//Set up the location of components
    		menu.add (Box.createRigidArea (new Dimension (25, 100)));
    		variables.addActionListener (this);
    		menu.add (variables);
    		menu.add (Box.createVerticalStrut (100));
    		fractions.addActionListener (this);
    		menu.add (fractions);
    		menu.add (Box.createVerticalStrut (100));
    		expanding.addActionListener (this);
    		menu.add (expanding);
    		menu.add (Box.createVerticalStrut (100));
    		factoring.addActionListener (this);
    		menu.add (factoring);
    
    		add (menu);
    
    		mmenu.setAlignmentY (Component.BOTTOM_ALIGNMENT);
    		previous.setAlignmentY (Component.BOTTOM_ALIGNMENT);
    		next.setAlignmentY (Component.BOTTOM_ALIGNMENT);
    		
    		sidebar2.add (Box.createHorizontalStrut (50));
    		mmenu.addActionListener (this);
    		sidebar2.add (mmenu);
    		sidebar2.add (Box.createHorizontalGlue ());
    		previous.addActionListener (this);
    		sidebar2.add (previous);
    		sidebar2.add (Box.createHorizontalStrut (50));
    		next.addActionListener (this);
    		sidebar2.add (next);
    		sidebar2.add (Box.createHorizontalStrut (50));
    		
    		sidebar.add (Box.createVerticalGlue ());
    		sidebar.add (sidebar2);
    		sidebar.add (Box.createVerticalStrut (50));
    		
    		add (sidebar);
    		
    		reinit ();
    	}
    
    	//The paint method manages the graphics (eg. text, images, diagrams)...
    	public void paint (Graphics g)
    	{
    		super.paint(g);
    
    		if (pgnumber == 0)
    		{
    			g.setFont (new Font ("Calibri", Font.BOLD, 26));
    
    			g.drawString ("Math Essentials", getWidth ()/2 - 50, 20);
    
    			g.setFont (new Font ("Calibri", Font.PLAIN, 16));
    
    			g.drawString ("Welcome to Math Essentials!", 50, 50);
    
    			g.drawString ("Variables are used to act as placeholders for terms with unknown values. In this section we will cover ", 300, 115);
    			g.drawString ("solving variables with a variety of methods", 300, 131);
    			g.drawString ("Fractions are a method of expressing a division statement. In this section we will cover adding, subtracting,", 300, 245);
    			g.drawString ("multiplying and dividing with fractions", 300, 261);
    			g.drawString ("Expanding is the process of multiplying a number of polynomials together. In this section, we will cover ", 300, 370);
    			g.drawString ("using expansion as a method of simplification", 300, 386);
    			g.drawString ("Factoring is the reverse process of expanding. In this section we will cover using factoring as a method of ", 300, 495);
    			g.drawString ("simplification", 300, 511);
    		}
    		else if (pgnumber == 1)
    		{
    			g.setFont (new Font ("Calibri", Font.BOLD, 26));
    			
    			g.drawString ("Variables", 25, 25);
    			
    			g.setFont (new Font ("Calibri", Font.BOLD, 20));
    			
    			g.drawString ("Introduction", 25, 75);
    			g.drawString ("Concept 1", 25, 300);
    			
    			g.setFont (new Font ("Calibri", Font.PLAIN, 16));
    			
    			g.drawString ("In general, we view mathematical variables as an unknown value in expressions and equations. Variables in an expression are represented by", 25, 100);
    			g.drawString ("letters. For example [a] can be a variable as well as [b], [u] and [z]. When trying to find an unknown value, we usually use variables to ", 25, 116);
    			g.drawString ("represent this value so that we can solve for them. When narrowed down however, variables are basically just numbers being represented ", 25, 132);
    			g.drawString ("by letters and follow the same rules as constant numbers", 25, 148);
    			//g.drawImage (testimage, 25, 150, null);
    			
    		}
    		else if (pgnumber == 2)
    		{
    			g.setFont (new Font ("Calibri", Font.BOLD, 26));
    			
    			g.drawString ("Variables", 25, 25);
    			
    			g.setFont (new Font ("Calibri", Font.BOLD, 20));
    			
    			g.drawString ("Example 1", 25, 75);
    			
    			g.setFont (new Font ("Calibri", Font.PLAIN, 16));
    			
    			g.drawString ("Sample Question", 25, 125);
    		}
    		else if (pgnumber == 3)
    		{
    
    		}
    		else if (pgnumber == 4)
    		{
    
    		}
    
    	}
    
    	//...while the reinit method organizes the components (eg. buttons, textfields, boxes)
    	public void reinit ()
    	{
    		resetButtons (buttonarray);
    
    		if (pgnumber == 0)
    		{
    			variables.setVisible (true);
    			fractions.setVisible (true);
    			expanding.setVisible (true);
    			factoring.setVisible (true);
    		}
    		else if (pgnumber == 1)
    		{
    			mmenu.setVisible (true);
    			next.setVisible (true);
    		}
    		else if (pgnumber == 2)
    		{
    			
    		}
    		else if (pgnumber == 3)
    		{
    
    		}
    		else if (pgnumber == 4)
    		{
    
    		}
    		
    		if (pgnumber >= 2)
    		{
    			mmenu.setVisible (true);
    			previous.setVisible (true);
    			next.setVisible (true);
    		}
    	}
    
    	//This method hides all the buttons
    	public void resetButtons (JButton[] a)
    	{
    		for (int counter = 0 ; counter < 7 ; counter++)
    		{
    				a [counter].setVisible (false);
    		}
    	}	
    
    	//This method hides all the boxes
    	public void resetPanels (Box[] a)
    	{
    		for (int counter = 0 ; counter < 2 ; counter++)
    		{
    			if (a [counter].isVisible ())
    			{
    				a [counter].setVisible (false);
    			}
    		}
    	}
    
    	//Every time a button is clicked, the reinit and paint methods are ran
    	public void actionPerformed (ActionEvent e)
    	{
    		if (e.getSource () == variables)
    		{
    			pgnumber = 1;
    			reinit ();
    			repaint ();
    		}
    		else if (e.getSource () == fractions)
    		{
    
    		}
    		else if (e.getSource () == expanding)
    		{
    
    		}
    		else if (e.getSource () == factoring)
    		{
    
    		}
    
    		if (e.getSource () == next)
    		{
    			pgnumber++;
    			reinit ();
    			repaint ();
    		}
    		else if (e.getSource () == previous)
    		{
    			pgnumber--;
    			reinit ();
    			repaint ();
    		}
    		else if (e.getSource () == mmenu)
    		{
    			pgnumber = 0;
    			reinit ();
    			repaint ();
    		}
    	}
    }
    With code for import images
    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.imageio.*;
    import java.io.*;
    
    public class appletsummative extends JApplet implements ActionListener
    {
    	//Set up Boxes
    	Box menu = new Box (BoxLayout.Y_AXIS);
    	Box sidebar = new Box (BoxLayout.Y_AXIS);
    	Box sidebar2 = new Box (BoxLayout.X_AXIS);
    	Box application = new Box (BoxLayout.Y_AXIS);
    	Box application2 = new Box (BoxLayout.X_AXIS);
    	
    	//Set up Buttons and TextFields
    	JButton variables = new JButton ("Variables");
    	JButton fractions = new JButton ("Fractions");
    	JButton expanding = new JButton ("Expanding");
    	JButton factoring = new JButton ("Factoring");
    
    	JButton next = new JButton ("Next >");
    	JButton previous = new JButton ("< Previous");
    	JButton mmenu = new JButton ("Main Menu");
    
    	JTextField input = new JTextField ("answer", 10);
    	JButton enter = new JButton ("Enter");
    	
    	//Set up images and diagrams
    	BufferedImage testimage = null;
    
    	//The page number is used to coordinate which components and graphics to display
    	int pgnumber = 0;
    
    	Box[] panelarray = new Box [99];
    
    	JButton[] buttonarray = new JButton [99];
    
    	public void init ()
    	{
    		//Set up window resolution and background colour
    		setSize (1000, 600);
    		setBackground (Color.lightGray);
    
    		//Organize components into arrays
    		panelarray [0] = menu;
    		panelarray [1] = sidebar;
    
    		buttonarray [0] = variables;
    		buttonarray [1] = fractions;
    		buttonarray [2] = expanding;
    		buttonarray [3] = factoring;
    		buttonarray [4] = next;
    		buttonarray [5] = previous;
    		buttonarray [6] = mmenu;
    		
    		//Load images and diagrams
    		try
    		{
    			testimage = ImageIO.read (new File ("strawberry.jpg"));
    		}
    		catch (IOException e)
    		{
    			
    		}
    
    		//Set up the location of components
    		menu.add (Box.createRigidArea (new Dimension (25, 100)));
    		variables.addActionListener (this);
    		menu.add (variables);
    		menu.add (Box.createVerticalStrut (100));
    		fractions.addActionListener (this);
    		menu.add (fractions);
    		menu.add (Box.createVerticalStrut (100));
    		expanding.addActionListener (this);
    		menu.add (expanding);
    		menu.add (Box.createVerticalStrut (100));
    		factoring.addActionListener (this);
    		menu.add (factoring);
    
    		add (menu);
    
    		mmenu.setAlignmentY (Component.BOTTOM_ALIGNMENT);
    		previous.setAlignmentY (Component.BOTTOM_ALIGNMENT);
    		next.setAlignmentY (Component.BOTTOM_ALIGNMENT);
    		
    		sidebar2.add (Box.createHorizontalStrut (50));
    		mmenu.addActionListener (this);
    		sidebar2.add (mmenu);
    		sidebar2.add (Box.createHorizontalGlue ());
    		previous.addActionListener (this);
    		sidebar2.add (previous);
    		sidebar2.add (Box.createHorizontalStrut (50));
    		next.addActionListener (this);
    		sidebar2.add (next);
    		sidebar2.add (Box.createHorizontalStrut (50));
    		
    		sidebar.add (Box.createVerticalGlue ());
    		sidebar.add (sidebar2);
    		sidebar.add (Box.createVerticalStrut (50));
    		
    		add (sidebar);
    		
    		reinit ();
    	}
    
    	//The paint method manages the graphics (eg. text, images, diagrams)...
    	public void paint (Graphics g)
    	{
    		super.paint(g);
    
    		if (pgnumber == 0)
    		{
    			g.setFont (new Font ("Calibri", Font.BOLD, 26));
    
    			g.drawString ("Math Essentials", getWidth ()/2 - 50, 20);
    
    			g.setFont (new Font ("Calibri", Font.PLAIN, 16));
    
    			g.drawString ("Welcome to Math Essentials!", 50, 50);
    
    			g.drawString ("Variables are used to act as placeholders for terms with unknown values. In this section we will cover ", 300, 115);
    			g.drawString ("solving variables with a variety of methods", 300, 131);
    			g.drawString ("Fractions are a method of expressing a division statement. In this section we will cover adding, subtracting,", 300, 245);
    			g.drawString ("multiplying and dividing with fractions", 300, 261);
    			g.drawString ("Expanding is the process of multiplying a number of polynomials together. In this section, we will cover ", 300, 370);
    			g.drawString ("using expansion as a method of simplification", 300, 386);
    			g.drawString ("Factoring is the reverse process of expanding. In this section we will cover using factoring as a method of ", 300, 495);
    			g.drawString ("simplification", 300, 511);
    		}
    		else if (pgnumber == 1)
    		{
    			g.setFont (new Font ("Calibri", Font.BOLD, 26));
    			
    			g.drawString ("Variables", 25, 25);
    			
    			g.setFont (new Font ("Calibri", Font.BOLD, 20));
    			
    			g.drawString ("Introduction", 25, 75);
    			g.drawString ("Concept 1", 25, 300);
    			
    			g.setFont (new Font ("Calibri", Font.PLAIN, 16));
    			
    			g.drawString ("In general, we view mathematical variables as an unknown value in expressions and equations. Variables in an expression are represented by", 25, 100);
    			g.drawString ("letters. For example [a] can be a variable as well as [b], [u] and [z]. When trying to find an unknown value, we usually use variables to ", 25, 116);
    			g.drawString ("represent this value so that we can solve for them. When narrowed down however, variables are basically just numbers being represented ", 25, 132);
    			g.drawString ("by letters and follow the same rules as constant numbers", 25, 148);
    			g.drawImage (testimage, 25, 150, null);
    			
    		}
    		else if (pgnumber == 2)
    		{
    			g.setFont (new Font ("Calibri", Font.BOLD, 26));
    			
    			g.drawString ("Variables", 25, 25);
    			
    			g.setFont (new Font ("Calibri", Font.BOLD, 20));
    			
    			g.drawString ("Example 1", 25, 75);
    			
    			g.setFont (new Font ("Calibri", Font.PLAIN, 16));
    			
    			g.drawString ("Sample Question", 25, 125);
    		}
    		else if (pgnumber == 3)
    		{
    
    		}
    		else if (pgnumber == 4)
    		{
    
    		}
    
    	}
    
    	//...while the reinit method organizes the components (eg. buttons, textfields, boxes)
    	public void reinit ()
    	{
    		resetButtons (buttonarray);
    
    		if (pgnumber == 0)
    		{
    			variables.setVisible (true);
    			fractions.setVisible (true);
    			expanding.setVisible (true);
    			factoring.setVisible (true);
    		}
    		else if (pgnumber == 1)
    		{
    			mmenu.setVisible (true);
    			next.setVisible (true);
    		}
    		else if (pgnumber == 2)
    		{
    			
    		}
    		else if (pgnumber == 3)
    		{
    
    		}
    		else if (pgnumber == 4)
    		{
    
    		}
    		
    		if (pgnumber >= 2)
    		{
    			mmenu.setVisible (true);
    			previous.setVisible (true);
    			next.setVisible (true);
    		}
    	}
    
    	//This method hides all the buttons
    	public void resetButtons (JButton[] a)
    	{
    		for (int counter = 0 ; counter < 7 ; counter++)
    		{
    				a [counter].setVisible (false);
    		}
    	}	
    
    	//This method hides all the boxes
    	public void resetPanels (Box[] a)
    	{
    		for (int counter = 0 ; counter < 2 ; counter++)
    		{
    			if (a [counter].isVisible ())
    			{
    				a [counter].setVisible (false);
    			}
    		}
    	}
    
    	//Every time a button is clicked, the reinit and paint methods are ran
    	public void actionPerformed (ActionEvent e)
    	{
    		if (e.getSource () == variables)
    		{
    			pgnumber = 1;
    			reinit ();
    			repaint ();
    		}
    		else if (e.getSource () == fractions)
    		{
    
    		}
    		else if (e.getSource () == expanding)
    		{
    
    		}
    		else if (e.getSource () == factoring)
    		{
    
    		}
    
    		if (e.getSource () == next)
    		{
    			pgnumber++;
    			reinit ();
    			repaint ();
    		}
    		else if (e.getSource () == previous)
    		{
    			pgnumber--;
    			reinit ();
    			repaint ();
    		}
    		else if (e.getSource () == mmenu)
    		{
    			pgnumber = 0;
    			reinit ();
    			repaint ();
    		}
    	}
    }
    I am using the setVisible command to display and hide my buttons.

    This is my "testimage"
    http://docs.oracle.com/javase/tutori...strawberry.jpg

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

    Default Re: Import images is messing with my buttons

    My impression: That's a heck of a lot of code to ask a volunteer to wade through. What have you done to debug this so far? Have you run this with a debugger to see where the problem is occurring? Have you sprinkled your program with println statements to try to check the state of variables in various locations of the program, again to try to isolate the error? Any results of efforts on your part to isolate the error will make it much easier for us to help you.
    Last edited by Fubarable; 05-23-2012 at 05:39 AM.

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

    Default Re: Import images is messing with my buttons

    OK, I waded through a bit, and I see one glaring error:

    Java Code:
            try
            {
                testimage = ImageIO.read (new File ("strawberry.jpg"));
            }
            catch (IOException e)
            {
                 
            }
    You're ignoring exceptions exactly in your code where you think you're having problems which is equivalent to trying to drive a motorcycle with your eyes closed. I suggest you catch the exception, at least with a printStackTrace(), and then come back and tell us what the error is that you've been ignoring.
    Last edited by Fubarable; 05-23-2012 at 05:54 AM.

  4. #4
    Neoco is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Import images is messing with my buttons

    Quote Originally Posted by Fubarable View Post
    My impression: That's a heck of a lot of code to ask a volunteer to wade through. What have you done to debug this so far? Have you run this with a debugger to see where the problem is occurring? Have you sprinkled your program with println statements to try to check the state of variables in various locations of the program, again to try to isolate the error? Any results of efforts on your part to isolate the error will make it much easier for us to help you.
    My apologies. I'm very very sorry to put you through all this, but I was afraid the error could have been anywhere. In the future I'll try to condense my code down to just the important parts
    Last edited by Neoco; 05-23-2012 at 04:19 PM.

  5. #5
    Neoco is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Import images is messing with my buttons

    Quote Originally Posted by Fubarable View Post
    OK, I waded through a bit, and I see one glaring error:

    Java Code:
            try
            {
                testimage = ImageIO.read (new File ("strawberry.jpg"));
            }
            catch (IOException e)
            {
                 
            }
    You're ignoring exceptions exactly in your code where you think you're having problems which is equivalent to trying to drive a motorcycle with your eyes closed. I suggest you catch the exception, at least with a printStackTrace(), and then come back and tell us what the error is that you've been ignoring.
    Thanks. I'll try this and post what I find

    EDIT: Alright I think my program caught the IOException. I stuck a boolean in the catch space and linked a drawString command to it. Another odd thing is that the isVisible status of my buttons is true, yet they are not showing up. But I'm still don't know what the error is
    Last edited by Neoco; 05-23-2012 at 04:18 PM.

  6. #6
    Neoco is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Import images is messing with my buttons

    Alright, I'm going to try and narrow this down a bit. For some reason, my buttons start disappearing and reappearing at random times every time I run the program. I use a setVisible command to control when my buttons are supposed to appear and disappear. I need to know: is there ever a case when the isVisible status of a button is true but the button still doesn't show up? What is happening with the code when this is happening?

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

    Default Re: Import images is messing with my buttons

    Quote Originally Posted by Neoco View Post
    Alright, I'm going to try and narrow this down a bit. For some reason, my buttons start disappearing and reappearing at random times every time I run the program. I use a setVisible command to control when my buttons are supposed to appear and disappear. I need to know: is there ever a case when the isVisible status of a button is true but the button still doesn't show up?
    Absolutely. The isVisible() method just checks to see if the visible property of the component is true, and if it is held by a visualized component (I *think*). You can easily have a component covered by another but still have isVisible() return true. Or if you make it visible but don't call repaint() on its parent, you can run into the same problem.

    What is happening with the code when this is happening?
    I'm not sure what's happening with *your* code when this is happening.

Similar Threads

  1. Tic tac Toe Getting the Images to work for the buttons
    By angelus2402004 in forum AWT / Swing
    Replies: 1
    Last Post: 03-14-2012, 05:25 PM
  2. how do i import images into a java program please
    By j programer in forum Java 2D
    Replies: 2
    Last Post: 10-24-2011, 07:43 AM
  3. Why can't I see the images in the buttons?
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 10-18-2011, 02:15 AM
  4. Why can't I see the images in the buttons?
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 10-17-2011, 10:40 PM
  5. Help: Displaying Images AND Adding Buttons
    By Rhez in forum New To Java
    Replies: 2
    Last Post: 08-05-2010, 07:19 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
  •