Triangle Method

In this example a method to draw a triangle is defined. The method is constructed out of drawLine methods and named triangleDraw. It is called twice to draw two triangles.

import java.awt.*;
import java.applet.Applet;

public class Triangle extends Applet {

public void paint (Graphics g) {

triangleDraw (g, 200, 200, 100, 100);
triangleDraw (g, 100, 100, 50, 50);

private void triangleDraw(Graphics g, int baseX, int baseY, int sideLength, int height ) {

g.drawLine(baseX, baseY, baseX+sideLength, baseY);
g.drawLine(baseX+sideLength, baseY, baseY+height, baseX+sideLength/2);
g.drawLine(baseY+height, baseX+sideLength/2, baseX, baseY);



This is the output

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License