Javaサンプル(棒グラフをドラッグして値変更)


---(アプリケーションの説明)---

棒グラフの赤い点をドラッグしてテキストフィールドの数字を変更します。

---(ソースリスト)---

//Sample26
import java.applet.Applet;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Sample26 extends Applet implements MouseListener,
    MouseMotionListener {
  private static final long serialVersionUID = 845631145360088170L;
  private static final int origin_X = 100;
  private static final int origin_Y = 300;
  private static final int rect_pitch = 40;
  private static final int rect_width = 10;
  private static final String tf_init_value = "100";
  private static final int tf_size = 3;
  private static final int eps = 3;
  private static final String window_title = "棒グラフをドラッグして値変更";
  private static final int window_width = 340;
  private static final int window_height = 400;
  TextField[] tf = new TextField[2];
  static int x1 = 103;
  static int y1 = 198;
  static int x2 = 143;
  static int y2 = 198;
  static int sw = 0;
  // 初期処理
  @Override
  public void init() {
    this.setSize(Sample26.window_width, Sample26.window_height);
    this.add(new Label(Sample26.window_title));
    this.setLayout(new FlowLayout());
    for (int i = 0; i < this.tf.length; i++) {
      this.tf[i] = new TextField(Sample26.tf_init_value, Sample26.tf_size);
      this.add(this.tf[i]);
    }
    this.addMouseListener(this);
    this.addMouseMotionListener(this);
  }
  // 描画
  @Override
  public void paint(final Graphics g) {
    int rect_height;
    final int r = 2;
    g
        .clearRect(0, 0, Sample26.window_width - 1,
            Sample26.window_height - 1);
    for (int i = 0; i < this.tf.length; i++) {
      String rs = this.tf[i].getText();
      if (rs != null) {
        try {
          rect_height = Integer.parseInt(rs);
          g.setColor(Color.blue);
          g.fillRect(Sample26.origin_X + i * Sample26.rect_pitch,
              Sample26.origin_Y - rect_height,
              Sample26.rect_width, rect_height);
          g.setColor(Color.red);
          g.fillOval(Sample26.origin_X + 5 + i * Sample26.rect_pitch
              - r, Sample26.origin_Y - rect_height - r, 2 * r,
              2 * r);
          if (i == 0) {
            Sample26.y1 = Sample26.origin_Y - rect_height - r;
          }
          if (i == 1) {
            Sample26.y2 = Sample26.origin_Y - rect_height - r;
          }
        } catch (final NumberFormatException e) {
        }
      }
    }
  }
  // マウスイベント
  public void mousePressed(final MouseEvent me) {
    Sample26.sw = 0;
    final int x = me.getX();
    final int y = me.getY();
    if ((Math.abs(Sample26.x1 - x) < Sample26.eps)
        && (Math.abs(Sample26.y1 - y) < Sample26.eps)) {
      Sample26.sw = 1;
    }
    if ((Math.abs(Sample26.x2 - x) < Sample26.eps)
        && (Math.abs(Sample26.y2 - y) < Sample26.eps)) {
      Sample26.sw = 2;
    }
  }
  public void mouseDragged(final MouseEvent me) {
    if (Sample26.sw > 0) {
      final int y = me.getY();
      this.tf[Sample26.sw - 1].setText("" + (Sample26.origin_Y - 2 - y));
      this.repaint();
    }
  }
  public void mouseReleased(final MouseEvent me) {
  }
  public void mouseMoved(final MouseEvent me) {
  }
  public void mouseEntered(final MouseEvent me) {
  }
  public void mouseExited(final MouseEvent me) {
  }
  public void mouseClicked(final MouseEvent me) {
  }
}