-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageMenu.java
More file actions
134 lines (116 loc) · 3.57 KB
/
ImageMenu.java
File metadata and controls
134 lines (116 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Opens a separate menu that displays a correctly scaled image, either a
* problem or solution image. Called from either QQMenu or TrainMenu.s
*
* @author Austin Lei and Jason Dong
* @version May 28, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: docs.oracle.com for the createImageIcon method
*/
public class ImageMenu
{
/**
* Holds all of the GUI elements.
*/
private static JFrame frame;
/**
* Constructs the menu given the problem and the boolean isProblem. If
* isProblem is true, creates a menu with a back button and the problem's
* problem image. Otherwise, creates a menu with a back button and the
* problem's solution image.
*
* @param prob
* the problem for which the images should be displayed
* @param isProblem
* whether the problem image should be shown or the solution
* image
*/
public ImageMenu( Problem prob, boolean isProblem )
{
JLabel label1;
JButton back = new JButton( "Close" );
back.setBounds( 5, 5, 50, 50 );
ImageIcon icon;
if ( isProblem )
{
icon = createImageIcon( prob.getProblemImage(), "image" );
}
else
{
icon = createImageIcon( prob.getSolutionImage(), "image" );
}
Image image = icon.getImage();
Image newImage;
if ( icon.getIconWidth() >= icon.getIconHeight() )
{
newImage = image.getScaledInstance( 680,
680 * icon.getIconHeight() / icon.getIconWidth(),
Image.SCALE_DEFAULT );
}
else
{
newImage = image.getScaledInstance( 680 * icon.getIconWidth() / icon.getIconHeight(),
680,
Image.SCALE_DEFAULT );
}
icon.setImage( newImage );
label1 = new JLabel( "", icon, JLabel.CENTER );
label1.setBounds( 60, 60, 680, 940 );
label1.setVerticalAlignment( JLabel.TOP );
label1.setHorizontalAlignment( JLabel.CENTER );
back.addActionListener( new BackButtonListener() );
frame = new JFrame( "ImageMenu" );
frame.setBounds( 0, 0, 800, 1000 );
frame.setDefaultCloseOperation( 0 );
frame.setVisible( true );
Container c = frame.getContentPane();
c.add( label1 );
c.add( back );
frame.setLayout( null );
frame.setResizable( false );
}
/**
* Handles when the back button is clicked; when clicked, returns to
* MainMenu by closing all of TypeMenu and creating a MainMenu object.
*
* @author Austin Lei
* @version May 22, 2018
* @author Period: 2
* @author Assignment: QuickQuiz
*
* @author Sources: none
*/
private class BackButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
frame.dispose();
}
}
/**
* Method was taken from docs.oracle.com
*
* @param path
* is the pathname of the image
* @param description
* is the name of the image
* @return ImageIcon, a fixed-size picture
*/
protected ImageIcon createImageIcon( String path, String description )
{
if ( path != null )
{
return new ImageIcon( path, description );
}
else
{
System.err.println( "Couldn't find file: " + path );
return null;
}
}
}