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