Java draw triangle draw method?

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









Related Tutorials/Questions & Answers:
Java draw triangle draw method?
Draw a Triangle using a Line2D
Advertisements
J2ME Draw Triangle
Lottery Draw - Java Beginners
Draw a diamond - Java Beginners
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
how to draw network with nodes - Java Beginners
J2ME Draw Triangle, Rectangle, Arc, Line Round Rectangle Example
how to draw network(lines and nodes) in java - Applet
draw chart in web application
how to draw a table on jframe in java - Java Beginners
ModuleNotFoundError: No module named 'draw'
ModuleNotFoundError: No module named 'draw'
How can i draw a line using java swings
How can i draw a line using java swings
Draw Line in J2me
How to draw pie chart in J2ME
how to draw a network with nodes and lines connecting them - Java Beginners
Java Swing: Draw rectangle on mouse click
How to draw a television
draw this pattern of swastic.
draw this pattern of swastic.
How to draw globe, draw globe, globe
triangle
Dynamic Line draw in JSp
thanks deepak for guiding me to draw graphs using org.jfree - Java Beginners
ModuleNotFoundError: No module named 'draw-and-learn'
ModuleNotFoundError: No module named 'draw-compose'
ModuleNotFoundError: No module named 'libsbml-draw'
ModuleNotFoundError: No module named 'metaphor-draw'
ModuleNotFoundError: No module named 'plato-draw'
ModuleNotFoundError: No module named 'py-draw'
ModuleNotFoundError: No module named 'simple-draw'
ModuleNotFoundError: No module named 'draw-and-learn'
ModuleNotFoundError: No module named 'draw-compose'
ModuleNotFoundError: No module named 'draw-mempool'
ModuleNotFoundError: No module named 'draw-plum'
ModuleNotFoundError: No module named 'grid-draw'
How to draw a house, draw a house, a house
how to draw lines,circles, rectangles on JSP (using Java Graphics)
How to draw a wall, draw a wall, a wall
drawing rectangle in java/Creating a draw rectangle filled with black color function in Java for a grid
Canvas won't draw on composite (SWT)
How to draw to graph in Ms Excel from the data which is sent from an java application....?
Draw bufferimage in jpanel using netbeans
how can i draw a table using applet or swings - Java Beginners
draw a sine wave using jsp

Ads