当前位置:首页 » 编程语言 » shapejava

shapejava

发布时间: 2022-11-29 03:42:56

A. java中shape这个类是怎么用的

注意:我已经将一个画图的程序代码发到你的QQ邮箱了,你可以参考下它的方法.

shape 在java.awt包中是一个接口,
所有已知实现类:
Arc2D, Arc2D.Double, Arc2D.Float, Area, BasicTextUI.BasicCaret, CubicCurve2D, CubicCurve2D.Double, CubicCurve2D.Float, DefaultCaret, Ellipse2D, Ellipse2D.Double, Ellipse2D.Float, GeneralPath, Line2D, Line2D.Double, Line2D.Float, Polygon, QuadCurve2D, QuadCurve2D.Double, QuadCurve2D.Float, Rectangle, Rectangle2D, Rectangle2D.Double, Rectangle2D.Float, RectangularShape, RoundRectangle2D, RoundRectangle2D.Double, RoundRectangle2D.Float

一个接口是不能实例化的。你是不是想保存上面这些已实现了的类对象?

保存一个图,有多种方法。在此我可以给你一种思路:你可以获得每一个对象图的点,边长,高等属性,然后将这些信息保存起来,在下次打开的时候,重新将这些图画一次,就能显示了原图了。

哈哈,这只是一种方法的。你可以想想还有没有别的方法。在Shape是没有方法提供保存功能的。你可以自己想个办法如何实现保存。或是请教你的老师。

祝你成功!

B. 用Java定义一个形状类Shape

publicabstractclass Shape {
publicabstractvoid area();
}

class Circle extends Shape {
privatedoubleradius;
privatedoubleS;

Circle(double radius) {
this.radius = radius;
}

publicvoid area() {
S = 3.14 * radius * radius;
System.out.println(S);
}

}

class Rect extends Shape {
privatedoublelength;
privatedoublewidth;
privatedoubleS;

Rect(double length,double width) {
this.length = length;
this.width = width;
}

publicvoid area() {
S = length * width;
System.out.println(S);
}
}

class Test {
publicstaticvoid main(String[] args) {
Circle a = new Circle(3);
a.area();
Rect b = new Rect(3,4);
b.area();
}
}

C. JAVA 设计一个Shape接口和它的一个实现类Triangle(三角形),要求如下:

publicinterfaceShape{
doublearea(doublel,doubleh);
}
importjava.math.BigDecimal;

