显示表情

来源:互联网 发布:安卓手机mac修改器 编辑:程序博客网 时间:2024/04/27 21:29

 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class ButtonTest
{
   private ButtonFrame frame;
  public ButtonTest()
  {
          frame =new ButtonFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);

  }
     public class ButtonFrame extends JFrame
{
    public JPanel p2=new JPanel();
    DrawComponent f1=new DrawComponent();
    DrawComponent1 f2=new DrawComponent1();
    DrawComponent2 f3=new DrawComponent2();
    public ButtonFrame()
    {
        setTitle("ButtonTest");
        setSize(400,400);

        //构造按钮

        JButton cryButton =new JButton("流泪");
        JButton smileButton =new JButton("微笑");
        JButton unhappyButton =new JButton("生气");
        JButton tuichuButton =new JButton("退出");
        JPanel p1=new JPanel();

        JPanel p3=new JPanel();
        JPanel p4=new JPanel();
        p1.add(cryButton);
        p1.add(smileButton);
        p1.add(unhappyButton);
        p1.add(tuichuButton);
        add(p1,"South");
        add(p2,"Center");

       smileButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {

                  add(f2,"Center");
                  f2.setVisible(true);
                  p2.setVisible(false);

                  f1.setVisible(false);
                  f3.setVisible(false);


                   repaint();
                   }
        });

       // p2.add(new JLabel("welcome"));
        cryButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {

                  add(f1,"Center");
                     f1.setVisible(true);
                   p2.setVisible(false);
                  f2.setVisible(false);

                  f3.setVisible(false);
                   repaint();
            }
        });

        unhappyButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                   add(f3,"Center");
                   f3.setVisible(true);
                p2.setVisible(false);
                 f2.setVisible(false);
                 f1.setVisible(false);
                  repaint();

            }
        });
         tuichuButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                System.exit(0);
            }
        });

}

}
public static void main(String[] args)
    {
         new ButtonTest();
    }

 

 

 


//流泪
class DrawComponent extends JPanel
{
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 =(Graphics2D)g;
        //画脸
        double leftX=50;
        double topY=50;
        double width=300;
        double height=240;
        Rectangle2D rect = new Rectangle2D.Double(leftX,topY,width,height);
        Ellipse2D ellipse =new  Ellipse2D.Double();
        ellipse.setFrame(rect);
        g2.draw(ellipse);

        //画左眼
        double leftX1=110;
        double topY1=160;
        double width1=70;
        double height1=20;
        Ellipse2D ellipse1 =new  Ellipse2D.Double();
        ellipse1.setFrame(leftX1,topY1,width1,height1);
        g2.draw(ellipse1);
        //画右眼
        double a=110;
        Ellipse2D ellipse2 =new  Ellipse2D.Double();
        ellipse2.setFrame(leftX1+a,topY1,width1,height1);
        g2.draw(ellipse2);

        //画左右眉

      double x=110;//左上角x
      double y=150;//左上角y
      Arc2D arc=new Arc2D.Double(x,y,80,20,40,140,Arc2D.OPEN);
       g2.draw(arc);
       double b=110;
       Arc2D arc1=new Arc2D.Double(x+b,y,80,20,40,140,Arc2D.OPEN);
       g2.draw(arc1);
       //画嘴
       Arc2D arc2=new Arc2D.Double(150,250,100,40,15,150,Arc2D.OPEN);
       g2.draw(arc2);
       Arc2D arc3=new Arc2D.Double(150,240,100,60,15,150,Arc2D.OPEN);
       g2.draw(arc3);

       //画眼球

        Ellipse2D circle1 =new  Ellipse2D.Double();//左眼
        circle1.setFrameFromCenter(145,170,155,180);
        g2.draw(circle1);
        g2.setPaint(Color.BLACK);
        g2.fill(circle1);

        Ellipse2D circle2 =new  Ellipse2D.Double();//右眼
        circle2.setFrameFromCenter(255,170,265,180);
        g2.draw(circle2);
        g2.setPaint(Color.BLACK);
        g2.fill(circle2);

        //流泪
        Ellipse2D circle3 =new  Ellipse2D.Double();//左眼第一滴
        circle3.setFrameFromCenter(145,200,147,195);
        g2.draw(circle3);
        g2.setPaint(Color.BLACK);
        g2.fill(circle3);

        Ellipse2D circle4 =new  Ellipse2D.Double();//左眼第二滴
        circle4.setFrameFromCenter(145,220,147,215);
        g2.draw(circle4);
        g2.setPaint(Color.BLACK);
        g2.fill(circle4);

