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