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

Thread: Polygon tutorial

  1. #1
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    21
    Posts
    3,843
    Blog Entries
    4

    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 07:52 AM.

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    13,155
    Blog Entries
    59

    Re: Polygon tutorial

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

  3. #3
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    21
    Posts
    3,843
    Blog Entries
    4

    Re: Polygon tutorial

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  4. #4
    Code Warrior BlaineSch is a name known to all BlaineSch is a name known to all BlaineSch is a name known to all BlaineSch is a name known to all BlaineSch is a name known to all BlaineSch is a name known to all BlaineSch's Avatar
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Age
    20
    Posts
    2,289
    Blog Entries
    8

    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.

  5. #5
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids, Egypt
    Age
    21
    Posts
    8,635
    Blog Entries
    12

    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"));

  6. #6
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    21
    Posts
    3,843
    Blog Entries
    4

    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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  7. #7
    Code Warrior marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89's Avatar
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,741
    Blog Entries
    2

    Re: Polygon tutorial

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

  8. #8
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    21
    Posts
    3,843
    Blog Entries
    4

    Re: Polygon tutorial

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  9. #9
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    Re: Polygon tutorial

    Awesome!!! +rep

  10. #10
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    21
    Posts
    3,843
    Blog Entries
    4

    Re: Polygon tutorial

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Tutorial - ListBox, ComboBox & Command button.
    By travy92 in forum VB Tutorials
    Replies: 16
    Last Post: 06-15-2010, 09:01 AM
  2. CodeCall Tutorial Contest #4
    By Jordan in forum Announcements
    Replies: 29
    Last Post: 02-25-2008, 09:25 AM
  3. John's Java Tutorial Index
    By John in forum Java Tutorials
    Replies: 0
    Last Post: 01-11-2007, 01:05 PM