扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
class Rectangle{
创新互联专注于伊金霍洛网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供伊金霍洛营销型网站建设,伊金霍洛网站制作、伊金霍洛网页设计、伊金霍洛网站官网定制、小程序定制开发服务,打造伊金霍洛网络公司原创品牌,更为您提供伊金霍洛网站排名全网营销落地服务。
private int width = 2;
private int length = 1;
public int getWidth(){
return this.width;
}
public void setWidth(int w){
this.width = w;
}
public int getLength(){
return this.length;
}
public void setLength(int l){
this.length = l;
}
public int getArea(){
return this.length * this.width;
}
public int getCircumference(){
return (this.length + this.width) * 2;
}
public Rectangle(){}
public Rectangle(int l, int w){
this.length = l;
this.width = w;
}
}
public class demo{
public static void main(String[] args) {
Rectangle rect = new Rectangle(30, 8);
System.out.print("长方形的面积是:");
System.out.println(rect.getArea());
System.out.printf("长方形的周长是:%d\n", rect.getCircumference());
}
}
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class LX3_3 extends Applet {
public void paint(Graphics g) {
g.setColor(Color.red);//设置红颜色
g.drawOval(35,35,100,60);//画椭圆(圆心、宽和高)
g.fillOval(200,15,60,100);//画具有填充色的圆
g.setColor(Color.blue);//设置蓝颜色
g.drawRect(20,130,80,80);//画矩形
g.fillRect(120,130,80,80);//画具有填充色的矩形
g.drawRoundRect(220,130,80,80,20,20);//画圆角矩形
g.fillRoundRect(320,130,80,80,20,20);//画具有填充色的 圆角矩形
}
}
该程序是在Myeclipse的环境下运行的
在二维平面内,画长方形等都只需要改变点的坐标即可实现平移,旋转,缩放
上面还加入了颜色,可供参考
水平有限,但希望对你有帮助
兄弟帮你写了一个:
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Print {
public static void main(String[] args) {
new Te();
}
}
class Te extends Frame implements ActionListener {
Color cc = Color.red;
int x = -20, y = -50;
Random r = new Random();
public Te() {
this.setLayout(null);
Button b = new Button("画圆");
this.add(b);
b.setBounds(30,30,50,50);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter () {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBounds(200,200,500,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
this.cc = Color.red;
this.x = r.nextInt(400);
do {
int x1 = r.nextInt(300);
this.y = x1;
} while (this.y 50);
this.repaint();
}
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(cc);
g.drawRect(x,y,50,50);
g.setColor(c);
}
}
import java.util.*;
public class Rectangle {
private float length; //定义长变量
private float width; // 宽变量
public Rectangle(float length,float width){
this.length=length;
this.width=width;
}
public float getGirth(){
return (length+width)*2;
} //求周长方法
public float getArea(){
return length*width;
} //求面积方法
public static void main (String[] args) {
Scanner in=new Scanner(System.in);//调用输入方法
System.out.println ("请输入矩形的长:");
float a=in.nextFloat();
System.out.println ("请输入矩形的宽:");
float b=in.nextFloat();
System.out.println ("矩形周长为:"+new Rectangle(a,b).getGirth());
System.out.println ("矩形面积为:"+new Rectangle(a,b).getArea());
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流