java应用小程序画矩形和圆

java小程序矩形和椭圆

哈罗,大家好,我是B站UP主(文哥动漫),那么今天我做了一小实验,用小程序画出,一个矩形和一个椭圆,并放在一个小应用程序的视图里。
接下来请看代码:加详细讲解
首先,我们要定义变量,设置图形在这个画板的中心位置(x轴、y轴坐标),然后通过带参构造方法传值,this关键字在这里可以看做本类的类名(就近原则),下面在定义两个方法,第一个方法是椭圆的,第二个方法四矩形的,两个方法里面对应的朗格函数,请大家自行去百度,发挥主观能动性。fillRect,填充一个矩形。

public class Shape{
	int x, y, width, height;
	int lineSize;
	public Shape(int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
	}
	public void draw(Graphics g) {
		g.drawOval(x, y, width, height);
		g.drawRect(100, 50, 50, 100);
	}
	public void draw1(Graphics g) {
		g.drawRect(100, 50, 50, 100);
		g.fillRect(100, 50, 50, 100);
	}
}

下面,是测试类,继承Applet,创建画板的方法,创建构造函数对象,调用椭圆和矩形的方法。那么本题也就成功了

import java.applet.Applet;
import java.awt.Graphics;
public class Shape1 extends Applet{
	public void paint(Graphics g) {
		Shape shape = new Shape(100, 200, 200, 100);
		shape.draw(g);
		shape.draw1(g);
	}
	}

接下来,看看效果图:
java应用小程序画矩形和圆_第1张图片

你可能感兴趣的:(java,java,小程序)