shapejava
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中