画图 画椭圆

package com.expriment;

//filename App14_1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class huitu extends JFrame
{
    
    
     static JButton b1=new JButton("画圆");
     static JButton b2=new JButton("画椭圆");
     static JPanel pan=new MyPanel();
     static huitu frm = new huitu();
     static int circle=0;
     public static void main(String[] args)
     {
    
    
           //App14_1 frm=new App14_1();
           frm.setVisible(true);
           frm.setTitle("简单绘图应用程序");
           frm.setSize(300,250);
           frm.setLayout(null);
           frm.add(pan);
           pan.setBounds(0,0,300,250);
           frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
     }
     static class MyPanel extends JPanel implements ActionListener
     {
    
    
           MyPanel()
           {
    
      
                      
                this.add(b1);   this.add(b2);      
                b1.addActionListener(this);
                b2.addActionListener(this);
            }
            public void actionPerformed(ActionEvent e)
            {
    
    
               JButton bt=(JButton)e.getSource();
               if (bt==b1) circle=1;
               else circle=2;
               Graphics g=this.getGraphics();
               repaint();
           }
            public void paintComponent(Graphics g)
           {
    
    
                super.paintComponent(g);
                b1.setBounds(50,180,80,25);
                b2.setBounds(150,180,80,25); 
                g.setFont(new Font("楷体",Font.ITALIC,20));
                g.setColor(Color.RED);
                g.drawString("画圆或椭圆",100,30);
                if (circle==1) g.drawOval(100,70,70,70);
                else if (circle==2) g.drawOval(80,40,70,120);
            }
     }        
}


在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44659084/article/details/109842224