import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; abstract class Shape01 { int x, y, w, h; boolean f = false; Color c = null; public Shape01(int x, int y, int w, int h, boolean f, Color c) { this.x = x; this.y = y; this.w = w; this.h = h; this.f = f; this.c = c; } abstract void draw(Graphics g); } class Rect01 extends Shape01 { Rect01(int x, int y, int w, int h, boolean f, Color c){ super(x, y, w, h, f, c); } @Override void draw(Graphics g){ g.setColor(c); if(f){ g.fillRect(x, y, w, h); }else{ g.drawRect(x, y, w, h); } } } class Oval01 extends Shape01 { Oval01(int x, int y, int w, int h, boolean f, Color c){ super(x, y, w, h, f, c); } @Override void draw(Graphics g){ g.setColor(c); if(f){ g.fillOval(x, y, w, h); }else{ g.drawOval(x, y, w, h); } } } class Triu01 extends Shape01 { Triu01(int x, int y, int w, int h, boolean f, Color c){ super(x, y, w, h, f, c); } @Override void draw(Graphics g){ g.setColor(c); if(f){ g.fillPolygon(new int[]{x, x + w / 2, x + w}, new int[]{y + h, y, y + h}, 3); }else{ g.drawPolygon(new int[]{x, x + w / 2, x + w}, new int[]{y + h, y, y + h}, 3); } } } class Trid01 extends Shape01 { Trid01(int x, int y, int w, int h, boolean f, Color c){ super(x, y, w, h, f, c); } @Override void draw(Graphics g){ g.setColor(c); if(f){ g.fillPolygon(new int[]{x, x + w / 2, x + w}, new int[]{y, y + h, y}, 3); }else{ g.drawPolygon(new int[]{x, x + w / 2, x + w}, new int[]{y, y + h, y}, 3); } } } public class ShapeDraw01 { private JFrame frame = new JFrame("図形描画ソフト"); private JPanel pane = (JPanel)frame.getContentPane(); private JPanel panel = new JPanel(); private ButtonGroup shapegrp = new ButtonGroup(); private JRadioButton rect = new JRadioButton("■", true); private JRadioButton oval = new JRadioButton("●"); private JRadioButton triu = new JRadioButton("▲"); private JRadioButton trid = new JRadioButton("▼"); private JButton button = new JButton("色の選択"); private JPanel samp = new JPanel(); private JCheckBox fill = new JCheckBox("塗りつぶし"); private JButton clear = new JButton("消去"); private int sx, sy, cx, cy; private Color color = Color.BLACK;; private Shape01 shape = null; private boolean dragg = false; LinkedList shapelist = new LinkedList(); private Canvas canvas = new Canvas(){ @Override public void update(Graphics g){ paint(g); } @Override public void paint(Graphics g){ Rectangle rec = g.getClipBounds(); Image img = createImage(rec.width, rec.height); Graphics ofg = img.getGraphics(); for(Shape01 s: shapelist){ s.draw(ofg); } ofg.setColor(color); int x, y, w, h; if(sx <= cx){x = sx; w = cx - sx;} else{x = cx;w = sx - cx;} if(sy <= cy){y = sy; h = cy - sy;} else{y = cy;h = sy - cy;} if(rect.isSelected()){ shape = new Rect01(x, y, w, h, fill.isSelected(), color); }else if(oval.isSelected()){ shape = new Oval01(x, y, w, h, fill.isSelected(), color); }else if(triu.isSelected()){ shape = new Triu01(x, y, w, h, fill.isSelected(), color); }else if(trid.isSelected()){ shape = new Trid01(x, y, w, h, fill.isSelected(), color); } if(dragg) shape.draw(ofg); g.drawImage(img, 0, 0, this); } }; public ShapeDraw01(){ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.add(new JLabel("図形の選択")); shapegrp.add(rect); shapegrp.add(oval); shapegrp.add(triu); shapegrp.add(trid); panel.add(rect); panel.add(oval); panel.add(triu); panel.add(trid); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ev){ color = JColorChooser.showDialog(button, "色を選択して下さい", Color.WHITE); samp.setBackground(color); canvas.repaint(); } }); panel.add(button); samp.add(new JLabel(" ")); samp.setBackground(color); panel.add(samp); panel.add(fill); clear.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ev){ shapelist.removeLast(); canvas.repaint(); } }); panel.add(clear); pane.add(panel, BorderLayout.NORTH); canvas.setBackground(Color.WHITE); canvas.setSize(400, 400); canvas.addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent ev){ sx = ev.getX(); sy = ev.getY(); dragg = true; } @Override public void mouseReleased(MouseEvent ev){ shapelist.add(shape); dragg = false; } }); canvas.addMouseMotionListener(new MouseMotionAdapter(){ @Override public void mouseDragged(MouseEvent ev){ cx = ev.getX(); cy = ev.getY(); canvas.repaint(); } }); pane.add(canvas); frame.pack(); frame.setVisible(true); } public static void main(String[] args){ G106WinLF.set(); new ShapeDraw01(); } }