001:import java.awt.event.* ;
002:import javax.swing.event.ChangeEvent;
003:import javax.swing.event.ChangeListener;
004:import javax.swing.* ;
005:import java.awt.image.BufferedImage ;
006:import java.awt.Graphics ;
007:import java.awt.Dimension ;
008:import java.awt.Color ;
009:import java.awt.geom.* ;
010:import java.awt.Graphics2D ;
011:import java.awt.BasicStroke ;
012:import java.awt.BorderLayout;
013:
014:class G2Canvas extends JPanel {
015:    Point2D.Double p1 = new Point2D.Double();  // 始点
016:    Point2D.Double p2 = new Point2D.Double();  // 終点
017:    Line2D.Double  line = new Line2D.Double(); // 直線
018:    BufferedImage image = null;      // イメージ
019:    Graphics2D    imageGraphics;     // イメージのGraphics
020:    final Color bc = Color.WHITE;    // 背景色
021:          Color dc = Color.RED;      // 描画色
022:    float thickness;                 // 線の太さ(初期値は main で設定)
023:    BasicStroke stroke;              // 描画のストローク
024:
025:    int width  = 300;
026:    int height = 300;
027:
028:    public G2Canvas() {
029:        addMouseMotionListener(new MouseMotionAdapter() {
030:            public void mouseDragged(MouseEvent e) {
031:                p1.setLocation(p2);
032:                p2.setLocation((double)e.getX(), (double)e.getY());
033:                repaint();
034:            }
035:        });
036:        addMouseListener(new MouseAdapter() {
037:            public void mousePressed(MouseEvent e) {
038:                p1.setLocation((double)e.getX(), (double)e.getY());
039:                p2.setLocation(p1);
040:            }
041:        });
042:    }
043:
044:    public void paintComponent(Graphics g) {
045:        if(image==null) {
046:            Dimension  d  = getSize();
047:            image  = (BufferedImage)createImage(d.width, d.height);
048:            imageGraphics = (Graphics2D)image.createGraphics();
049:            imageGraphics.setBackground(bc);
050:            imageGraphics.clearRect(0, 0, d.width, d.height);
---:(2行削除)
051:        }
052:        if(!p1.equals(p2)) {
053:            line.setLine(p1,p2);
054:            imageGraphics.setPaint(dc);
055:            imageGraphics.setStroke(stroke);
056:            imageGraphics.draw(line);
057:            p1.setLocation(p2);
058:        }
059:        g.drawImage(image, 0, 0, this);
060:    }
061:
062:    void setThickness(float t) {
063:        thickness = t;
064:        stroke = new BasicStroke(thickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
065:    }
066:
067:    @Override
068:    public Dimension getPreferredSize() { return new Dimension(width, height); }
069:}
070:
071:public class Graph9b {
072:    public static void main(String[] args) {
073:        JFrame frame = new JFrame();
074:        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
075:        frame.getContentPane().setLayout(new BorderLayout());
076:
077:        final G2Canvas displayPanel = new G2Canvas();
078:
079:        final JPanel panel = new JPanel();
080:
081:        final JLabel colorIndicator = new JLabel("  ");
082:        panel.add(colorIndicator);
083:
084:        final JButton button = new JButton("色選択");
085:        button.addActionListener(new ActionListener() {
086:            public void actionPerformed(ActionEvent e) {
087:                Color c = JColorChooser.showDialog(null, "描画色", displayPanel.dc);
088:                if(c != null) displayPanel.dc = c;
089:                colorIndicator.setBackground(c);
090:                displayPanel.repaint();
091:            }
092:        });
093:        colorIndicator.setOpaque(true);
094:        colorIndicator.setBackground(displayPanel.dc);
095:        panel.add(button);
096:
097:        final JSlider slider = new JSlider(1,9,3);
098:        displayPanel.setThickness((float)slider.getValue());
099:        slider.addChangeListener(new ChangeListener() {
100:            public void stateChanged(ChangeEvent e) {
101:                displayPanel.setThickness((float)slider.getValue());
102:            }
103:        });
104:        slider.setMajorTickSpacing(2);
105:        slider.setPaintTicks(true);
106:        slider.setPaintLabels(true);
107:        slider.setSnapToTicks(true);
108:        panel.add(slider, BorderLayout.SOUTH);
109:
110:        frame.getContentPane().add(displayPanel, BorderLayout.CENTER );
111:        frame.getContentPane().add( panel, BorderLayout.SOUTH );
112:
113:        frame.pack();
114:        frame.setVisible(true);
115:    }
116:}