8-1 使用按钮选择绘制不同图形

1、需要连续点击两下才能显示
2、已经生成的不能消除

–2022年10月24日–

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javafx.geometry.Dimension2D;
import java.awt.geom.*;
abstract class MyShape {
   private double x1,y1,x2,y2;
   MyShape() {
       this.x1=this.y1=this.x2=this.y2=0;
   }
   MyShape(double a, double b, double c, double d) {
       this.x1=a;this.y1=b; 
       this.x2=c;this.y2=d;
   }
   public double getX1() {
       return x1;
   }
   public void setX1(double x1) {
       this.x1 = x1;
   }
   public double getY1() {
       return y1;
   }
   public void setY1(double y1) {
       this.y1 = y1;
   }
   public double getX2() {
       return x2;
   }
   public void setX2(double x2) {
       this.x2 = x2;
   }
   public double getY2() {
       return y2;
   }
   public void setY2(double y2) {
       this.y2 = y2;
   }
   abstract void draw(Graphics g);
}
class MyLine extends MyShape {
   MyLine(double a,double b,double c,double d){
       super(a,b,c,d);
   }
   public void draw(Graphics g) {
       Graphics2D g2=(Graphics2D)g;
       Line2D t=new Line2D.Double();
       t.setLine(getX1(), getY1(), getX2(), getY2());
       g2.draw(t);
   }
}
class MyRectangle extends MyShape {
   MyRectangle(double a,double b,double c,double d){
       super(a,b,c,d);
   }
   public void draw(Graphics g) {
       Graphics2D g2=(Graphics2D)g;
       Rectangle2D t=new Rectangle2D.Double();
       t.setRect(getX1(), getY1(), getX2(), getY2());
       g2.draw(t);
   }
}
class MyOval extends MyShape {
   MyOval(double a,double b,double c,double d){
       super(a,b,c,d);
   }
   public void draw(Graphics g) {
       Graphics2D g2=(Graphics2D)g;
       Ellipse2D t=new Ellipse2D.Double();
       t.setFrame(getX1(), getY2(), Math.abs(getX1()-getX2()), Math.abs(getY1()-getY2()));
       g2.draw(t);
   }
}
class myJFrame extends JFrame {
   private JButton btn1,btn2,btn3;
   private JPanel all;

   class draw1 extends JComponent {
      public void paintComponent(Graphics g){
         int a=500;
         int b=250;
         int c=250;
         int d=150;
        MyOval t=new MyOval(a,b,c,d);
        t.draw(g);
      }
   }
   class draw2 extends JComponent {
      public void paintComponent(Graphics g){
         int a=100;
         int b=100;
         int c=100;
         int d=100;
        MyRectangle t=new MyRectangle(a,b,c,d);
        t.draw(g);
      }
   }
   class draw3 extends JComponent {
      public void paintComponent(Graphics g){
         int a=100;
         int b=350;
         int c=900;
         int d=350;
        MyLine t=new MyLine(a,b,c,d);
        t.draw(g);
      }
   }

    public myJFrame(){
        all =new JPanel();
        all.setLayout(null);

        btn1=new JButton("椭圆");
        btn1.setBounds(5, 400, 200, 40);
        btn1.setFont(new Font("微软雅黑",Font.BOLD,20));
        all.add(btn1);

        btn2=new JButton("矩形");
        btn2.setBounds(330, 400, 200, 40);
        btn2.setFont(new Font("微软雅黑",Font.BOLD,20));
        all.add(btn2);

        btn3=new JButton("直线");
        btn3.setBounds(655, 400, 200, 40);
        btn3.setFont(new Font("微软雅黑",Font.BOLD,20));
        all.add(btn3);

        MyListener t =new MyListener();
        //给按钮添加事件监听
        btn1.addActionListener(t);    //传this指针方便判断
        btn2.addActionListener(t);
        btn3.addActionListener(t);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("绘制图形");
        setBounds(0,0,1000,500);
        
        add(all);
        setVisible(true);
   }
   class  MyListener implements ActionListener {//内部类实现
      public void actionPerformed(ActionEvent e){
         if(e.getSource()==btn1){  
            repaint();
            add(all);
            add(new draw1());
            setVisible(true);
         }
         else if(e.getSource()==btn2){
            repaint();
            add(all);
            add(new draw2());
            setVisible(true);
         }else{       
            repaint();
            add(all);
            add(new draw3());
            setVisible(true);
         }
      }
   }
} 
public class Main {
    public static void main(String[] args) {
        myJFrame myjf=new myJFrame();
    }
}

Update 2022年10月25日

根据老师给的题解代码调试的,达到了想要的效果

(老师YYDS)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javafx.geometry.Dimension2D;
import java.awt.geom.*;
class myJFrame extends JFrame {

	private mydraw huatu;

    public myJFrame(){
		
		huatu = new mydraw();
		add(huatu, BorderLayout.CENTER);

        JPanel all =new JPanel();

        JButton btn1=new JButton("椭圆");
        all.add(btn1);
        JButton btn2=new JButton("矩形");
        all.add(btn2);
        JButton btn3=new JButton("直线");
        all.add(btn3);
        btn1.addActionListener(new MyListener(1));
        btn2.addActionListener(new MyListener(2));
        btn3.addActionListener(new MyListener(3));
	
		add(all,BorderLayout.SOUTH);
    }
	private class MyListener implements ActionListener {//内部类实现
		int id;
		MyListener(int t){
			this.id=t;
		}
		public void actionPerformed(ActionEvent e){
			huatu.SetId(this.id);
			repaint();
		}
	}
   class mydraw extends JComponent {
		private int id;
		public void SetId(int t){
			this.id=t;
		}
		public void paintComponent(Graphics g){
			Graphics2D g2 = (Graphics2D) g;
			Ellipse2D elipse = new Ellipse2D.Double(500,250,250,150);
			Rectangle2D rect = new Rectangle2D.Double(100,100,100,100);
			Line2D line = new Line2D.Double(100,350,900,350);
			
			if(this.id==1) g2.draw(elipse);
			if(this.id==2) g2.draw(rect);
			if(this.id==3) g2.draw(line);
		}
	}
} 

public class Main {
    public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
					myJFrame myjf = new myJFrame();
					myjf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					myjf.setTitle("绘制图形");
					myjf.setBounds(0,0,1000,500);
					myjf.setVisible(true);
			}
		});
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值