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
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
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
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.
Re: Import images is messing with my buttons
OK, I waded through a bit, and I see one glaring error:
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.
Re: Import images is messing with my buttons
Quote:
Originally Posted by
Fubarable
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
Re: Import images is messing with my buttons
Quote:
Originally Posted by
Fubarable
OK, I waded through a bit, and I see one glaring error:
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
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?
Re: Import images is messing with my buttons
Quote:
Originally Posted by
Neoco
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.
Quote:
What is happening with the code when this is happening?
I'm not sure what's happening with *your* code when this is happening.