
正文
java矩形代码 java定义矩形
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java 编写一个矩形类Rect
public class Rect {
private double length;//矩形java矩形代码的长
private double width;//矩形java矩形代码的宽
public Rect() {}//默认构造器
public Rect(double length,double width) {
this.length = length;
this.width = width;
}
/**
* 取得矩形java矩形代码的面积
* */
public double getArea(){
return this.getLength() * this.getWidth();
}
/**
* 取得矩形的周长
* */
public double getPerimeter(){
return (this.getLength() + this.getWidth()) * 2;
}
/**
* 取得矩形的面积java矩形代码,需传入矩形长与宽
* */
public double getArea(double length,double width){
return length * width;
}
/**
* 取得矩形的周长java矩形代码,需传入矩形长与宽
* */
public double getPerimeter(double length,double width){
return (length + width) * 2;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
相关问答
Q1: 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);
}
}
}
Q2: 画空心矩形的代码是什么?要java的。
以下是代码,可以参考一下,希望对您有帮助。
============================================
public class PrintStars {
private static final int LENGTH=10;
private static final int WIDTH=20;
public static void main(String[] args) {
for (int i = 0; i LENGTH; i++) {
if(i==0||i==LENGTH-1){
for (int j = 0; j WIDTH; j++) {
System.out.print("* ");
}
}else {
for (int j = 0; j WIDTH; j++) {
if(j==0||j==WIDTH-1){
System.out.print("* ");
}else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
Q3: JAVA代码 定一个矩形类,有两个属性,宽度和长度,有两个方法,一个计算面积,还有计算周长
public class Rectangle {
public static int height;
public static int weight;
public static void main(String[] args) {
int a = (height + weight) * 2;
int b = height * weight;
System.out.println("周长=" + a);
System.out.println("面积=" + b);
}
}
关于java矩形代码和java定义矩形的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







