+rep for nice code![]()
Thread: Polygon 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
Create the objectCode:import javax.swing.*; import java.awt.*; import java.awt.event.*;
Class
Add up the needed variablesCode:public class Pent extends JPanel implements ActionListener {
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?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);
tim presents the time it will take to go around and around the world...
Build up functions
Pentagon
Our pentagon will take form soon, this is it's bones...Code:public Pent(int pieces, int radie) { n=pieces; r=radie; x = new int[n]; y = new int[n]; angle= 2*Math.PI/n; }
Start and Stop
Movement of our pentagonCode:public void start() { tim.start(); } public void stop() { tim.stop(); }
Pentagon behaviorCode:public void actionPerformed(ActionEvent E) { turn= turn+dv; if(turn>2*Math.PI) turn-= 2*Math.PI; repaint(); }
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.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); } }
Lets work on the main class now.
Main packages
Main classCode:import java.awt.*; import javax.swing.*; import java.awt.event.*;
Main variablesCode:public class PentDemo extends JFrame implements ActionListener {
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.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);
The main constructor and GUI build...
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).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"); }
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
As we using our reference from pent we can use methods inside from pent to our main, which is start and stop...Code:public void actionPerformed(ActionEvent e) { if(e.getSource() == On) { y.start(); } else y.stop(); }
The main application...
And that's about it hope you enjoy this vague tutorial and hopefully you will do something more creative than I did...Code:public static void main(String[] arg) { PentDemo pd = new PentDemo(); } }
Cheers !
Output
![]()
Last edited by Turk4n; 07-29-2009 at 07:52 AM.
Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
+rep for nice code![]()
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
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.

+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"));
Nice work! +rep!
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Awesome!!! +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)