Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This is the souce code I wrote which failed to implement the function I hoped:
import java.awt.*;

Java
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]Code block added - OriginalGriff[/edit]
Posted
Updated 6-Jul-14 2:15am
v2
Comments
OriginalGriff 6-Jul-14 8:16am    
"which failed to implement the function I hoped" tells us pretty much nothing.
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So tell us what it didn't do that you expected, or did do that you didn't!
Use the "Improve question" widget to edit your question and provide better information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900