SWUST--Java实验(三) GUI绘图并求面积

import java.awt.Graphics;

public class Cycle extends Figure {
	double a;

	public Cycle() {

	}

	public Cycle(double a1) {
		a = a1;
	}

	public void Area() {
		System.out.println("圆面积:" + Math.PI * a / 2 * a / 2);
	}

	@Override
	void drawSquare(Graphics g) {
		// TODO Auto-generated method stub
		g.drawOval(65, 250, 120, 120);
	}
}
import java.awt.Color;
import java.awt.Graphics;

public class RightTriangle extends Figure {
	double a;

	public RightTriangle() {

	}

	public RightTriangle(double a1) {
		a = a1;
	}

	public void Area() {
		System.out.println("三角形面积:" + a * a / 2);
	}

	@Override
	void drawSquare(Graphics g) {
		// TODO Auto-generated method stub
		g.drawLine(70, 100, 70, 220);
		g.drawLine(70, 220, 190, 220);
		g.drawLine(70, 100, 190, 220);
	}

}
import java.awt.*;

public class Square extends Figure {
	double a;

	public Square() {

	}

	public Square(double a1) {
		a = a1;
	}

	public void Area() {
		System.out.println("正方形面积:" + a * a);
	}

	@Override
	void drawSquare(Graphics g) {
		// TODO Auto-generated method stub
		g.drawRect(250, 250, 120, 120);
	}
}
import java.awt.Graphics;

public abstract class Figure {

	public void Area() {
	}

	abstract void drawSquare(Graphics g);

}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;
import java.util.Scanner;

import javax.swing.*;
import javax.swing.border.Border;

public class TestSquare extends JFrame {
	static {
		System.out.print("1.三角形  2.圆  3.正方形\n选择执行:");
	}
		
	private JPanel jp = new JPanel();
	private JLabel jl = new JLabel("←选择图形");
	public TestSquare(){

		JButton bt[] = new JButton[3];		   
		for(int i=0;i<bt.length;i++)
		{
			bt[i] = new JButton();
			jp.add(bt[i]);
		}
        bt[0].setText("三角形");
        bt[1].setText("圆");
        bt[2].setText("正方形");
		
        bt[1].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				Graphics g = getGraphics();
				Cycle s = new Cycle();
				g.setColor(Color.BLUE);
				s.drawSquare(g);
			}
		});
        
        bt[0].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				Graphics g = getGraphics();
				RightTriangle s = new RightTriangle();
				g.setColor(Color.red);
				s.drawSquare(g);
			}
		});
        
        bt[2].addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
                   Graphics g = getGraphics();
                   Square s = new Square();
                   g.setColor(Color.cyan);
                   s.drawSquare(g);
			}
		});

        jp.add(jl);
		this.add(jp);
		jp.setBorder(BorderFactory.createTitledBorder("我来绘图啦"));
		setTitle("Paint Panel");
		setBounds(60,60,400,400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
		
	
	public static void main(String[] args) {
		Scanner val = new Scanner(System.in);
		new TestSquare();
		while (true) {
			int y = val.nextInt();
			if (y == 1) {
				RightTriangle s1 = new RightTriangle(val.nextInt());
				s1.Area();
			}
			if (y == 2) {
				Cycle s2 = new Cycle(val.nextInt());
				s2.Area();

			}
			if (y == 3) {
				Square s3 = new Square(val.nextInt());
				s3.Area();
			}
		}
	}
}





阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页