Go Back   CodeCall Programming Forum > Software Development > Tutorials > Java Tutorials
Register Blogs Search Today's Posts Mark Forums Read

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-28-2009, 05:12 PM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
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
__________________

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

Last edited by Turk4n; 07-29-2009 at 11:52 AM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-29-2009, 11:40 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: Polygon tutorial

+rep for nice code
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-29-2009, 11:51 AM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Re: Polygon tutorial

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

Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-29-2009, 01:30 PM
BlaineSch's Avatar
Code Warrior
 
Join Date: Apr 2009
Location: Trapped in my own little world.
Age: 19
Posts: 2,169
BlaineSch is a glorious beacon of lightBlaineSch is a glorious beacon of lightBlaineSch is a glorious beacon of lightBlaineSch is a glorious beacon of lightBlaineSch is a glorious beacon of lightBlaineSch is a glorious beacon of light
Send a message via MSN to BlaineSch
Re: Polygon tutorial

How gewey!

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


+Rep!
Edit:
Quote:
You must spread some Reputation around before giving it to Turk4n again.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-29-2009, 01:42 PM
amrosama's Avatar
Code Warrior
/////////|||||\\\\\\\\\
 
Join Date: Aug 2007
Location: Pyramids st, Giza, Egypt
Age: 21
Posts: 8,112
amrosama is a splendid one to beholdamrosama is a splendid one to beholdamrosama is a splendid one to beholdamrosama is a splendid one to beholdamrosama is a splendid one to beholdamrosama is a splendid one to beholdamrosama is a splendid one to behold
Send a message via MSN to amrosama
Re: Polygon tutorial

+rep!
love it!
__________________
im a code-warrior, see my avatar
who said arabs cant dance?
and who said arabs cant drive??!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-29-2009, 02:07 PM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
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 ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-29-2009, 03:57 PM
marwex89's Avatar
Code Warrior
 
Join Date: Jul 2008
Location: Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
Posts: 9,849
marwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of lightmarwex89 is a glorious beacon of light
Send a message via AIM to marwex89 Send a message via MSN to marwex89
Re: Polygon tutorial

Nice work! +rep!
__________________

Computers make very fast, very accurate mistakes.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-29-2009, 03:57 PM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Re: Polygon tutorial

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

Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-29-2009, 04:46 PM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: Polygon tutorial

Awesome!!! +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-29-2009, 04:53 PM
Turk4n's Avatar
Code Warrior
 
Join Date: May 2008
Location: 4chan.org/g/
Age: 20
Posts: 3,822
Turk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud ofTurk4n has much to be proud of
Send a message via MSN to Turk4n Send a message via Skype™ to Turk4n
Re: Polygon tutorial

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

Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial - ListBox, ComboBox & Command button. travy92 VB Tutorials 15 01-22-2010 10:13 AM
CodeCall Tutorial Contest #4 Jordan Announcements 29 02-25-2008 12:25 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 04:05 PM


All times are GMT -5. The time now is 09:28 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0