import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AnimationFace extends JFrame{ public AnimationFace(){ //画面の設定 setSize(700,700); setTitle("Java Programing"); setDefaultCloseOperation(EXIT_ON_CLOSE); //パネルを貼り付ける MyJPanel myJPanel= new MyJPanel(); Container c = getContentPane(); //コンテナの取得 c.add(myJPanel); //パネルを貼る setVisible(true); } public static void main(String[] args){ new AnimationFace(); } public class MyJPanel extends JPanel implements ActionListener{ JButton doSmaller,doBigger; double mouse_width, mouse_height, eye_shift; public MyJPanel(){ setBackground(Color.white); doSmaller = new JButton("Small mouse"); doBigger = new JButton("Big mouse"); mouse_width = 0.3; mouse_height = 0.05; eye_shift = 0; add(doSmaller); add(doBigger); doSmaller.addActionListener(this); doBigger.addActionListener(this); } //x座標の変換 public double x_change(double x, double width, double x_min, double x_max){ double new_x; new_x = (x - x_min)/(x_max - x_min) * width; return new_x; } //y座標の変換 public double y_change(double y, double height, double y_min, double y_max){ double new_y; new_y = (y - y_min)/(y_max - y_min) * height; new_y = height - new_y; return new_y; } public void paintComponent(Graphics g){ super.paintComponent(g); //画面クリアする。 Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(10));//描画の線分の幅を調整する。 Dimension d; d = getSize(); //螺旋を描く double theta, theta_step=2*Math.PI/50; double r, r_step = 0.01; double x_start,y_start, x_end, y_end; int px_start, py_start, px_end, py_end; int mouse_width_screen, mouse_height_screen; //口の幅と高さの変換 mouse_width_screen = (int)(mouse_width * d.width/2.0); mouse_height_screen = (int)(mouse_height * d.height/2.0); //Draw left eye px_start = (int) x_change(-0.5, d.width, -1,1); py_start = (int) y_change(0.5 + eye_shift, d.height, -1,1); px_end = (int) x_change(-0.5 + 0.3, d.width, -1,1); py_end = (int) y_change(0.5 + eye_shift, d.height, -1,1); g.drawLine( px_start, py_start, px_end, py_end); //Draw right eye px_start = (int) x_change(0.5, d.width, -1,1); py_start = (int) y_change(0.5 + eye_shift, d.height, -1,1); px_end = (int) x_change(0.5 - 0.3, d.width, -1,1); py_end = (int) y_change(0.5 + eye_shift, d.height, -1,1); g.drawLine( px_start, py_start, px_end, py_end); //口を描く g.drawRect( (int) x_change( - mouse_width/2 , d.width, -1,1) , (int) y_change( mouse_height/2 - 0.1, d.height, -1,1), mouse_width_screen, mouse_height_screen); } public void actionPerformed(ActionEvent e) { if(e.getSource()==doSmaller) { //口を横にする mouse_width = 0.3; mouse_height = 0.05; eye_shift = 0.0; repaint(); } if(e.getSource()==doBigger) { //口を縦にする mouse_width = 0.2; mouse_height = 0.3; eye_shift = 0.2; repaint(); } } } }