ECE 462Object-Oriented Programmingusing C++ and JavaLecture 14Yung-Hsiang [email protected] 8 2• VirtualBase.cc• VirtualBaseCopyConstruct.cc (modified with deep copy and destructor)• VirtualBaseAssign.cc (modified)• The demonstration is conducted in Windows using cygwin so the executable files have .exe extensions.week 8 3Java Applet• programs transmitted through web browsers and executed in the clients• security restrictions:– cannot access clients’ file systems– cannot communicate through Internet– cannot execute any program in clients• invoked by HTML using <applet code=xxx.class width=... height=...></applet>week 8 4Watch.html<html><head><title>Watch Applet</title></head><body><h1>Java Watch</h1><applet code="Watch.class" height="50" width="345">This program requires a Java-enabled browser.</applet></body></html>load Watch.html in a web browser orappletviewer Watch.htmlweek 8 5// Watch.java (from Teach Yourself Java 2 in 21 Days)import java.awt.*;import java.util.*;public class Watch extends javax.swing.JApplet {private String lastTime = "";private int height = 0;private int width = 0;public void init() { // no main functionsetBackground(Color.white);height = Integer.valueOf(getParameter("height")).intValue();width = Integer.valueOf(getParameter("width")).intValue();}public void paint(Graphics screen) {Graphics2D screen2D = (Graphics2D)screen;Font type = new Font("Monospaced", Font.BOLD, 20);screen2D.setFont(type);create Watch.class by runningjavac Watch.javaweek 8 6GregorianCalendar day = new GregorianCalendar();String time = day.getTime().toString();screen2D.setColor(Color.white);screen2D.fillRect(0, 0, width, height); // erase the previous timescreen2D.setColor(Color.black);screen2D.drawString(time, 5, 25);try {Thread.sleep(1000);} catch (InterruptedException e) {// do nothing}lastTime = time;repaint();}}week 8 7Procedure to Create Applet• write HTML page with <applet code=“XXX.class" height=“YYY" width=“ZZZ">• create public class XXX extends JApplet• remove main and JFrame object• initialize in the init function• do not call setSize, setTitle, setVisible(true)• four special functions in Applet– init: initialize the Applet, assign values to attributes, alwaysneeded– start: called when the Applet is visible on a browser– stop: execute when invisible (minimized, blocked ...), to conserve the resources of the client– destroy: not needed in most Appletsweek 8 8Calculator<!--- HTML comment: from Core Java 2 Volume 1-Fundamentals ---><!--- http://horstmann.com/corejava.html, Further Information, Download Code ---><html><head><title>A Calculator</title></head><body><p>Here is a calculator, just in case you can't find yours.</p><applet code="CalculatorApplet.class" width="180" height="180"></applet></body></html>week 8 9/**@version 1.31 2004-05-07@author Cay Horstmann*/// CalculatorApplet.javaimport java.awt.*;import javax.swing.*;public class CalculatorApplet extends JApplet{ public void init(){ CalculatorPanel panel = new CalculatorPanel();getContentPane().add(panel);}}week 8 10/**@version 1.31 2004-05-07@author Cay Horstmann*/import java.awt.*;import java.awt.event.*;import javax.swing.*;/**A panel with calculator buttons and a result display.*/class CalculatorPanel extends JPanel{ public CalculatorPanel(){ setLayout(new BorderLayout());result = 0;lastCommand = "=";start = true;// add the displaydisplay = new JLabel("0");add(display, BorderLayout.NORTH); ActionListener insert = new InsertAction();ActionListener command = new CommandAction();// add the buttons in a 4 x 4 gridpanel = new JPanel();panel.setLayout(newGridLayout(4, 4));addButton("7", insert);addButton("8", insert);addButton("9", insert);addButton("/", command);week 8 11addButton("4", insert);addButton("5", insert);addButton("6", insert);addButton("*", command);addButton("1", insert);addButton("2", insert);addButton("3", insert);addButton("-", command);addButton("0", insert);addButton(".", insert);addButton("=", command);addButton("+", command);add(panel, BorderLayout.CENTER);}/** Adds a button to the center panel.@param label the button label@param listener the button listener */private void addButton(String label, ActionListener listener){ JButton button = new JButton(label);button.addActionListener(listener);panel.add(button);}/** This action inserts the button action string to theend of the display text. */private class InsertAction implements ActionListener{week 8 12public void actionPerformed(ActionEventevent){String input = event.getActionCommand();if (start) {display.setText("");start = false;}display.setText(display.getText() + input);}}/** This action executes the command that the buttonaction string denotes. */private class CommandActionimplements ActionListener{public void actionPerformed(ActionEventevent){ String command = event.getActionCommand();if (start){ if (command.equals("-")) { display.setText(command); start = false; }else lastCommand = command;week 8 13}else{ calculate(Double.parseDouble(display.getText()));lastCommand = command;start = true;}}}/**Carries out the pending calculation. @param x the value to be accumulated with the prior result.*/public void calculate(double x){if (lastCommand.equals("+")) result += x;else if (lastCommand.equals("-")) result -= x;else if (lastCommand.equals("*")) result *= x;else if (lastCommand.equals("/")) result /= x;else if (lastCommand.equals("=")) result = x;display.setText("" + result);}private JLabel display;private JPanel panel;private double result;private String lastCommand;private boolean start;}Lab 8: Creating a Java Appletweek 8 15Dual-Purpose Program• single program for both application and applet• in the applet (SlideShow) class– set inApplet to be true– create a constructor, set inApplet to false, call init– in init, if inApplet is true, call getParameter• in public static void main:– create a JFrame– create an object from the applet (SlideShow) class– add the object, pack, setSize, and setVisible• application: call main• applet: call initweek 8 16Parallel Programming • Sequential programs are no longer sufficient to meet today’s needs.• A web server must handle many viewers.• A eCommerce server must process many transactions simultaneously.• Most computers shipped today have multiple cores.• National Science Foundation gives Purdue ECE $0.8M to teach the concept of parallelism in computer courses.•
View Full Document