0

I want to draw a graphics pattern as below image.enter image description here

I do so here. enter image description here

My problem is how to draw these triangles that connected with square.

My java code is below.

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;


    public class Shape extends Applet {

        private static final long serialVersionUID = 1L;

        public void paint(Graphics g){

           Graphics2D g2 = (Graphics2D)g;  // turn on antialiasing
           g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                                        RenderingHints.VALUE_ANTIALIAS_ON);  

           g.setColor(Color.YELLOW);

           int xPoly[] = {60, 140, 100};
           int yPoly[] = {60, 60, 30};

           g.fillRect(20,20,160,160);
           g.setColor(Color.PINK);       // draw the shapes
           g.fillOval(20,20,160,160);
           g.setColor(Color.CYAN);
           g.fillRect(60,60,80,80);
           g.setColor(Color.BLACK);
           g.drawPolygon(xPoly, yPoly, xPoly.length);

           g.setColor(Color.RED);
           g.fillOval(60,60,80,80);
        } 
    }

Please help me to draw these triangles. I'll appreciate your help.

Parth Solanki
  • 3,268
  • 2
  • 22
  • 41
  • http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html#fillPolygon-java.awt.Polygon- – fabian Oct 24 '15 at 19:27
  • Well, if my eyes don't deceive me, the triangles are equilateral, and each side is the size of the square's sides. From there it's a question of applying geometry. – RealSkeptic Oct 24 '15 at 19:28
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Oct 26 '15 at 16:55

1 Answers1

3

you must use the g.drawPolygon(int[] xPoints, int[] yPoints, int nPoints) method to make triangles with swing.

    int xPoly[] = {60, 140, 100};
    int yPoly[] = {60, 60, 30};

    poly = new Polygon(xPoly, yPoly, xPoly.length);

    g.drawPolygon(poly);

You will need to load xPoly/yPoly with your coords for each triangle and create a Polygon out of each of them(or call the method directly with your data) for each triangle. Just line up the coords from your square with the polygon and you should be set.

Ryan
  • 1,972
  • 2
  • 23
  • 36
  • Hi Ryan , thanks for your answer , I am beginner in computer graphics and I dont know much about this , can you give me a fix code that I put over there and get same output that I want.. – Parth Solanki Oct 24 '15 at 19:39
  • that should give you the top triangle i can't do all of them but just use the coords you entered for your tiny square. each triangle will use two of those coords and then one thats a average of the x or y depending on what side its on plus 30 to make it a triangle. – Ryan Oct 24 '15 at 19:49
  • Hi Ryan , I put this code and I get one triangle, let me know what i do for all those 4. – Parth Solanki Oct 24 '15 at 20:02
  • Repeat the code 3 more times with different xpoly and ypoly values for your other 3 triangles – Ryan Oct 24 '15 at 20:02
  • 60,60 is your top left square coord 140,140 is your bottom right. 0,0 is the top left of the screen. Take this into concideration when setting your points – Ryan Oct 24 '15 at 20:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93261/discussion-between-parth-solanki-and-ryan). – Parth Solanki Oct 24 '15 at 20:37
  • I cant help you through every triangle I hope this answer is sufficent. Ths swing component uses a coordinate system where 0,0 is the top left and the dimensions of your jframe is the bottom right. All of your shapes are drawn with coordinate points. Your squares top left corner is 60,60 and bottom right is 140,140. This is x and y. If you want a triangle on the left of the cube your two x values would be 60 your y values would be 60 and 140 then your third point would be the average of the ys which is 100 and the x would be 60-30 so 30. Just think through the system logically or draw a pic. – Ryan Oct 24 '15 at 22:54
  • Hi Ryan, Thanks for your help , finally its done..Really appreciate your help buddy.. :) – Parth Solanki Oct 26 '15 at 12:05