Complete Chapter 12 Lab Assignment 1212 and Chapter 13 Lab A
Complete Chapter 12 Lab Assignment 12.12 and Chapter 13 Lab Assignment 13.9. The Convert.java and Triangles.java classes are provided. You will need to complete the ConvertFrame.java and TrianglesPanel.java classes.
// Exercise 12.12 : Convert.java
//Temperature-conversion program
import javax.swing.JFrame;
public class Convert{
public static void main(String[] args) {
ConvertFrame convertFrame = new ConvertFrame();
convertFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
convertFrame.setSize(225, 90); // set frame size
convertFrame.setVisible(true); // display frame
}
} // end class Convert
// Exercise 12.12 : ConvertFrame.java
// Temperature-conversion program
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ConvertFrame extends JFrame {
private JLabel prompt;
// label to prompt user to enter Fahrenheit
// label to display temperature in Celsius
// textfield to enter temperature
// constructor sets up GUI
public ConvertFrame() {
//prompt to enter Fahrenheit temperature
// textfield for Fahrenheit
// register anonymous action listener
//get temperature, be user to parse value and convert to an integer
//calculate the celsius
//display celsius
//set border layouts
} // end ConvertFrame constructor
} // end class ConvertFrame
----------------------------------------------------------------------------------------------------------------------------------------------------
// Exercise 13.9 : Triangles.java
// Displays randomly generated triangles in different colors.
import javax.swing.JFrame;
public class Triangles{
public static void main(String args[]) {
// create frame for TrianglesJPanel
JFrame frame = new JFrame(\"Drawing Triangles\");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TrianglesJPanel trianglesJPanel = new TrianglesJPanel();
frame.add(trianglesJPanel); // add trianglesJPanel to frame
frame.setSize(400, 400); // set frame size
frame.setVisible(true); // display frame
}
} // end class Triangles
// Exercise 13.9: TrianglesJPanel.java
// Displays randomly generated triangles in different colors.
import java.awt.Color;
import java.awt.geom.GeneralPath;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
import javax.swing.JPanel;
public class TrianglesJPanel extends JPanel{
// random-number generator
// constructor sets background
public TrianglesJPanel() {
// set JPanel background color
}
// draw ten triangles
public void paintComponent(Graphics g) {
super.paintComponent(g);
// cast graphics object
// create a triangle from three random points
// create the object which will be the triangle
// min distance between triangle and top/left
// size of area triangle appears in
// use method moveTo to start the triangle
// draw a line to the second point
// draw a line to the third point
// draw a line back to the initial point
// choose a random color
// color the interior of the triangle
}
}
} // end class TrianglesJPanel
Solution
13.9
package drawtriangleusingline2d;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.util.Random;
public class DrawTriangleUsingLine2D extends JApplet {
public void init() {
setBackground(Color.lightGray);
}
public void paint(Graphics g) {
Random r = new Random();
int x1 = r.nextInt((50-1)+1)+1;
int x2 = r.nextInt((150-1)+1)+1;
int y1 = r.nextInt((150-1)+1)+1;
int y2 = r.nextInt((150-1)+1)+1;
int y11 = r.nextInt((50-1)+1)+1;
int x22 = r.nextInt((50-1)+1)+1;
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.red);
g2d.draw(new Line2D.Double(x1,y1,x2,y2 ));
g2d.draw(new Line2D.Double(50,y11,150,150 ));
g2d.draw(new Line2D.Double(50,50,x22,150 ));
}
public static void main(String s[]) {
JFrame frame = new JFrame(\"Show Triangle\");
JApplet applet = new DrawTriangleUsingLine2D();
frame.getContentPane().add(\"Center\", applet);
applet.init();
frame.setSize(500, 550);
frame.show();
}
}
12.12:
package fahrenheitgui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FahrenheitGUI {
private int WIDTH = 300;
private int HEIGHT = 75;
private JFrame frame;
private JPanel panel;
private JLabel inputLabel, resultLabel;
private JTextField fahrenheit;
//-----------------------------------------------------------------
// Sets up the GUI.
//-----------------------------------------------------------------
public FahrenheitGUI() {
frame = new JFrame(\"Temperature Conversion\");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputLabel = new JLabel(\"Enter Fahrenheit temperature:\");
JLabel outputLabel = new JLabel(\"Temperature in Celsius: \");
resultLabel = new JLabel(\"---\");
fahrenheit = new JTextField(5);
fahrenheit.addActionListener(new TempListener());
panel = new JPanel();
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
panel.setBackground(Color.yellow);
panel.add(inputLabel);
panel.add(fahrenheit);
panel.add(outputLabel);
panel.add(resultLabel);
frame.getContentPane().add(panel);
}
//-----------------------------------------------------------------
// Displays the primary application frame.
//-----------------------------------------------------------------
public void display() {
frame.pack();
frame.setVisible(true);
}
//*****************************************************************
// Represents an action listener for the temperature input field.
//*****************************************************************
private class TempListener implements ActionListener {
//--------------------------------------------------------------
// Performs the conversion when the enter key is pressed in
// the text field.
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event) {
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt(text);
celsiusTemp = (fahrenheitTemp - 32) * 5 / 9;
resultLabel.setText(Integer.toString(celsiusTemp));
}
}
public static void main(String args[]){
FahrenheitGUI fh = new FahrenheitGUI();
fh.display();
}
}




