Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Last active December 28, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/7430118 to your computer and use it in GitHub Desktop.
Save shigemk2/7430118 to your computer and use it in GitHub Desktop.
やさしいJava 16
  • webブラウザ上で動作するアプレットを作成することができる
  • paint()メソッドを定義して、画面に文字やグラフィカルな要素を表示できる
  • init() start() stop() destroy() メソッドを定義して、webブラウザの動作にあわせた処理を記述できる
  • ウィンドウ部品(AWT)をアプレット上で使うことが出来る
  • スレッドによるアニメーションをアプレット上で行うことができる
<html>
<body>
<applet code = "Sample1.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
public class Sample1 extends Applet
{
public void paint(Graphics g)
{
g.drawString("ようこそJavaアプレットへ!!", 10, 10);
}
}
<html>
<body>
<applet code = "Sample2.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
public class Sample2 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(10, 10, 100, 100);
g.setFont(new Font("Serif", Font.BOLD, 24));
g.drawString("Hello", 10, 50);
}
}
<html>
<body>
<applet code = "Sample3.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class Sample3 extends Applet
{
Image img;
public void init()
{
img = getImage(getDocumentBase(), "shigemk2.gif");
}
public void paint(Graphics g)
{
g.drawImage(img, 10, 10, this);
}
}
<html>
<body>
<applet code = "Sample4.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Sample4 extends Applet implements MouseListener
{
int x = 10;
int y = 10;
public void init()
{
addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
// マウスを押した位置を調べ、画面表示を更新するようにする
x = e.getX();
y = e.getY();
repaint();
}
public void mouseReleased(MouseEvent e)
{
}
public void paint(Graphics g)
{
g.fillOval(x, y, 10, 10); // 楕円を描く
}
}
<html>
<body>
<applet code = "Sample5.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Sample5 extends Applet
{
int x = 10;
int y = 10;
// イベント処理の記述
public void init()
{
// 無名クラスを使う
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
});
}
public void paint(Graphics g)
{
g.fillOval(x, y, 10, 10); // 楕円を描く
}
}
<html>
<body>
<applet code = "Sample6.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Sample6 extends Applet implements ActionListener
{
Button bt;
// イベント処理の記述
public void init()
{
// ボタンオブジェクトを作成する
bt = new Button("開始");
add(bt);
// ボタンを押すイベントを受け取る準備をする
bt.addActionListener(this);
}
// ボタンを押したときの処理を記述する
public void actionPerformed(ActionEvent ae)
{
bt.setLabel("中止");
}
}
<html>
<body>
<applet code = "Sample7.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
public class Sample7 extends Applet implements Runnable
{
int num;
// イベント処理の記述
public void init()
{
Thread th;
th = new Thread(this);
th.start();
}
public void run()
{
try {
for(int i=0; i<10; i++) {
num = i;
repaint();
Thread.sleep(1000);
}
}
catch (InterruptedException e) {}
}
public void paint(Graphics g)
{
String str = num + "です";
g.drawString(str, 10, 10);
}
}
<html>
<body>
<applet code = "SampleP1.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
public class SampleP1 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.setFont(new Font("Serif", Font.BOLD, 20));
g.drawString("Hello", 20, 20);
}
}
<html>
<body>
<applet code = "SampleP2.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
public class SampleP2 extends Applet
{
public void paint(Graphics g)
{
g.drawRect(10, 10, 100, 100);
}
}
<html>
<body>
<applet code = "SampleP3.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Image;
public class SampleP3 extends Applet implements MouseListener
{
int x = 10;
int y = 10;
Image img;
public void init()
{
img = getImage(getDocumentBase(), "shigemk2.gif");
addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
// マウスを押した位置を調べ、画面表示を更新するようにする
x = e.getX();
y = e.getY();
repaint();
}
public void mouseReleased(MouseEvent e)
{
}
public void paint(Graphics g)
{
g.drawImage(img, x, y, this);
}
}
<html>
<body>
<applet code = "SampleP4.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class SampleP4 extends Applet implements MouseListener
{
int x = 10;
int y = 10;
String str = "";
public void init()
{
addMouseListener(this);
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
str = "こんにちは";
repaint();
}
public void mouseExited(MouseEvent e)
{
str = "さようなら";
repaint();
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void paint(Graphics g)
{
g.drawString(str, x, y);
}
}
<html>
<body>
<applet code = "SampleP5.class" width = 200 height = 100>
</applet>
</body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
public class SampleP5 extends Applet implements Runnable
{
int num;
// イベント処理の記述
public void init()
{
Thread th;
th = new Thread(this);
th.start();
}
public void run()
{
try {
for(int i=0; i<10; i++) {
num = i;
repaint();
Thread.sleep(1000);
}
}
catch (InterruptedException e) {}
}
public void paint(Graphics g)
{
String str = num + "です";
g.drawString(str, (10+num*10), 10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment