请给我关于Triangle extends Polygon的建议,它实现了Drawable接口。 [英] Please give me suggestions about Triangle extends Polygon ,which implements interface Drawable.

查看:78
本文介绍了请给我关于Triangle extends Polygon的建议,它实现了Drawable接口。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我写的源代码无法实现我希望的功能:

import java.awt。*;



This is the souce code I wrote which failed to implement the function I hoped:
import java.awt.*;

public class Triangle extends Polygon implements Drawable {

	Color triangleColor;
	Polygon triangleShape;
	int currentX;
	int currentY;
	
	
	public Triangle() {
		Triangle t = makeDefaultTriangle();
		setColor(t.getColor());
		setShape(t.getShape());
		setPosition(t.getX(),t.getY());
	}
	
	
	public Triangle(Color c,Polygon p,int x,int y) {
		setColor(c);
		setShape(p);
		setPosition(x,y);
	}
	
	
		public void setColor(Color c) {
			triangleColor = c;
		}
		
		
		public Color getColor() {
			return triangleColor;
		}
		
		
		public void setShape(Polygon p) {
			triangleShape = new Polygon(p.xpoints,p.ypoints,p.npoints);
		}
		
		
		public Polygon getShape() {
			return triangleShape;
		}
		
		
		public void setX(int x) {
			currentX = x;
		}
		
		
		public int getX() {
			return currentX;
		}
		
		
		public void setY(int y) {
			currentY = y;
		}
		
		
		public int getY() {
			return currentY;
		}
		
		
		public void setPosition(int x,int y) {
			setX(x);
			setY(y);
		}
		
		
		public void paint(Graphics g) {
			Color c = getColor();
			Polygon p = getShape();
			int[] x = (int[]) p.xpoints.clone();
			int[] y = (int[]) p.ypoints.clone();
			int n = p.npoints;
			
			for(int i = 0;i < n; i++) {
				x[i] += getX();
				y[i] += getY();
			}
			
			p = new Polygon(x,y,n);
			
			g.setColor(c);
			g.fillPolygon(p);
		}
		
		public static Triangle makeDefaultTriangle() {
			int[] x = {0,3,0};
			int[] y = {0,0,4};
			Color c = Color.WHITE;
			
			Polygon p = new Polygon(x,y,x.length);
			return new Triangle(c,p,0,0);
		}
}





[edit]已添加代码块 - OriginalGriff [/ edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

这篇关于请给我关于Triangle extends Polygon的建议,它实现了Drawable接口。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