{
@Override
publicdoublearea(doublel,doubleh){
//使用BigDecimal计算防止精度出错
BigDecimallen=newBigDecimal(l);
BigDecimalhigh=newBigDecimal(h);
returnlen.multiply(high).divide(newBigDecimal(2),5,BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
publicclassTest{
publicstaticvoidmain(String[]args){
Triangletriangle=newTriangle();
doublearea=triangle.area(3.5,2.7);
System.out.println("面积为:"+area);
}
}

D. JAVA程序编写。 定义一个抽象类shape ,用来表示一般图形。

Java程序:

publicclassMain{	
publicstaticvoidmain(String[]args){
Shapes=null;
s=newCircle(3);

System.out.println("圆的面积:"+s.area());
System.out.println("圆的周长:"+s.perimeter());
}
}

/**
*形状类:抽象类
*@authordeveloper
*@version2017.05.23
*/
abstractclassShape{
/**
*计算形状的面积
*@return形状的面积
*/
abstractdoublearea();

/**
*计算形状的周长
*@return形状的周长
*/
abstractdoubleperimeter();
}

/**
*圆类
*@authordeveloper
*@version2017.05.23
*/
classCircleextendsShape{
/**
*半径
*/
protecteddoubleradius;

/**
*构造方法
*@paramradius半径
*/
publicCircle(doubleradius){
this.radius=radius;
}

@Override
doublearea(){
returnMath.PI*radius*radius;
}

@Override
doubleperimeter(){
return2*Math.PI*radius;
}
}


运行测试:

圆的面积:28.274333882308138
圆的周长:18.84955592153876

E. java设计图形(Shape)类及其子类(Circle、Rectangle)

你好,刚好闲着帮你写一个:
Shape类:
public class Shape {

protected Point location;
public Shape(){

}
public double area(){
return 0.0;
}

}
Circle类:
public class Circle extends Shape{
private int r;
public Circle() {

}
public Circle(Point center,int r) {
super.location=center;
this.r = r;
}
public double area() {

return Math.PI*r*r ;
}
}
Rectangle类:
public class Rectangle extends Shape{
private int width;
private int height;
public Rectangle() {

}
public Rectangle(Point o,int width, int height) {
location=o;

this.width = width;
this.height = height;
}
public double area() {

return width*height;
}

}
我这里图方便,在创建圆的时候直接用圆心和半径创建,还有矩形也是用一个点位置和长宽创建,所以还要加一个点类:
public class Point {

public int x;
public int y;

public Point() {
}

public Point(int x, int y) {
this.x = x;
this.y = y;
}
}

F. java编写图形抽象类(Shape)

我来写一下吧:
abstract class Shape{
private double c;
private double s;
public abstract void girth();
public abstract void area();
public void setGirth(double c){
this.c = c;
}
public void setArea(double s){
this.s = s;
}
public double getGirth(){
return c;
}
public double getArea(){
return s;
}
public void outInfo(){}
}
class Circle extends Shape{
private static final double PI = 3.1415926;
private double r;
//定义一个构造函数
public Circle(double r){
this.r = r;
}
//重写抽象方法
public void girth() {
double a =2*PI*r;
super.setGirth(a);
}
public void area() {
double b =PI*r*r;
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求圆形周长为:"+super.getGirth());
System.out.println("所求圆形面积为:"+super.getArea());
}
}
class Rectangle extends Shape{
private double height;
private double width;
//定义一个构造函数
public Rectangle(double height,double width){
this.height = height;
this.width = width;
}
//重写抽象方法
public void girth() {
double a =2*(height+width);
super.setGirth(a);
}
public void area() {
double b =(height*width);
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求矩形周长为:"+super.getGirth());
System.out.println("所求矩形面积为:"+super.getArea());
}
}
class Triangle extends Shape{
private double lengthA;
private double lengthB;
private double lengthC;
//定义一个构造函数
public Triangle(double lengthA,double lengthB,double lengthC){
this.lengthA = lengthA;
this.lengthB = lengthB;
this.lengthC = lengthC;
}
//重写抽象方法
public void girth() {
double a =(lengthA+lengthB+lengthC);
super.setGirth(a);
}
public void area() {
if((lengthA+lengthB < lengthC) || (lengthA + lengthC < lengthB) || (lengthB+lengthC < lengthA)) {
System.out.println("两边之和必须大于第三个边");
System.exit(0);
}
double p = (lengthA+lengthB+lengthC)/2;
double b = Math.sqrt(p*(p-lengthA)*(p-lengthB)*(p-lengthC));
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求三角形周长为:"+super.getGirth());
System.out.println("所求三角形面积为:"+super.getArea());
}
}
public class ShapeTest {
public static void main (String [] args){
Shape circle = new Circle(3.0);
Shape rectangle = new Rectangle(8.0,7.0);
Shape triangle = new Triangle(3.0,4.0,5.0);

circle.outInfo();
rectangle.outInfo();
triangle.outInfo();
}
}

G. java如何编程定义一个shape抽象类,具有求面积、求周长、显示输出功能

楼主是不是想利用Java求shape文件中 面的面积,也就是polygon或者multipolygon的面积。实际上就是不规则多边形的面积,如果不用什么函数库(geotools)的话,还是有现成的公式的,非是通过定积分推倒了一个公式而已。
需要注意的是:
点要按照逆时针或者顺时针的顺序添加进list
package geodemo;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import org.opengis.feature.simple.SimpleFeature;
import com.vividsolutions.jts.geom.Geometry;
public class GetArea{
public static void main(String args[]){
Point p1 = new Point(1,0);
Point p2 = new Point(12,0);
Point p3 = new Point(10,10);
Point p4 = new Point(0,10);
Point p5= new Point(3,3);
List<Point> list = new ArrayList<Point>();//泛型
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
GetArea t = new GetArea();
double area = t.getArea(list);
System.out.println(area);
}
public double getArea(List<Point> list)
{
//S = 0.5 * ( (x0*y1-x1*y0) + (x1*y2-x2*y1) + ... + (xn*y0-x0*yn) )
double area = 0.00;
for(int i = 0;i<list.size();i++){
if(i<list.size()-1){
Point p1 = list.get(i);//得到p1坐标对(x,y)
Point p2 = list.get(i+1); //得到p2坐标对(x,y)
area += p1.getX()*p2.getY() - p2.getX()*p1.getY();
}else{
Point pn = list.get(i);
Point p0 = list.get(0);
area += pn.getX()*p0.getY()- p0.getX()*pn.getY();
}
}
area = area/2.00;
return area;
}
}
原理如下:shapefile文件面文件也是由一个个坐标点构成的,无论是不规则凸多边形还凹多边形,都可以分成多个三角形,然后就是按一定顺序求解三角形面积了。我把网上的一个例子修改了下,加上了泛型(广泛的类型,类似int,double),点是五个,你可以在此基础上修改,读取shp文件,把坐标提取出来,然后再计算。
至于求周长之类的就是把公式变成代码的过程,本人强烈建议还是用arcgis求面积比较好,

H. JAVA编写一个名为Shape的接口类型

public class Circle implements Shape{

public static void main(String[] args) {
Circle circle = new Circle();
float r = 5; //半径
System.out.println("圆面积:" + circle.getArea(r));
System.out.println("圆周长:" + circle.getCircumference(r));
}

@Override
public float getArea(float a) {
float pi = (float) Math.PI;
return pi * a * a;
}

@Override
public float getCircumference(float a) {
float pi = (float) Math.PI;
return 2 * pi * a;
}

}

public interface Shape {
float getArea(float a);
float getCircumference (float a);
}

I. Shapes.java:2: 类 Shape 是公共的,应在名为 Shape.java 的文件中声明

一个文件中只能有一个public的类,解决方法是把它们分开放在对应的文件中如Circle 放在Circle.java中Shape放在Shape.java中

热点内容
脚本设计图 发布:2025-01-18 18:06:17 浏览:600
内部存储空间不足总是跳出来 发布:2025-01-18 17:56:22 浏览:950
安卓光遇更新后魔法商店去哪里了 发布:2025-01-18 17:55:47 浏览:132
安卓手机怎么变成苹果设备 发布:2025-01-18 17:46:06 浏览:915
linux服务端 发布:2025-01-18 17:36:55 浏览:250
表白代码的编译器是什么 发布:2025-01-18 17:36:53 浏览:999
录像机日志服务器地址怎么填 发布:2025-01-18 17:28:17 浏览:110
安卓手机如何调整图标样式 发布:2025-01-18 17:23:53 浏览:549
计算机配置单怎么算 发布:2025-01-18 17:23:52 浏览:81
超级终端如何清空配置 发布:2025-01-18 17:18:44 浏览:598