第5回セミナー

宿題の解答例

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Homework1 extends Applet implements MouseListener,MouseMotionListener {
  int[][] lines;
  int line_num;
  Image off;          // 裏画面
  int offw,offh;      // 裏画面の大きさ

  public void init(){
    lines=new int[10][4];
    line_num=0;
    addMouseListener(this);
    addMouseMotionListener(this);
    off=null;
    offw=offh=-1;
  }

  public void update(Graphics g){ paint(g); }
  public void paint(Graphics g){
    int w=getWidth(),h=getHeight();
    if(offw!=w || offh!=h)
      off=createImage(offw=w,offh=h);
    Graphics offg=off.getGraphics();
    offg.setColor(getBackground());
    offg.fillRect(0,0,w,h);
    offg.setColor(getForeground());
    paint1(offg);
    g.drawImage(off,0,0,null);
  }
  public void paint1(Graphics g){
    for(int i=0;i<line_num;i++){
      for(int j=0;j<100;j++){
        g.drawLine(lines[i][0],lines[i][1],lines[i][2],lines[i][3]);
      }
    }
  }

  // マウスリスナー
  public void mousePressed(MouseEvent ev){
    if(line_num==lines.length) return;
    lines[line_num][0]=ev.getX();
    lines[line_num][1]=ev.getY();
    lines[line_num][2]=ev.getX();
    lines[line_num][3]=ev.getY();
    line_num++;
    repaint();
  }
  public void mouseReleased(MouseEvent ev){}
  public void mouseClicked(MouseEvent ev){}
  public void mouseEntered(MouseEvent ev){}
  public void mouseExited(MouseEvent ev){}
  public void mouseDragged(MouseEvent ev){
    if(line_num-1==lines.length) return;
    lines[line_num-1][2]=ev.getX();
    lines[line_num-1][3]=ev.getY();
    repaint();
  }
  public void mouseMoved(MouseEvent ev){}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Homework1 extends Applet implements MouseListener,MouseMotionListener {

  // 線クラス
  public class Line {
    int x1,y1,x2,y2;

    // 生成
    public Line(int x1,int y1,int x2,int y2){
      this.x1=x1;
      this.y1=y1;
      this.x2=x2;
      this.y2=y2;
    }

    // 終点を変更する
    public void setEnd(int x,int y){
      x2=x;
      y2=y;
    }

    // 描画
    public void draw(Graphics g){
      g.drawLine(x1,y1,x2,y2);
    }
  }

  ArrayList<Line> lines;   // 線のリスト
  Image off;               // 裏画面
  int offw,offh;           // 裏画面の大きさ

  public void init(){
    lines=new ArrayList<Line>();
    addMouseListener(this);
    addMouseMotionListener(this);
    off=null;
    offw=offh=-1;
  }

  public void update(Graphics g){ paint(g); }
  public void paint(Graphics g){
    int w=getWidth(),h=getHeight();
    if(offw!=w || offh!=h)
      off=createImage(offw=w,offh=h);
    Graphics offg=off.getGraphics();
    offg.setColor(getBackground());
    offg.fillRect(0,0,w,h);
    offg.setColor(getForeground());
    paint1(offg);
    g.drawImage(off,0,0,null);
  }
  public void paint1(Graphics g){
    for(Line l:lines)
      l.draw(g);
  }

  // マウスリスナー
  public void mousePressed(MouseEvent ev){
    int x=ev.getX();
    int y=ev.getY();
    lines.add(new Line(x,y,x,y));
    repaint();
  }
  public void mouseReleased(MouseEvent ev){}
  public void mouseClicked(MouseEvent ev){}
  public void mouseEntered(MouseEvent ev){}
  public void mouseExited(MouseEvent ev){}
  public void mouseDragged(MouseEvent ev){
    Line l=lines.get(lines.size()-1);
    l.setEnd(ev.getX(),ev.getY());
    repaint();
  }
  public void mouseMoved(MouseEvent ev){}
}

GUIの利用

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Sample6 extends Applet implements ActionListener,ItemListener {

  public void init(){
    Button bt=new Button("開始");
    add(bt);
    bt.addActionListener(this);

    Choice ch=new Choice();
    ch.add("赤");
    ch.add("緑");
    ch.add("青");
    ch.add("黒");
    add(ch);
    ch.addItemListener(this);
  }

  public void actionPerformed(ActionEvent ev){
    String cmd=ev.getActionCommand();
    Button bt=(Button)ev.getSource();
    if(cmd.equals("開始"))
      bt.setLabel("中止");
    else
      bt.setLabel("開始");
  }

  public void itemStateChanged(ItemEvent ev){
    String it=(String)ev.getItem();
    System.out.println(it+" selected");
  }
}

保存と読み込みを実装

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class Homework1 extends Applet
  implements MouseListener,MouseMotionListener,
             ActionListener,ItemListener {

  // 線クラス
  public class Line {
    int x1,y1,x2,y2;
    Color color;
    float thickness;

    // 生成
    public Line(int x1,int y1,int x2,int y2,Color c,float t){
      this.x1=x1;
      this.y1=y1;
      this.x2=x2;
      this.y2=y2;
      color=c;
      thickness=t;
    }
    public Line(BufferedReader br) throws IOException {
      read(br);
    }

    // 終点を変更する
    public void setEnd(int x,int y){
      x2=x;
      y2=y;
    }

    // 描画
    public void draw(Graphics2D g){
      g.setColor(color);
      g.setStroke(new BasicStroke(thickness));
      g.drawLine(x1,y1,x2,y2);
    }

    // 保存
    public void save(PrintStream ps) throws IOException {
      ps.println(x1+","+y1+","+x2+","+y2+","+
                 color.getRed()+","+color.getGreen()+","+color.getBlue()+","+
                 thickness);
    }

    // 読み込み
    public void read(BufferedReader br) throws IOException {
      String l=br.readLine();
      String[] tk=l.split(",");
      x1=Integer.parseInt(tk[0]);
      y1=Integer.parseInt(tk[1]);
      x2=Integer.parseInt(tk[2]);
      y2=Integer.parseInt(tk[3]);
      int r=Integer.parseInt(tk[4]);
      int g=Integer.parseInt(tk[5]);
      int b=Integer.parseInt(tk[6]);
      color=new Color(r,g,b);
      thickness=Float.parseFloat(tk[7]);
    }
  }

  ArrayList<Line> lines;   // 線のリスト
  Image off;               // 裏画面
  int offw,offh;           // 裏画面の大きさ
  String[] col_names={ "黒","赤","緑","青","ピンク","オレンジ" };
  Color[] colors={ Color.black,Color.red,Color.green,
                   Color.blue,Color.pink,Color.orange };
  Color color;
  float thickness=1.0f;
  TextField tf_filename;

  // 初期化
  public void init(){
    lines=new ArrayList<Line>();
    addMouseListener(this);
    addMouseMotionListener(this);
    off=null;
    offw=offh=-1;
    color=Color.black;

    Choice ch=new Choice();
    for(String s:col_names)
      ch.add(s);
    add(ch);
    ch.addItemListener(this);
    ch=new Choice();
    ch.add("細");
    ch.add("中");
    ch.add("太");
    add(ch);
    ch.addItemListener(this);

    Button bt=new Button("Undo");
    add(bt);
    bt.addActionListener(this);
    bt=new Button("クリア");
    add(bt);
    bt.addActionListener(this);

    tf_filename=new TextField(10);
    add(tf_filename);

    bt=new Button("保存");
    add(bt);
    bt.addActionListener(this);
    bt=new Button("読み込み");
    add(bt);
    bt.addActionListener(this);
  }

  // 描画
  public void update(Graphics g){ paint(g); }
  public void paint(Graphics g){
    int w=getWidth(),h=getHeight();
    if(offw!=w || offh!=h)
      off=createImage(offw=w,offh=h);
    Graphics offg=off.getGraphics();
    offg.setColor(getBackground());
    offg.fillRect(0,0,w,h);
    offg.setColor(getForeground());
    paint1((Graphics2D)offg);
    g.drawImage(off,0,0,null);
  }
  public void paint1(Graphics2D g){
    for(Line l:lines)
      l.draw(g);
  }

  // 保存
  private void save(){
    String fn=tf_filename.getText();
    try{
      PrintStream ps=new PrintStream(new BufferedOutputStream(new FileOutputStream(fn)));
      ps.println(lines.size());
      for(Line l:lines)
        l.save(ps);
      ps.close();
    }
    catch(IOException e){
      System.err.println("Error: "+e);
    }
  }

  // 読み込み
  private void open(){
    lines.clear();
    String fn=tf_filename.getText();
    try{
      BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fn)));
      int n=Integer.parseInt(br.readLine());
      for(int i=0;i<n;i++)
        lines.add(new Line(br));
      br.close();
    }
    catch(IOException e){
      System.err.println("Error: "+e);
    }
  }

  // マウスリスナー
  public void mousePressed(MouseEvent ev){
    int x=ev.getX();
    int y=ev.getY();
    lines.add(new Line(x,y,x,y,color,thickness));
    repaint();
  }
  public void mouseReleased(MouseEvent ev){}
  public void mouseClicked(MouseEvent ev){}
  public void mouseEntered(MouseEvent ev){}
  public void mouseExited(MouseEvent ev){}
  public void mouseDragged(MouseEvent ev){
    Line l=lines.get(lines.size()-1);
    l.setEnd(ev.getX(),ev.getY());
    repaint();
  }
  public void mouseMoved(MouseEvent ev){}

  // アクションリスナー
  public void actionPerformed(ActionEvent ev){
    String cmd=ev.getActionCommand();
    if(cmd.equals("クリア"))
      lines.clear();
    else if(cmd.equals("Undo")){
      int n=lines.size();
      if(n>0)
        lines.remove(n-1);
    }
    else if(cmd.equals("保存")) save();
    else if(cmd.equals("読み込み")) open();
    repaint();
  }

  // アイテムリスナー
  public void itemStateChanged(ItemEvent ev){
    String it=(String)ev.getItem();
    for(int i=0;i<colors.length;i++){
      if(it.equals(col_names[i]))
        color=colors[i];
    }
    if(it.equals("細")) thickness=1.0f;
    if(it.equals("中")) thickness=4.0f;
    if(it.equals("太")) thickness=8.0f;
  }
}

Javaセミナー2014


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS