import?java.awt.Point;
創(chuàng)新互聯(lián)建站專注于保定企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城網(wǎng)站制作。保定網(wǎng)站建設(shè)公司,為保定等地區(qū)提供建站服務(wù)。全流程定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
public?class?Rectangle?{
private?int?widthT?=?0;
private?int?heightT?=?0;
Point?point?=?new?Point();
public?Rectangle(int?width,?int?height,?int?centerX,?int?centerY)?{
widthT?=?width;
heightT?=?height;
point.x?=?centerX;
point.y?=?centerY;
}
public?int?width()?{
return?widthT;
}
public?int?height()?{
return?heightT;
}
public?int?centerX()?{
return?point.x;
}
public?int?centerY()?{
return?point.y;
}
}
麻煩采納呦~~~親
// 矩形
public class RectangleDemo {
public static void main(String[] args) {
RectangleDemo demo = new RectangleDemo(12, 32);
System.out.println(demo.getPerimeter());
System.out.println(demo.getArea());
demo = new RectangleDemo();
System.out.println(demo.getArea());
System.out.println(demo.getPerimeter());
demo.setHeight(50);
demo.setWidth(30);
System.out.println(demo.getArea());
System.out.println(demo.getPerimeter());
}
// 求周
public double getPerimeter() {
return (height + width) * 2;
}
// 求面積
public double getArea() {
return height * width;
}
public RectangleDemo(double height, double width) {
this.height = height;
this.width = width;
}
public RectangleDemo() {
this.height = 10;
this.width = 10;
}
private double height;// 高度
private double width;// 寬度
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
編寫矩形類RectangleJava程序矩形類兩數(shù)據(jù)員別rLength寬rWidth通getLength()、getWidth()、getArea()別查看矩形、寬面積通setLength()setWidth()重新設(shè)置矩形寬
兄弟幫你寫了一個(gè):
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);
}
}
public?class?Rect{
private?int?length;
private?int?width;
private?int?startX;
private?int?startY
public?Rect(){}
public?Rect(int?length,int?width){
this.length?=?length;
this.width?=?width;
}
public?Rect(int?length,int?width,int?startX,int?startY){
this.length?=?length;
this.width?=?width;
this.startX?=?startX;
this.startY?=?startY;
}
//不知道你要什么成員方法,我隨便點(diǎn)....
public?void?louzhuhao(){
System.out.println("樓主好....");
}
public?int?getLength()?{
return?length;
}
public?void?setLength(int?length)?{????
this.length?=?length;
}
public?int?getWidth()?{
return?width;
}
public?void?setWidth(int?width)?{
this.width?=?width;
}
public?int?getStartX()?{
return?startX;
}
public?void?setStartX(int?startX)?{
this.startX?=?startX;
}
public?int?getStartY()?{
return?startY;
}
public?void?setStartY(int?startY)?{
this.startY?=?startY;
}
}
public class Rectangle{
private int width;
private int height;
public Rectangle(){
this.width = 10;
this.height = 10;
}
public Rectangle(int width, int height){
this.width = width;
this.height = height;
}
public int area(){
return width * height;
}
//省略getter/setter
}
這個(gè)我做完了 希望您能滿意
public class Rectangle {
private double height;
private double width;
private String color;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Rectangle(double width,double height,String color){
this.setColor(color);
this.setHeight(height);
this.setWidth(width);
}
public void getArea(){
double area=0;
area=this.height*this.width;
System.out.println("矩形的面積為"+area);
}
public String toString(){
String recStr="矩形的高度:"+this.getHeight()+"寬度:"+this.getWidth()
+"顏色:"+this.getColor();
return recStr;
}
/**
* 測試函數(shù)
* @param args
*/
public static void main(String[] args) {
Rectangle rec=new Rectangle(3, 4, "紅色");
rec.getArea();
System.out.println(rec.toString());
}
}
本文題目:java設(shè)置一個(gè)矩形代碼 java畫一個(gè)矩形
文章來源:http://sd-ha.com/article46/hieehg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、標(biāo)簽優(yōu)化、定制開發(fā)、做網(wǎng)站、App開發(fā)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)