001:import java.awt.event.* ; // ActionEvent MouseEvent MouseAdapter WindowEvent WindowAdapter
002:import javax.swing.* ; // ButtonGroup JFrame JLabel JPanel JRadioButton JTextField
003:import java.awt.* ; // BorderLayout Color Container Dimension Graphics Graphics2D
004:import java.awt.geom.* ; // RectangularShape Ellipse2D.Double Rectangle2D.Double
005:
006:interface ColoredShape extends Shape {
007:(削除)
007: public double getX();
008: public double getY();
009: public double getWidth();
010: public double getHeight();
011: public void setFrame(double x, double y, double w, double h);
012: public void draw(Graphics2D gg);
013:}
014:
015:class ColoredEllipse extends Ellipse2D.Double
016: implements ColoredShape {
017: Color color;
018:
019: ColoredEllipse(double x, double y, double w, double h, Color c) {
020: super(x, y, w, h);
021: color = c;
022: }
023:
024:(削除)
024: public double getX() { return super.getX(); }
025: public double getY() { return super.getY(); }
026: public double getWidth() { return super.getWidth(); }
027: public double getHeight() { return super.getHeight(); }
028: public void setFrame(double x, double y, double w, double h) { super.setFrame(x, y, w, h); }
029: public void draw(Graphics2D gg) {
030: gg.setColor(color);
031: gg.fill(this);
032: }
033:}
034:
035:class ColoredRectangle extends Rectangle2D.Double
036: implements ColoredShape {
037: Color color;
038:
039: ColoredRectangle(double x, double y, double w, double h, Color c) {
040: super(x, y, w, h);
041: color = c;
042: }
043:
044:(削除)
044: public double getX() { return super.getX(); }
045: public double getY() { return super.getY(); }
046: public double getWidth() { return super.getWidth(); }
047: public double getHeight() { return super.getHeight(); }
048: public void setFrame(double x, double y, double w, double h) { super.setFrame(x, y, w, h); }
049: public void draw(Graphics2D gg) {
050: gg.setColor(color);
051: gg.fill(this);
052: }
053:}
054:
055:public class Graph2d2y extends JPanel {
056: static int shapetype = 0; // ものの形状 0:楕円 1:矩形
057: final static Color bc = Color.white; // 背景色
058: static Color dc = Color.red; // 描画色
059: final static JTextField tw = new JTextField("30",3); // ものの幅
060: final static JTextField th = new JTextField("30",3); // ものの高さ
061: final int MAX_NUM = 100; // ものの最大個数
062: int nshape = 0; // ものの個数
063: ColoredShape[] shape = new ColoredShape[MAX_NUM]; // 矩形のもの
064: double x, y; // ドラッグ前の座標
065:
066: public Graph2d2y() {
067: addMouseListener(new MouseAdapter() {
068: public void mouseClicked(MouseEvent e) { // マウスがクリックされた場合
069: double x = (double)e.getX(); // カーソルの座標
070: double y = (double)e.getY();
071: double w = Double.parseDouble(tw.getText().trim()); // 下欄に表示中の幅
072: double h = Double.parseDouble(th.getText().trim()); // 高さ
073: RectangularShape s = null;
074: if(nshape<MAX_NUM && shapetype==0) { // 下欄で楕円を指定中
075: shape[nshape++] =
076: new ColoredEllipse(x-w/2.0, y-h/2.0, w, h, dc); // 楕円の生成
077: } else if(nshape<MAX_NUM && shapetype==1) {
078: shape[nshape++] =
079: new ColoredRectangle(x-w/2.0, y-h/2.0, w, h, dc); // 矩形の生成
080: } else {
081: System.out.println("これ以上書けません");
082: }
083: repaint(); // 再描画
084: }
085: public void mousePressed(MouseEvent e) { // マウスが押された場合
086: x = (double)e.getX(); // カーソルの座標を保存
087: y = (double)e.getY();
088: }
089: });
090: addMouseMotionListener(new MouseMotionAdapter() {
091: public void mouseDragged(MouseEvent e) { // マウスがドラッグされた場合
092: double newx = (double)e.getX(); // 新しい座標
093: double newy = (double)e.getY();
094: for(int i=0; i<nshape; i++) { // 保存してある全図形について
095: if(shape[i].contains(x,y)){ // カーソルが触れている場合
096: shape[i].setFrame(shape[i].getX() + newx - x, // 図形を移動
097: shape[i].getY() + newy - y,
098: shape[i].getWidth(), shape[i].getHeight());
099: }
100: }
101: x = newx; // 新しい座標を保存
102: y = newy;
103: double w = (double)getWidth(); // 表示枠の幅と高さ
104: double h = (double)getHeight();
105: int j=0; // 保存してある全図形について
106: while(j<nshape) {
107: if(!shape[j].intersects(0.0, 0.0, w, h)) { // 表示枠の外にある場合
108: for(int i=j+1; i<nshape; i++) shape[i-1] = shape[i]; // その図形を消去
109: nshape--;
110: }
111: j++;
112: }
113: repaint(); // 再描画
114: }
115: });
116: setBackground(bc);
117: }
118:
119: public void paintComponent(Graphics g) {
120: super.paintComponent(g);
121: Graphics2D gg = (Graphics2D)g;
122: for(int i=0; i<nshape; i++) { // 保存してある全図形について
123: shape[i].draw(gg); // 描画
124: }
125: }
126:
127: public static void main(String[] args) {
128: JFrame frame = new JFrame();
129: frame.addWindowListener(new WindowAdapter() {
130: public void windowClosing(WindowEvent e) { System.exit(0); }
131: });
132: final Container cont = frame.getContentPane();
133: cont.setLayout(new BorderLayout());
134: cont.add( new Graph2d2y(), BorderLayout.CENTER );
135: JPanel southpan = new JPanel(); // ボタンをならべるパネル
136: JButton button = new JButton("色選択"); // カラーチューザを呼び出すボタン
137: button.addActionListener(new ActionListener() {
138: public void actionPerformed(ActionEvent e) {
139: Color c = JColorChooser.showDialog(cont, "Choose", Color.white);
140: if( c!=null ) dc = c;
141: }
142: });
143: southpan.add(button);
144: JRadioButton buttonE = new JRadioButton("楕円", true); // ラジオボタン
145: buttonE.addActionListener(new ActionListener() {
146: public void actionPerformed(ActionEvent e) {
147: shapetype = 0;
148: }
149: });
150: JRadioButton buttonR = new JRadioButton("矩形", false); // ラジオボタン
151: buttonR.addActionListener(new ActionListener() {
152: public void actionPerformed(ActionEvent e) {
153: shapetype = 1;
154: }
155: });
156: ButtonGroup bg = new ButtonGroup(); //
157: bg.add(buttonE);
158: bg.add(buttonR);
159: southpan.add(buttonE);
160: southpan.add(buttonR);
161: southpan.add(new JLabel(" 幅"));
162: southpan.add(tw);
163: southpan.add(new JLabel(" 高さ"));
164: southpan.add(th);
165: cont.add( southpan, BorderLayout.SOUTH);
166: frame.setSize(400, 300);
167: frame.setVisible(true);
168: }
169:}