+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Polygon tutorial

  1. #1
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,850
    Blog Entries
    4
    Rep Power
    49

    Pentagon tutorial

    Hello and welcome to a CodeCall Java tutorial. Today I will talk about Pentagons in Java and how you can draw one also using it for fun with javas buildin GUI set; swing.
    Firstly we all no what a Pentagons is, however for those unfamiliar with what a pentagons is here is an example of a pentagon.


    So mainly a pentagon is a five edged figure. So how do I draw a pentagon with swing?
    Well let's get going and do the thing.

    Step #1
    Make the pentagon(Object !)

    Import the needed packages

    Packages
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    Create the object

    Class
    Code:
    public class Pent extends JPanel implements ActionListener {
    Add up the needed variables
    Code:
    private int n,r;
    	private double angle;
    	private int[] x,y;
    	private double dv = 5*2*Math.PI/360;
    	private double turn = 0.0;
    	private Timer tim = new Timer(100, this);
    What are our variables for? Firstly we need two integers which represents the sum of every part and time. We need angle to deiced the angle of our pentagon you could do as you suit with it later on. We need two arrays to represent the 2-D figure. Dv will represent our edges and turn how much it will turn since I want to show one possibility with swing why not show off?
    tim presents the time it will take to go around and around the world...


    Build up functions

    Pentagon

    Code:
    public Pent(int pieces, int radie) {
    		n=pieces; r=radie;
    		x = new int[n];
    		y = new int[n];
    		angle= 2*Math.PI/n;
    	}
    Our pentagon will take form soon, this is it's bones...

    Start and Stop
    Code:
    	public void start() {
    		tim.start();
    	}
    	public void stop() {
    		tim.stop();
    	}
    Movement of our pentagon
    Code:
    	public void actionPerformed(ActionEvent E) {
    		turn= turn+dv;
    		if(turn>2*Math.PI)
    			turn-= 2*Math.PI;
    		repaint();
    	}
    Pentagon behavior
    Code:
    	public void paintComponent(Graphics G) {
    		super.paintComponents(G);
    		int x0 = getSize().width/2;
    		int y0 = getSize().height/2;
    		
    		for(int i=0; i<n; i++) {
    			double v = i*angle- turn;
    		x[i] = x0 + (int)Math.round(r*Math.cos(v));
    		y[i] = y0 + (int)Math.round(r*Math.sin(v));
    		}
    		G.fillPolygon(x, y, n);
    	}
    }
    Here we are actually filling the figure in a component that will we later present up into a frame with a start and stop button. The math.round presents the rotation that will happen.
    Lets work on the main class now.

    Main packages
    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    Main class
    Code:
    public class PentDemo extends JFrame implements ActionListener {
    Main variables
    Code:
    private JButton On= new JButton("On");
    	private JButton Off = new JButton("Off");
            private JPanel a = new JPanel();
    	private Pent p = new Poly(5,50);
    We are going to use a Frame as our container for the Panel which will contain the pentagram allowing us to have it in a fine motion without disturbance, you could do it on the frame if you like to instead of my way. As we do not extend from the Pentagon class(pent) instead we are creating a reference to it and adding values to the constructor.

    The main constructor and GUI build...
    Code:
    	public PentDemo() {
    		add(y,BorderLayout.CENTER);
    		add(a,BorderLayout.SOUTH);
    		a.add(On); a.add(Off); 
    		On.addActionListener(this);
    		Off.addActionListener(this);
    		setSize(200,180);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setTitle("Pentagon");
    	}
    Simple frame build adding the components Panel and our Pent into the frame allowing us to use it for our own purpose I guess :X?(LOL).
    Note as I told before you can add everything directly to the Frame we are extending from, instead of using a seperate Panel.(the variable called 'a')


    Actions
    Code:
    	public void actionPerformed(ActionEvent e) {
    		if(e.getSource() == On) {
    			y.start();
    		}
    		else
    			y.stop();
    	}
    As we using our reference from pent we can use methods inside from pent to our main, which is start and stop...

    The main application...
    Code:
    	public static void main(String[] arg) {
    		PentDemo pd = new PentDemo();
    	}
    }
    And that's about it hope you enjoy this vague tutorial and hopefully you will do something more creative than I did...
    Cheers !

    Output

    Polygon tutorial-pentagon.png
    Last edited by Turk4n; 07-29-2009 at 08:52 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    Re: Polygon tutorial

    +rep for nice code
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,850
    Blog Entries
    4
    Rep Power
    49

    Re: Polygon tutorial

    Quote Originally Posted by WingedPanther View Post
    +rep for nice code
    Thank you

  5. #4
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Polygon tutorial

    How gewey!

    I need to try this, I have never done GUI with Java before.
    /me opens eclipse


    +Rep!
    Edit:
    You must spread some Reputation around before giving it to Turk4n again.

  6. #5
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Polygon tutorial

    +rep!
    love it!
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  7. #6
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,850
    Blog Entries
    4
    Rep Power
    49

    Re: Polygon tutorial

    Quote Originally Posted by BlaineSch View Post
    How gewey!

    I need to try this, I have never done GUI with Java before.
    /me opens eclipse


    +Rep!
    Edit:
    Lol, Good luck hope you will do something really cool
    Quote Originally Posted by amrosama View Post
    +rep!
    love it!
    Thank you my friend

  8. #7
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Polygon tutorial

    Nice work! +rep!
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  9. #8
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,850
    Blog Entries
    4
    Rep Power
    49

    Re: Polygon tutorial

    Quote Originally Posted by marwex89 View Post
    Nice work! +rep!
    Thanks

  10. #9
    Jordan Guest

    Re: Polygon tutorial

    Awesome!!! +rep

  11. #10
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,850
    Blog Entries
    4
    Rep Power
    49

    Re: Polygon tutorial

    Quote Originally Posted by Jordan View Post
    Awesome!!! +rep
    Glad you liked it :>

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Polygon slice?!
    By cesarone82 in forum General Programming
    Replies: 2
    Last Post: 10-03-2010, 08:08 AM
  2. Creating Polygon
    By coiner in forum Java Help
    Replies: 8
    Last Post: 06-15-2010, 11:34 PM
  3. C++ Suport Polygon Vector Problem
    By psnow85 in forum C and C++
    Replies: 6
    Last Post: 06-28-2009, 05:15 AM
  4. Area of a polygon...
    By smoore in forum Java Help
    Replies: 8
    Last Post: 03-01-2009, 09:28 AM
  5. Replies: 5
    Last Post: 04-17-2007, 08:34 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts