用java画矩形与正方形

package 绘制图形;
//画矩形
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;



public class rectangle extends JFrame{
    public static void main(String args[]) {
        JFrame rect = new JFrame("绘制矩形");
        JFrame square = new JFrame("绘制正方形");
        rect.add(new DrawRectanglePanel());
        rect.setVisible(true);
        rect.setDefaultCloseOperation(3);
        square.add(new DrawSquarePanel());
        square.setVisible(true);
        square.setDefaultCloseOperation(3);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int width = screenSize.width;
        int hight = screenSize.height;
        rect.setBounds(width/2, hight/2, 400, 300);
        square.setBounds(width/4, hight/4, 400, 300);
        
    }

}
class DrawRectanglePanel extends JPanel{
    public void paint(Graphics g) {
        //空心矩形的坐标及其长宽
        g.drawRect(30, 40, 80, 60);
        //实心矩形的坐标及其长宽
        g.fillRect(140, 40, 80, 60);
    }
}
class DrawSquarePanel extends JPanel{
    public void paint(Graphics g) {
        //空心正方形
        g.drawRect(20, 20, 100, 100);
        g.drawRect(40, 40, 60, 60);
        //实心加空心正方形
        g.drawRect(140, 20, 100, 100);
        g.fillRect(160, 40, 60, 60);
    }
}

画矩形与方形.png

我要持续学习,持续更新

你可能感兴趣的:(用java画矩形与正方形)