Java draw triangle draw method?

Ads
 

Java draw triangle draw method?

hi

how would i construct the draw method for an triangle using the 'public void draw (graphics g ) method? im unsure how to use the g.fillPolygon and g.drawPolygon?

i have this for drawing a rectangle and this works for a rectangle:

public void draw(Graphics g) {
    g.setColor(colorFill);
    g.fillRect(p.x, p.y, width, height);
    g.setColor(colorBorder);
    g.drawRect(p.x, p.y, width, height);
    drawHandles(g);
}
View Answers

August 13, 2012 at 6:03 PM

Here is an example that draws a triangle using drawPolygon method.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class DrawTriangle extends JPanel {
  public void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.setColor(Color.red);
 Polygon polygon1= new Polygon();
 for (int i = 0; i < 3; i++){
  polygon1.addPoint((int) (40 + 50 * Math.cos(i * 2 * Math.PI / 3)),
  (int) (150 + 50 * Math.sin(i * 2 * Math.PI / 3)));
 }
 g.drawPolygon(polygon1);
 }

  public static void main(String[] args) {
  JFrame frame = new JFrame();
   frame.setSize(350, 250);
  Container contentPane = frame.getContentPane();
  contentPane.add(new DrawTriangle());
  frame.show();
  }
}

If you want to use Line2D class to draw a triangle, then please go through the following link:

http://www.roseindia.net/java/example/java/swing/graphics2D/triangle-line2d.shtml

Ads









Related Tutorials/Questions & Answers:
Java draw triangle draw method?
J2ME Draw Triangle
Advertisements
Write a Java applet to draw cylinder and pentagon shapes.
Draw a line in a JPanel with button click in Java
How to draw in jPanel? swing/graphics Java
Draw Line
J2ME Draw Triangle, Rectangle, Arc, Line Round Rectangle Example
draw chart in web application
ModuleNotFoundError: No module named 'draw'
ModuleNotFoundError: No module named 'draw'
drawing rectangle in java/Creating a draw rectangle filled with black color function in Java for a grid
How to draw pie chart in J2ME
Java Swing: Draw rectangle on mouse click
How to draw to graph in Ms Excel from the data which is sent from an java application....?
How to draw a television
draw this pattern of swastic.
draw this pattern of swastic.
How to draw globe, draw globe, globe
ModuleNotFoundError: No module named 'draw-and-learn'
ModuleNotFoundError: No module named 'draw-compose'
ModuleNotFoundError: No module named 'draw-and-learn'
ModuleNotFoundError: No module named 'draw-mempool'
ModuleNotFoundError: No module named 'grid-draw'
ModuleNotFoundError: No module named 'libsbml-draw'
ModuleNotFoundError: No module named 'metaphor-draw'
ModuleNotFoundError: No module named 'py-draw'
ModuleNotFoundError: No module named 'draw-compose'
ModuleNotFoundError: No module named 'draw-plum'
ModuleNotFoundError: No module named 'plato-draw'
ModuleNotFoundError: No module named 'simple-draw'
How to draw a house, draw a house, a house

Ads