「java写矩形」java编写一个矩形类

博主:adminadmin 2022-12-04 00:36:07 65

今天给各位分享java写矩形的知识,其中也会对java编写一个矩形类进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java编写一个矩形类,至少包含以下方法:

import java.awt.Point;

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;

}

}

麻烦采纳呦~~~亲

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 编写一个矩形类 rect 要求如下:

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;

    }

    

    //不知道你要什么成员方法,我随便点....

    

    

    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;

}

}

java画矩形

直接说event是简单,不过总要试一试才敢拿上来讲,所以就全写上来了。。。

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Point;

import java.awt.Rectangle;

import java.awt.event.MouseEvent;

import java.util.ArrayList;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.event.MouseInputAdapter;

import javax.swing.event.MouseInputListener;

public class Test {

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(new PaintingPanel());

frame.setBounds(100, 100, 400, 400);

frame.setVisible(true);

}

}

class PaintingPanel extends JPanel {

ArrayListRectangle list;

Rectangle current;

public PaintingPanel() {

list = new ArrayListRectangle();

addMouseMotionListener(mouseHandler);

addMouseListener(mouseHandler);

}

MouseInputListener mouseHandler = new MouseInputAdapter() {

Point startPoint;

public void mousePressed(MouseEvent e) {

startPoint = e.getPoint();

current = new Rectangle();

}

public void mouseReleased(MouseEvent e) {

makeRectangle(startPoint, e.getPoint());

if (current.width 0 current.height 0) {

list.add(current);

current = null;

repaint();

}

}

public void mouseDragged(MouseEvent e) {

if (current != null) {

makeRectangle(startPoint, e.getPoint());

repaint();

}

}

private void makeRectangle(Point p1, Point p2) {

int x = Math.min(p1.x, p2.x);

int y = Math.min(p1.y, p2.y);

int w = Math.abs(p1.x - p2.x);

int h = Math.abs(p1.y - p2.y);

current.setBounds(x, y, w, h);

}

};

public void paint(Graphics g) {

super.paint(g);

g.setColor(Color.BLACK);

for (Rectangle rect : list) {

g.fillRect(rect.x, rect.y, rect.width, rect.height);

}

if (current != null) {

g.drawRect(current.x, current.y, current.width, current.height);

}

}

}

编写一个JAVA程序,描写一个矩形类,并输出某个矩形的长,宽,面积。具体描述如下?

// 矩形

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程序矩形类两数据员别rLength宽rWidth通getLength()、getWidth()、getArea()别查看矩形、宽面积通setLength()setWidth()重新设置矩形宽

JAVA,写一个名为Rectangle的类表示矩形

这个我做完了 希望您能满意

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;

}

/**

* 测试函数

* @param args

*/

public static void main(String[] args) {

Rectangle rec=new Rectangle(3, 4, "红色");

rec.getArea();

System.out.println(rec.toString());

}

}

java写矩形的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java编写一个矩形类、java写矩形的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-04,除非注明,否则均为首码项目网原创文章,转载请注明出处。