Java applet
This presentation is the property of its rightful owner.
Sponsored Links
1 / 33

Java applet PowerPoint PPT Presentation


  • 85 Views
  • Uploaded on
  • Presentation posted in: General

Java applet. ใช้ยังไง. มันรันจากหน้า html ซึ่งหน้านั้นต้องมี tag ที่บอกข้อมูล เอา applet อะไร ( class file อยู่ไหน) จะอยู่ตรงไหนในหน้า browser (size, location) Browser ต้องมี java plug-in นะ ดูตัวอย่างหน้าต่อไปเลย. ต้อง extends ตัวนี้. import java.awt.*;

Download Presentation

Java applet

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Java applet

Java applet


Java applet

  • html tag

    • applet (class file )

    • browser (size, location)

  • Browser java plug-in


Java applet

extends

import java.awt.*;

import javax.swing.*;

/**

* @version 1.22 2007-06-12

* @author Cay Horstmann

*/

public class NotHelloWorldApplet extends JApplet

{

public void init()

{

EventQueue.invokeLater(new Runnable()

{

public void run()

{

JLabel label = new JLabel("Not a Hello, World applet", SwingConstants.CENTER);

add(label);

}

});

}

}

user interface event dispatch thread main GUI

invokeLater

html


Java applet

<applet code="NotHelloWorldApplet.class"

width="300" height="100">

</applet>

appletviewer xxx.html

appletviewer


Java applet

applet browser


Application applet

application applet

  • html

  • public class extends Japplet

  • main method

  • GUI

  • init

  • applet init

  • setSize

  • setDefaultCloseOperation

  • setTitle setVisible


Override

override

void start()

void stop()

void destroy()

void resize(int width, int height)


Attribute tag

Attribute Tag

  • text applet tag

  • width, height resize

  • align alignment img

  • vspace, hspace () ()

  • code .class package


Java applet

  • codebase url .class

  • archive

    archive = a.jar, corejava/corejavaclasses.jar

  • object serialize init start state

  • name


Html 4 0

html 4.0

  • applet tag object tag

    <object>

    codetype=application/java

    classid=java:MyApplet.class

    width=100 height=150

    codebase


Applet

applet

  • html html font applet

    <applet code=FontParamApplet.class width=100 height=150>

    <param name=font value=Helvetica/>

    </applet>


Applet html getparameter

applet html getParameter

public class FontParamApplet extends JApplet{

public void init(){

String fontname = getParameter(font);

}

}

intfontSize = Integet.parseInt(getParameter(size));


Java applet

  • null

  • applet


Html applet barchart

Html applet barchart

<applet code="Chart.class" width=400 height=300>

<param name="title" value="Diameters of the Planets"/>

<param name="values" value="9"/>

<param name="name.1" value="Mercury"/>

<param name="name.2" value="Venus"/>

<param name="name.3" value="Earth"/>

<param name="name.4" value="Mars"/>

<param name="name.5" value="Jupiter"/>

<param name="name.6" value="Saturn"/>

<param name="name.7" value="Uranus"/>

<param name="name.8" value="Neptune"/>

<param name="name.9" value="Pluto"/>

<param name="value.1" value="3100"/>

<param name="value.2" value="7500"/>

<param name="value.3" value="8000"/>

<param name="value.4" value="4200"/>

<param name="value.5" value="88000"/>

<param name="value.6" value="71000"/>

<param name="value.7" value="32000"/>

<param name="value.8" value="30600"/>

<param name="value.9" value="1430"/>

</applet>

applet


Java applet


Code applet barchart

Code applet barchart

import javax.swing.*;

public class Chart extends JApplet

{

public void init()

{

EventQueue.invokeLater(new Runnable()

{

public void run()

{

String v = getParameter("values");

if (v == null) return;

int n = Integer.parseInt(v);

double[] values = new double[n];

String[] names = new String[n];

for (int i = 0; i < n; i++)

{

values[i] = Double.parseDouble(getParameter("value." + (i + 1)));

names[i] = getParameter("name." + (i + 1));

}

add(new ChartComponent(values, names, getParameter("title")));

}

});

}

}


Java applet

class ChartComponent extends JComponent

{

/**

* Constructs a ChartComponent.

* @param v the array of values for the chart

* @param n the array of names for the values

* @param t the title of the chart

*/

public ChartComponent(double[] v, String[] n, String t)

{

values = v;

names = n;

title = t;

}

public void paintComponent(Graphics g)

{

Graphics2D g2 = (Graphics2D) g;

// compute the minimum and maximum values

if (values == null) return;

double minValue = 0;

double maxValue = 0;


Java applet

for (double v : values)

{

if (minValue > v) minValue = v;

if (maxValue < v) maxValue = v;

}

if (maxValue == minValue) return;

intpanelWidth = getWidth();

intpanelHeight = getHeight();

Font titleFont = new Font("SansSerif", Font.BOLD, 20);

Font labelFont = new Font("SansSerif", Font.PLAIN, 10);

// compute the extent of the title

FontRenderContext context = g2.getFontRenderContext();

Rectangle2D titleBounds = titleFont.getStringBounds(title, context);

double titleWidth = titleBounds.getWidth();

double top = titleBounds.getHeight();

Returns the logical bounds of the specified String in the specified FontRenderContext


Java applet

Y coordinate of the upper-left corner

The baseline of the first character is at position (x,y)

// draw the title

double y = -titleBounds.getY(); // ascent

double x = (panelWidth - titleWidth) / 2;

g2.setFont(titleFont);

g2.drawString(title, (float) x, (float) y);

// compute the extent of the bar labels

LineMetricslabelMetrics = labelFont.getLineMetrics("", context);

double bottom = labelMetrics.getHeight();

y = panelHeight - labelMetrics.getDescent();

g2.setFont(labelFont);

// get the scale factor and width for the bars

double scale = (panelHeight - top - bottom) / (maxValue - minValue);

intbarWidth = panelWidth / values.length;

encapsulates the measurement information associated with a run of text

distance from the baseline to the descender line. ->the line beneath the lowercase j and y.


Java applet

// draw the bars

for (inti = 0; i < values.length; i++)

{

// get the coordinates of the bar rectangle

double x1 = i * barWidth + 1;

double y1 = top;

double height = values[i] * scale;

if (values[i] >= 0) y1 += (maxValue - values[i]) * scale;

else

{

y1 += maxValue * scale;

height = -height;

}

// fill the bar and draw the bar outline

Rectangle2D rect = new Rectangle2D.Double(x1, y1, barWidth - 2, height);

g2.setPaint(Color.RED);

g2.fill(rect);

g2.setPaint(Color.BLACK);

g2.draw(rect);

// draw the centered label below the bar

Rectangle2D labelBounds = labelFont.getStringBounds(names[i], context);

double labelWidth = labelBounds.getWidth();

x = x1 + (barWidth - labelWidth) / 2;

g2.drawString(names[i], (float) x, (float) y);

}

} // paintConponent


Java applet

private double[] values;

private String[] names;

private String title;

} //


Override1

override

public String getAppletInfo()

public String[][] getParameterInfo()

row

Name, type


Java applet

  • png, gif, jpeg

  • au, aiff, wav, midi

  • location relative url

    getDocumentBase url

    getCodeBase url codebase


Java applet

Image cat = getImage(getCodeBase(), images/cat.gif);

AudioClip meaw = getAudioClip(getCodeBase(), audio/meaw.au);

play(getCodeBase() ,audio/meaw.au)


Java applet

void play(URL url)

url

void play(URL url, String name)

relative path

AudioClipgetAudioClip(URL, String name) null

getImage


Applet browser

Applet browser

  • getAppletContext


Java applet

  • <applet code =Chart.class width = 100 height=100 name=Chart1>

    Applet chart1 = getAppletContext().getApplet(Chart1);

    chart1 applet

    ((Chart) chart1).setData(..);

    -


Java applet

Enumeration<Applet> e = getAppletContext().getApplets()l

While(e.hasMoreElements())

{

Applet a = e.nextElement();

System.out.println(a.getClass().getName());


Java applet

  • status lint

    showStatus ();

  • URL u = new URL(http:// .);

    getAppletContext().showDocument(u);

    showDocument


Two in one application applet

Two in one application applet

  • Jframe applet content pane

    public class AppletFrame extends JFrame implements AppletStub, AppletContext

    {

    public AppletFrame(Applet anApplet)

    {

    applet = anApplet;

    add(applet);


Java applet

  • main show AppletFrame

    public class AppletApplication extends NotHelloWorldApplet

    {

    public static void main(String[] args)

    {

    EventQueue.invokeLater(new Runnable()

    {

    public void run()

    {

    AppletFrame frame = new AppletFrame(new NotHelloWorldApplet());

    frame.setTitle("NotHelloWorldApplet");

    frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

    }

    });

    }


Java applet

  • setVisible AppletFrame init start

    public void setVisible(boolean b)

    {

    if (b)

    {

    applet.init();

    super.setVisible(true);

    applet.start();

    }

    else

    {

    applet.stop();

    super.setVisible(false);

    applet.destroy();

    }

    }


Java applet

  • getAppletContext().getApplet(Chart1) null

  • setStub


  • Login