        Ellipse2D circle5 =new  Ellipse2D.Double();//右眼第一滴
        circle5.setFrameFromCenter(255,200,257,195);
        g2.draw(circle5);
        g2.setPaint(Color.BLACK);
        g2.fill(circle5);

         Ellipse2D circle6 =new  Ellipse2D.Double();//右眼第二滴
        circle6.setFrameFromCenter(255,220,257,215);
        g2.draw(circle6);
        g2.setPaint(Color.BLACK);
        g2.fill(circle6);

 

 }
}

 

 

 

 

//微笑
class DrawComponent1 extends JPanel
{
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 =(Graphics2D)g;
        //画脸
        double leftX=50;
        double topY=50;
        double width=300;
        double height=240;
        Rectangle2D rect = new Rectangle2D.Double(leftX,topY,width,height);
        Ellipse2D ellipse =new  Ellipse2D.Double();
        ellipse.setFrame(rect);
        g2.draw(ellipse);

        //画左眼
        double leftX1=110;
        double topY1=160;
        double width1=70;
        double height1=20;
        Ellipse2D ellipse1 =new  Ellipse2D.Double();
        ellipse1.setFrame(leftX1,topY1,width1,height1);
        g2.draw(ellipse1);
        //画右眼
        double a=110;
        Ellipse2D ellipse2 =new  Ellipse2D.Double();
        ellipse2.setFrame(leftX1+a,topY1,width1,height1);
        g2.draw(ellipse2);

        //画左右眉

      double x=110;//左上角x
      double y=150;//左上角y
      Arc2D arc=new Arc2D.Double(x,y,80,20,40,140,Arc2D.OPEN);
       g2.draw(arc);
       double b=110;
       Arc2D arc1=new Arc2D.Double(x+b,y,80,20,40,140,Arc2D.OPEN);
       g2.draw(arc1);
       //画嘴
       Arc2D arc2=new Arc2D.Double(150,230,100,40,200,140,Arc2D.OPEN);
       g2.draw(arc2);
       Arc2D arc3=new Arc2D.Double(150,215,100,60,200,140,Arc2D.OPEN);
       g2.draw(arc3);

       //画眼球

        Ellipse2D circle1 =new  Ellipse2D.Double();//左眼
        circle1.setFrameFromCenter(145,170,155,180);
        g2.draw(circle1);
        g2.setPaint(Color.BLACK);
        g2.fill(circle1);

        Ellipse2D circle2 =new  Ellipse2D.Double();//右眼
        circle2.setFrameFromCenter(255,170,265,180);
        g2.draw(circle2);
        g2.setPaint(Color.BLACK);
        g2.fill(circle2);
 }
}

 

 

 

 

 

//生气
class DrawComponent2 extends JPanel
{
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 =(Graphics2D)g;
        //画脸

        double leftX=50;
        double topY=50;
        double width=300;
        double height=240;
        Rectangle2D rect = new Rectangle2D.Double(leftX,topY,width,height);
        Ellipse2D ellipse =new  Ellipse2D.Double();
        ellipse.setFrame(rect);
        g2.draw(ellipse);
        //画左眼
        double leftX1=110;
        double topY1=160;
        double width1=70;
        double height1=20;
        Ellipse2D ellipse1 =new  Ellipse2D.Double();
        ellipse1.setFrame(leftX1,topY1,width1,height1);
        g2.draw(ellipse1);
        //画右眼
        double a=110;
        Ellipse2D ellipse2 =new  Ellipse2D.Double();
        ellipse2.setFrame(leftX1+a,topY1,width1,height1);
        g2.draw(ellipse2);

        //画左右眉

      double x=110;//左上角x
      double y=150;//左上角y
      Arc2D arc=new Arc2D.Double(x,y,80,20,40,140,Arc2D.OPEN);
       g2.draw(arc);
       double b=110;
       Arc2D arc1=new Arc2D.Double(x+b,y,80,20,40,140,Arc2D.OPEN);
       g2.draw(arc1);
       //画嘴
       Arc2D arc2=new Arc2D.Double(150,250,100,40,15,150,Arc2D.OPEN);
       g2.draw(arc2);
       Arc2D arc3=new Arc2D.Double(150,240,100,60,15,150,Arc2D.OPEN);
       g2.draw(arc3);

       //画眼球

        Ellipse2D circle1 =new  Ellipse2D.Double();//左眼
        circle1.setFrameFromCenter(145,170,155,180);
        g2.draw(circle1);
        g2.setPaint(Color.BLACK);
        g2.fill(circle1);

        Ellipse2D circle2 =new  Ellipse2D.Double();//右眼
        circle2.setFrameFromCenter(255,170,265,180);
        g2.draw(circle2);
        g2.setPaint(Color.BLACK);
        g2.fill(circle2);
     }
}

}

 

 

原创粉丝点击