Java编写一个矩形类,并计算面积和周长?
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());
}
}
如何用java写矩形平移和旋转后输出坐标的代码
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的环境下运行的
在二维平面内,画长方形等都只需要改变点的坐标即可实现平移,旋转,缩放
上面还加入了颜色,可供参考
水平有限,但希望对你有帮助
java中做一个按钮,点击按钮后画一个矩形的代码怎么写?
兄弟帮你写了一个:
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);
}
}
Java编程求矩形的面积
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());
}
}
本文题目:Java运行矩形代码 java编程实现矩形类
分享路径:http://scgulin.cn/article/dogdodh.html