Window の改良版
Written by Naoyuki Kajino.
( 改良点 )
1. windowを閉じたり、開いたりするボタンだけでなく時計もつけた.
2. window上に線を描けるようにした.しかも線の色は描いていく本数によって変わっていく.
( ソース )
import java.awt.*;
import java.util.Date;
public class window extends java.applet.Applet implements Runnable{
Frame window;
Thread runner;
Font theFont = new Font("TimesRoman", Font.BOLD+Font.ITALIC, 20);
Date theDate;
public void start(){
if(runner == null){
runner = new Thread(this);
runner.start();
}
}
public void stop(){
if(runner != null){
runner.stop();
runner = null;
}
}
public void run(){
while(true){
theDate = new Date();
repaint();
try{
Thread.sleep(1000);
}
catch (InterruptedException e){
}
}
}
public void paint(Graphics g){
FontMetrics fm = getFontMetrics(theFont);
setBackground(Color.black);
g.setColor(Color.white);
g.setFont(theFont);
int xstart = (this.size().width - fm.stringWidth(theDate.toString())) / 2;
int ystart = (this.size().height + fm.getAscent()) / 2;
g.drawString(theDate.toString(), xstart, ystart);
}
public void init(){
add(new Button("Open Window"));
add(new Button("Close Window"));
window = new MyFrame("A Popup Window");
window.resize(400,400);
window.show();
}
public boolean action(Event evt , Object obj){
if(evt.target instanceof Button){
String label = (String) obj;
if(label.equals("Open Window")){
if(!window.isShowing())
window.show();
}else if(label == "Close Window"){
if(window.isShowing())
window.hide();
}
return true;
}else
return false;
}
}
class MyFrame extends Frame{
Label l = new Label("This is a window", Label.CENTER);
Dialog dl;
TextField tf;
MyFrame(String title){
super(title);
setLayout(new FlowLayout(FlowLayout.CENTER));
add(l);
MenuBar mbar = new MenuBar();
Menu m = new Menu("Back");
m.add(new MenuItem("Red"));
m.add(new MenuItem("Blue"));
m.add(new MenuItem("Green"));
m.add(new MenuItem("Gray"));
m.add(new MenuItem("Black"));
Menu e = new Menu("Text");
e.add(new MenuItem("Set Text"));
e.add(new CheckboxMenuItem("Reverse Text"));
mbar.add(m);
mbar.add(e);
setMenuBar(mbar);
dl = new Dialog(this, "Enter Text",true);
dl.setLayout(new GridLayout(2,1,30,30));
tf = new TextField(l.getText(),20);
dl.add(tf);
dl.add(new Button("OK"));
dl.setBackground(Color.gray);
dl.resize(200,150);
}
public boolean action(Event evt, Object obj){
String label = (String)obj;
if(evt.target instanceof MenuItem){
if(label.equals("Red")){
setBackground(Color.red);
repaint();
}else if(label.equals("Blue")){
setBackground(Color.blue);
repaint();
}else if(label.equals("Green")){
setBackground(Color.green);
repaint();
}else if(label.equals("Gray")){
setBackground(Color.gray);
repaint();
}else if(label.equals("Black")){
setBackground(Color.black);
repaint();
}else if(label.equals("Set Text")){
dl.show();
}
return false;
}
if(evt.target instanceof CheckboxMenuItem){
if(l.getForeground()==Color.black){
l.setForeground(Color.white);
l.setBackground(Color.black);
l.repaint();
}else{
l.setForeground(Color.black);
l.setBackground(Color.white);
l.repaint();
}
return false;
}
if(evt.target instanceof Button){
if(label == "OK"){
l.setText(tf.getText());
dl.hide();
}
return false;
}
return true;
}
final int MAXLINES = 60;
Point starts[] = new Point[MAXLINES];
Point ends[] = new Point[60];
Point anchor;
Point current_point;
int current_line = 0;
int f[];
public boolean mouseDown(Event evt, int x, int y){
anchor = new Point (x, y);
return true;
}
public boolean mouseUp(Event evt, int x, int y){
if(current_line < MAXLINES){
addline(x, y);
}else{
System.out.println("Too many lines.");
}
current_point = null;
repaint();
return true;
}
public boolean mouseDraw(Event evt, int x, int y){
current_point = new Point(x, y);
repaint();
return true;
}
void addline(int x, int y){
starts[current_line] = anchor;
ends[current_line] = new Point(x, y);
current_line++;
}
public void paint(Graphics g){
int i;
for ( i=0; i<current_line; i++ ){
g.drawLine(starts[i].x, starts[i].y, ends[i].x, ends[i].y);
g.setColor(new Color(i*i,i+4*i,i*i+3));
}
if(current_point != null)
g.drawLine(anchor.x, anchor.y, current_point.x, current_point.y);
}
}
> Back !!