Reusing JFrame As windowUtilities

View previous topic View next topic Go down

Reusing JFrame As windowUtilities

Post by BIT0112-Rokon on Tue Aug 03, 2010 8:18 am

Some time we test different new things to learn. For say today I've tried to learn java2D graphics. I have written a lot of codes to learn this new thing. So every time I have to open a new class which extends JPanel and to make it visible I have to add it in a JFrame. So lots of time I have to write same code again and again. But In Object oriented design, there is approach that is, we don't write same code again and again that is called code reuse. In this text I'll show you the way of code reuse.

Let have a look my first code ..

Code:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class ShapeEample extends JPanel {
    private Ellipse2D.Double circle = new Ellipse2D.Double(10, 10, 350, 350);
    private Rectangle2D.Double square = new Rectangle2D.Double(10, 10, 350, 350);

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.fill(circle);
        g2d.draw(square);
    }
   
    public static void main(String[] args) {
        JFrame frm = new JFrame("Shape example");
        frm.setSize(400, 400);
        frm.add(new ShapeEample());
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }
}

Let see in the main method.


public static void main(String[] args) {
JFrame frm = new JFrame("Shape example");
frm.setSize(400, 400);
frm.add(new ShapeEample());
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
If you go this way, you have to write the same code in your every test class.

so let's get a better way . lets write a new class named WindowUtilites.
Code:

import java.awt.Container;
import javax.swing.JFrame;

public class WindowUtilites {
    public static JFrame openInJFrame(Container content, int width, int height,
            String title) {
        JFrame frame = new JFrame(title);
        frame.add(content);
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        return (frame);
    }
}


and now check the previous code again..
Code:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class ShapeEample extends JPanel {
    private Ellipse2D.Double circle = new Ellipse2D.Double(10, 10, 350, 350);
    private Rectangle2D.Double square = new Rectangle2D.Double(10, 10, 350, 350);

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.fill(circle);
        g2d.draw(square);
    }
   
    public static void main(String[] args) {
        WindowUtilities.openInJFrame(new ShapeEample(), 380, 400, "Shape");
    }
}

did you find any change? yap this time our main method is simple.
Code:

public static void main(String[] args) {
        WindowUtilities.openInJFrame(new ShapeEample(), 380, 400, "Shape");
    }

now you can add it many times in your different program.

Cheers !!


Last edited by bit0112-rokon on Wed Aug 04, 2010 1:10 am; edited 1 time in total (Reason for editing : topic title added)

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Reusing JFrame As windowUtilities

Post by BIT0122-Amit on Tue Aug 03, 2010 11:39 pm

nice post with a slight problem :p

Which day is it?

Confused?

:p I am thinking that, if it is tip of the day, which day's tip is it ? Razz

I think I should edit the title, and simply make it .. well.. you choose the title Very Happy

But there is another problem. The title, just WindowUtilities, might not be the best choice. The character limit is pretty big here for titles, so, use it as much as you want Very Happy
Can it be.. something like:

"Reusing JFrame Intelligently" ?

I hope you don't mind for nagging -_-! heh heh..

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Reusing JFrame As windowUtilities

Post by BIT0112-Rokon on Tue Aug 03, 2010 11:46 pm

I'm requesting to admin to change it. If there it is possible, make a section named tip of the day.

BTW thanks for your suggestion...


Last edited by bit0112-rokon on Sat Sep 18, 2010 7:02 am; edited 1 time in total

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Reusing JFrame As windowUtilities

Post by BIT0112-Rokon on Tue Aug 03, 2010 11:55 pm

And I've decided, I will continue here, I mean I'll continue only programming section. I think, you already noticed that, now a days, I'm not regular in other section.

And one more thing, I'm not expert too much. So mistake may happen and it is quite natural. If any mistake goes on, don't hesitate to inform me.!!

you know, there are four key things to be a exert. They are,
Learn it,
Do it,
Discuss it,
and finally Teach it.

So now I can only first three.


best of luck admin!!!

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Reusing JFrame As windowUtilities

Post by BIT0122-Amit on Wed Aug 04, 2010 12:59 am

Huh?
change it to what?
I only suggested, The choice is yours.

And making a tip of the day section for each programming language... will make the bloated forum even more bloated.

Currently we are trying to figure out a way to integrate different sections so that it can be easily managed and be more smaller.

Yeah I know.. I am acting evil and stuffs for not giving the section.

But if you are the only one posting in that section, then it might not be a good choice.

Better, we make a index topic, like this:

And make it locked, so that noone but with special privileges can post there.

Now, will you take the responsibility for updating the index from time to time?

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Reusing JFrame As windowUtilities

Post by BIT0112-Rokon on Wed Aug 04, 2010 1:09 am

Yap .. I can take the responsibility .. as you already noticed, none but only me working for it...

can you please make it Sticky


_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Reusing JFrame As windowUtilities

Post by BIT0122-Amit on Wed Aug 04, 2010 1:13 am

If you have not noticed, sticky topics do not go to the facebook.com/iitduforum or twitter.com/iitdu page automatically. neither it comes up in the feed generator, as a result, becomes invisible in Google.

I will make it sticky when it is due time.

Making you moderator of whole Java section then.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Reusing JFrame As windowUtilities

Post by BIT0122-Amit on Wed Aug 04, 2010 1:33 am

It is changed Cool! thanks to awesome me Cool!

new java moderator, roll out rock on!

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Reusing JFrame As windowUtilities

Post by BIT0122-Amit on Wed Aug 04, 2010 10:04 pm

did i mention rep++ for editing the topic title?

Also, I think mohaimin's graphics topic can be included in the index Wink

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Reusing JFrame As windowUtilities

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum