JApplet 클래스를 이용한 애플릿 구현
Applet 클래스와 JApplet 클래스의 차이점
- 사용할 수 있는 범위
- Applet : 보안과 관련된 부분에서 예외가 많이 발생
- JApplet : 로컬시스템과 결합하기 전까지 예외가 발생하지 않음
주의할 점
- 그래픽을 표현할 때 paint()라는 메서드를 JApplet을 상속받은 클래스와 함께 쓰지 말아야 한다.
- Canvas와 같이 그래픽을 표현할 수 있는 다른 클래스의 도움을 받아 표시하는 것이 좋다.
JApplet 그래픽 표현 예제
import java.awt.*;
import javax.swing.*;
class Round23_Ex01_Sub extends Canvas {
public void paint(Graphics g) {
g.drawLine(50, 50, 100, 100);
g.setColor(Color.red);
g.fillRect(100, 100, 50, 50);
}
}
public class Round23_Ex01 extends JApplet {
private Container con;
private JPannel jp = new JPannel(new FlowLayout(FlowLayout.RIGHT));
private JButton bt = new JButton("확인");
private JButton bt1 = new JButton("취소");
private Round23_Ex01_Sub es = new Round23_Ex01_Sub();
public void init() {
con = this.getContentPane();
con.setLayout(new BorderLayout());
jp.add(bt);
jp.add(bt1);
con.add("South", jp);
con.add("Center", es);
}
public void start() {
}
public void stop() {
}
public void destroy() {
}
}
Round23_Ex01.html
<html>
<head>
<!-- header - edit "Data/yourHtmlHeader" to customize -->
<!-- contents - edit "EventHandlers/Html file/onCreate" to customize -->
<meta name="generator" content="ModelWorks IDE" />
<title></title>
</head>
<body>
<center>
<p>이것이 JApplet 입니다.</p>
<applet code="Round23_Ex01.class" width=300 height=200 ></applet>
<hr>
<a href="Round23_Ex01.java">소스보기</a>
</center>
</body>
</html>
JApplet 으로 JMenu 구현
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Round23_Ex02 extends JApplet implements ActionListener {
private JMenuBar jmb = new JMenuBar();
private JMenu file = new JMenu("File");
private JMenu help = new JMenu("Help");
private JMenuItem fnew = new JMenuItem("New File");
private JMenuItem fstop = new JMenuItem("Stop");
public void init() {
this.setJMenuBar(jmb);
jmb.add(file);
jmb.add(help);
fnew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
fstop.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
file.add(fnew);
file.addSeparator();
file.add(fstop);
}
public void start() {
fnew.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == fnew) {
this.showStatus("New File 키를 눌렀습니다.");// 상태바에 문자열 띄우기
}
}
}
Round23_Ex02.html
<html>
<head>
<!-- header - edit "Data/yourHtmlHeader" to customize -->
<!-- contents - edit "EventHandlers/Html file/onCreate" to customize -->
<meta name="generator" content="ModelWorks IDE" />
<title></title>
</head>
<body>
<center>
<applet code="Round23_Ex02.class" width=300 height=200 ></applet>
</center>
</body>
</html>
아날로그 시계와 디지털 시계
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class Round23_Ex03_Sub extends Canvas implements Runnable {
private Thread timeThread;
private int h, m, s; // 시 분 초의 값을 기억
private xh, yh, xm, ym, xs, ys; // 현재의 시 분 초의 좌표를 기억
private xh1, yh1, xm1, ym1, xs1, ys1; // 바로 이전의 시 분 초의 좌표를 기억
private int xcenter = 100; // 중심점의 x좌표
private int ycenter = 100; // 중심점의 y좌표
public Round23_Ex03_Sub() {
timeThread = new Thread(this);
timeThread.start();
}
public void updatge(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
Calendar ca = Calendar.getInstance();
h = (int)ca.get(Calendar.HOUR_OF_DAY); // new Date().getHours();
m = (int)ca.get(Calendar.MINUTE);
s = (int)ca.get(Calendar.SECOND);
xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 60 + xcenter);
yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 60 + ycenter);
xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 90 + xcenter);
ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 90 + ycenter);
xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 90 + xcenter);
ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 90 + ycenter);
g.drawOval(0, 0, 200, 200); // 원
g.fillOval(98, 98, 4, 4); // 중심원
g.drawString("9", xcenter - 90, ycenter + 3));
g.drawString("3", xcenter - 80, ycenter + 3));
g.drawString("12", xcenter - 5, ycenter + 83));
g.drawString("6", xcenter - 3, ycenter + 93));
// 과거의 선들을 지우는 방법
g.setColor(this.getBackground());
if( xh != xh1 || yh != yh1 ) {
d.drawLine(xcenter, ycenter-2, xh1, yh1);
d.drawLine(xcenter-2, ycenter, xh1, yh1);
}
if( xm != xm1 || ym != ym1 ) {
d.drawLine(xcenter, ycenter-1, xm1, ym1);
d.drawLine(xcenter-1, ycenter, xm1, ym1);
}
if( xs != xs1 || ys != ys1 ) {
d.drawLine(xcenter, ycenter, xs1, ys1);
}
g.setColor(Color.black);
g.drawLine(xcenter, ycenter-1, xs, ys);
g.setColor(Color.red);
g.drawLine(xcenter, ycenter-1, xm, ym);
g.drawLine(xcenter-1, ycenter, xm, ym);
g.setColor(Color.blue);
g.drawLine(xcenter, ycenter-2, xh, yh);
g.drawLine(xcenter-2, ycenter, xh, yh);
xh1 = xh;
yh1 = yh;
xm1 = xm;
ym1 = ym;
xs1 = xs;
ys1 = ys;
}
public void run() {
while( timeThread != null ) {
try { Thread.sleep(1000); } catch ( InterruptedException e){}
repaint();
}
}
public void stop() {
timeThread = null;
}
}
public class Round23_Ex03 extends JApplet implements Runnable {
private Container con;
private BorderLayout bl = new BorderLayout();
private JTextField jtf = new JTextField();
private Thread timeThread;
private Round23_Ex03_Sub es = new Round23_Ex03_Sub();
public void init() {
con = this.getContentPane();
con.setLayout(bl);
jtf.setEnabled(false);
jtf.setDiabledTextColor(Color.magenta);
jtf.setFont(new Font("TimesRoman", Font.BOLD, 15));
jtf.setBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED));
jtf.setHorizontalAlignment(SwingConstants.CENTER);
con.add("Center",es);
con.add("South", jtf);
}
public void start() {
timeThread = new Thread(this);
timeThread.start();
}
public void stop() {
while( timeThread != null) {
try { Thread.sleep(1000); } catch ( InterruptedException e){}
timeChange();
}
}
private void timeChange() {
Calendar ca = Calendar.getInstance();
Date dd = new Date();
jtf.setText(dd.toString());
}
}
Roun23_Ex03.html
<html>
<head>
<!-- header - edit "Data/yourHtmlHeader" to customize -->
<!-- contents - edit "EventHandlers/Html file/onCreate" to customize -->
<meta name="generator" content="ModelWorks IDE" />
<title></title>
</head>
<body>
<center>
<h2>시간을 표시합니다.</h2>
<applet code="Round23_Ex03.class" width=300 height=350 ></applet>
<hr>
<a href="Round23_Ex03.java">소스보기</a>
</center>
</body>
</html>