Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Polygon tutorial

timer

  • Please log in to reply
17 replies to this topic

#1 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 28 July 2009 - 01:12 PM

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.
Posted Image

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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

Create the object

Class
public class Pent extends JPanel implements ActionListener {

Add up the needed variables
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

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
public void start() {
		tim.start();
	}
	public void stop() {
		tim.stop();
	}

Movement of our pentagon
public void actionPerformed(ActionEvent E) {
		turn= turn+dv;
		if(turn>2*Math.PI)
			turn-= 2*Math.PI;
		repaint();
	}

Pentagon behavior
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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

Main class
public class PentDemo extends JFrame implements ActionListener {
Main variables
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...
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
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...
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
Posted Image
[ATTACH]1858[/ATTACH]

Attached Thumbnails

  • Pentagon.png

Edited by Turk4n, 29 July 2009 - 07:52 AM.

  • 8

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 29 July 2009 - 07:40 AM

+rep for nice code :)
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 29 July 2009 - 07:51 AM

+rep for nice code :)


Thank you :)
  • 0

#4 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 29 July 2009 - 09:30 AM

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.


  • 0

#5 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2765 posts

Posted 29 July 2009 - 09:42 AM

+rep!
love it!
  • 0
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"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#6 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 29 July 2009 - 10:07 AM

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 :D

+rep!
love it!


Thank you my friend :D
  • 0

#7 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

Posted 29 July 2009 - 11:57 AM

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

#8 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 29 July 2009 - 11:57 AM

Nice work! +rep!


Thanks :D
  • 0

#9 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 29 July 2009 - 12:46 PM

Awesome!!! +rep
  • 0

#10 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 29 July 2009 - 12:53 PM

Awesome!!! +rep


Glad you liked it :>
  • 0

#11 Egz0N

Egz0N

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1155 posts

Posted 29 July 2009 - 01:06 PM

cool .. i like it .. +rep :)
  • 0

#12 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 29 July 2009 - 01:12 PM

cool .. i like it .. +rep :)


Thanks Egz0N and long time no see :)
  • 0





Also tagged with one or more of these keywords: timer

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download