Term Ques on Answer

snottybugbearΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

55 εμφανίσεις

Java Programming!Name ______________________________________
Java Practice Test 5 - Units 10-12!Date _______________________ Period __________
1) Vocabulary
Term
Ques(on
Answer
Standard  Image  Formats
List  the  three  standard  image  formats.  
How  do  they  differ?
drawImage()
In  this  method,  what  two  types  of  image  
files  can  be  drawn?
a)
b)
JSlider(min,  max,  value)
a)  What  do  
min,  max,  
and
 value
 
represent  in  this  constructor?
b)  What  listener  is  associated  with  this  
component?
a)
b)
Layouts
What  is  the  default  layout  for  
(a)  a  JPanel?
(b)  a  JFrame?
a)
b)
setBounds(a,  b,  c,  d)
With  what  is  the  
setBounds
 method  
associated  in  a  null  layout?
Describe  how  parameters  
a
,  
b
,  
c
,  and  
d
 
are  used.
setVisible(true)
(a)  Name  three  components  this  method  
could  be  used  with.
(b)  What  does  the  method  do?
a)
b)
2. On the graph provided, draw as accurately as possible the output of the following program.
Note: You may assume that the JFrame has a width of 400 and a height of 500. Do not worry about getting
this to look "exactly correct" in terms of pixel placement. Just make sure that the components are in the
correct general area, with the correct layouts applied. Shade in colored areas and mention the color.
import java.awt.*;
import javax.swing.*;
public class LayoutStuffA {
JFrame frame;
FillPanel canvas;
public static void main(String[] args) {
LayoutStuffA lsa = new LayoutStuffA();
lsa.Run();
}
public void Run () {
frame = new JFrame ("LayoutStuff");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

canvas = new FillPanel();n
canvas.setBackground(Color.lightGray);

frame.getContentPane().add(canvas); // puts panel on frame
frame.setVisible(true);
}
}
class FillPanel extends JPanel {
JPanel panel1, panel2, panel3;
Drawer drawer1;
JTextArea area;
JTextField field;
JScrollBar bar;
JButton button1, button2;
public FillPanel ( ) {
this.setLayout(new GridLayout(2,1));
panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,1));
panel2 = new JPanel();
area = new JTextArea(8,8);
field = new JTextField("good");
bar = new JScrollBar( JScrollBar.HORIZONTAL, 1, 1, 1, 20 );
panel2.add(area);
panel2.add(field);
panel2.add(bar);
panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
button1 = new JButton("Java");
button2 = new JButton("Programmer");
panel3.add(button1,BorderLayout.EAST);
panel3.add(button2,BorderLayout.CENTER);
panel1.add(panel2);
panel1.add(panel3);
this.add(panel1);
drawer1 = new Drawer();
this.add(drawer1);
}
}
class Drawer extends JPanel{
public Drawer ( ) {
setBackground(Color.RED);
}
public void paintComponent ( Graphics g ) {
super.paintComponent(g);
}
}
3. PictureDictionary.java
Complete the code below.
A Horizontal JSlider allows the user to select a picture for the first five letters in the English alphabet, and
show English or French words that correspond. The images are stored in a 1-D array, with names of the
images that correspond to the English word. The English and French words are stored in a 2-D array, with
the 0
th
row being English words, and the 1
st
row being French words. If the slider tab is all the way to the
left, it would be at 0 for A and show imageFile[0] (apple.gif), corresponding to stringFile[0][0](“apple”). If the
slider tab is all the way to the right, it would be at 4 for E and show imageFile[4] (for eggplant.gif),
corresponding to stringFile[0][4] (“eggplant”).
Two JButtons can be used to print the English or French word associated with the picture; when the tab of
the slider is at 0, and French is chosen with the radio button, the word “pomme” is printed to the JTextField.
For the same slider location, when English is chosen with the radio button, the word “apple” is printed. See
the sample images below.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.imageio.*;
public class PictureDictionary {
JFrame frame;
Dictionary canvas;

public static void main (String[] args) {
PictureDictionary pd = new PictureDictionary();
pd.Run();
}
public void Run () {
// Create a frame to hold everything
frame = new JFrame ("PictureDictionary");
frame.setSize(550, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.gray);

// Define panel to draw on
canvas = new Dictionary();
canvas.setBackground(Color.lightGray);

// Put frame together
frame.getContentPane().add(canvas); // puts panel on frame
frame.setVisible(true);
}
class Dictionary extends JPanel implements ChangeListener, ActionListener {
private int imgnumber, langnumber;
JSlider imgchoose;
JButton english, french;
JTextField langfield;
String [][] stringFile ={{"apple","banana","cherry","doughnut","eggplant"},
{"pomme","banane","cerise","beignet","aubergine"}};
Image [] imageFile;
Dictionary canvas;

public Dictionary ( ) {
imgnumber = langnumber = 0;
imageFile = new Image[5];
for ( int i = 0; i < imageFile.length; i++ ) {
String name = stringFile[0][i] + ".jpg";
try { imageFile[i] = ImageIO.read(new File(name)); }
catch (IOException e) {
System.out.println("ERROR: File not found - " + name);
System.exit(1);
}
}
// Initialize all components: JSlider, JRadioButton, JTextField
// Add listeners to components
// Add components to the panel
}
public void paintComponent ( Graphics g ) {
super.paintComponent ( g );
langfield.setText(stringFile[langnumber][imgnumber]);
g.drawImage ( imageFile[imgnumber] , 75 , 75 , this);
}
public void stateChanged ( ChangeEvent evt ) {
// Change the imgnumber
}
public void actionPerformed ( ActionEvent evt ) {
// Change the langnumber
}
}
}