Monday, December 31, 2012

Java : Resize Component according to WIndow Resize

Here I post some of my own experiment about layout manager.. specially on about resize-ing.. I mean when we resize the window or frame using mouse, the component inside also resize accordingly to window size..

Below is some my code, and also below is just a bit experiment between 3 layout manager, GridBagLayout, BorderLayout, FlowLayout, all is implement on JFrame (not JPanel). I may do further experiment about this (try to implement on JPanel also with more Layout manager). I will update once I done another experiment.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;


/** Author Dell

*/

public class Resizeable{
    
    public static void main(String[] args) {
        LFrame testFrame = new LFrame();
        
        
    }
}

class LFrame extends JFrame{
    int valueI = 0; /**Assign value here to switch among layout 
                       Assign with value = 0,1,2*/
    LPanel testPanel;
    Double WindowWidth, WindowHeight, WScale, HScale;
    
    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 300;
    public LFrame(){
        setTitle("Test Layout");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GridBagLayout gridBagLayout = new GridBagLayout();
        BorderLayout borderLayout = new BorderLayout();
        FlowLayout flowLayout = new FlowLayout();
        
        testPanel = new LPanel(this);
        
        switch (valueI) {
            case 0 : setLayout(gridBagLayout);
                     add(testPanel, new GBC(0,0).setWeight(100, 100).setFill(GBC.BOTH));
                     break;
            case 1 : setLayout(borderLayout);
                     add(testPanel, BorderLayout.CENTER);
                     break;
            case 2 : setLayout(flowLayout);
                     add(testPanel);
                     break;
                
        }
        
        addComponentListener(new ComponentListener() {

            @Override
            public void componentResized(ComponentEvent e) {
                WindowWidth = getSize().getWidth();
                WindowHeight = getSize().getHeight();
                WScale = WindowWidth/DEFAULT_WIDTH;
                HScale = WindowHeight/DEFAULT_HEIGHT;

                testPanel.setScale(WScale, HScale);

                showWindowSize();
                testPanel.showPanelSize();
                testPanel.showComponentSizeScale();
                testPanel.showPosition();
                revalidate();
            }

            @Override
            public void componentMoved(ComponentEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void componentShown(ComponentEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void componentHidden(ComponentEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        
        setVisible(true);
    }
    
    public void showWindowSize(){
        System.out.println("Window Width : " + getWidth() + " Height : " + getHeight());
    }
    
    public int getWindowWidthSize(){
        return getWidth();
    }
    
    public int getWindowHeightSize(){
        return getHeight();
    }
}

class LPanel extends JPanel{
    LComponent testComponent;
    LFrame aReferFrame;
    Double WScale = 1.0;
    Double HScale = 1.0;
    class LComponent extends JComponent{
        public void paintComponent(Graphics g){
            Graphics2D g2 = (Graphics2D) g;
            
            Rectangle2D rect = new Rectangle2D.Double(0, 0, 150, 150); 
            /**
             *  Set 150 so it easier to predict with bare eyes
             *  Always fill a quarter of WIndows size
             */
            g2.scale(WScale, HScale);
            g2.draw(rect);
        }
    }
    
    public LPanel(LFrame referFrame){
        aReferFrame = referFrame;
        /**
         * aReferFrame is a variable of LFrame
         * It's contain a reference to the Frame I created earlier (above)
         */
        //setPreferredSize(new Dimension(300, 300));
        //setPreferredSize(new Dimension(aReferFrame.getWindowWidthSize(), aReferFrame.getWindowHeightSize()));
        /**
         * setPreferredSize didn't use because I set Fill attribute in GridBagConstraint of this Panel as BOTH,
         * so as default it will put the panel in the largest possible size.
         * I mean this -> add(testPanel, new GBC(0,0).setWeight(100, 100).setFill(GBC.BOTH));
         * For other layout manager, you may try to remove of the comment // for setPreferredSize
         */
        GridBagLayout gridBagLayoutPanel = new GridBagLayout();
        setLayout(gridBagLayoutPanel);
        
        testComponent = new LComponent();
        add(testComponent, new GBC(0, 0).setWeight(100, 100).setAnchor(GBC.CENTER).setFill(GBC.BOTH));
        
    }
    
    public void setScale(Double Wscale, Double Hscale){
        WScale = Wscale;
        HScale = Hscale;
    }
    
    public void showPanelSize(){
        System.out.println("Panel Width : " + getWidth() + " Height : " + getHeight());
    }
    
    public void showComponentSizeScale(){
        System.out.println("Component Width : " + testComponent.getWidth() + " Height : " + testComponent.getHeight());
        System.out.println("Component WScale : " + WScale + " HScale : " + HScale);
    }

    public void showPosition(){
        System.out.println("Panel Position X : " + getX() + " Y : " + getY());
    }
}
/**
 * Below is a Helper class of GridBagConstraint
 */
class GBC extends GridBagConstraints {
    
    public GBC(int gridx, int gridy){
        this.gridx = gridx;
        this.gridy = gridy;
        
    }
    
    public GBC(int gridx, int gridy, int gridwidth, int gridheight){
        this.gridx = gridx;
        this.gridy = gridy;
        this.gridwidth = gridwidth;
        this.gridheight = gridheight;
    }
    
    public GBC setAnchor(int anchor){
        this.anchor = anchor;
        return this;
    }
    
    public GBC setFill(int fill){
        this.fill = fill;
        return this;
    }
    
    public GBC setWeight(double weightx, double weighty){
        this.weightx = weightx;
        this.weighty = weighty;
        return this;
    }
    
    public GBC setInsets(int distance){
        this.insets = new Insets(distance, distance, distance, distance);
        return this;
    }
    
    public GBC setInsets(int top, int left, int bottom, int right){
        this.insets = new Insets(top, left, bottom, right);
        return this;
    }
    
    public GBC setIpad(int ipadx, int ipady){
        this.ipadx = ipadx;
        this.ipady = ipady;
        return this;
    }
}

I try to explain a bit: Start From : (Remember the Layout that I test is Layout for JFrame, so the focus in resizing is the JPanel)
  1. GridBagLayout , in GridBagLayout the panel inside can resize according to windows size.

    In above the test is perform in [setFill(GBC.BOTH)] then when the window in maximize sate. If I put setFill(GBC.VERTICAL) or setFill(GBC.HORIZONTAL) or setFill(GBC.NONE) then the panel size will become one (1) for width (VERTICAL), one (1) for height (HORIZONTAL) or one (1) for both(NONE). This will happen unless you specified the size using setPreferredSize() (the part that I put comment in LPanel class). changing setWeight value didn't do any change because in above the panel only one. So GridBagLayout is very flexible.

  2. BorderLayout, in Border Layout also the panel size can resize according to window size.

    In above I put the LPanel into BorderLayout.CENTER, so the layout manager will automatically set the LPanel in widest size possible (have same behavious as setFill(GBC.BOTH)). In max size of window the Panel will have a quarter of window size. You can do much in Border Layout.

  3. FlowLayout, In Flow Layout you cannot do any change due to a PreferredSize set.

    If you didn't setPreferredSize() for LPanel then the LPanel will have size of one (1). So then you set it up. After you set it up then the size cannot be changed anymore.

    but one thing that I catch is, due to resize of window, the panel didn't change size but the the position do change!! Now I put a tracer method, showPosition(). You may see what happen.. hahaha, I have no explaination regarding this...

    But as I know, Flow Layout will put all component fit in smallest space possible, means all component in one line if possible. If not the Flow Layout will put some component exactly below other component.
That's it that I can tell you, I am not a master in Java, but I keep learning(studing) to be better.

Updated!! : Now, I finally put some additional in a day after the first publish.. ^^.. After a further experiment... I got  a conclusion.. actually I am not very sure in my conclusion because the experiment didn't conducted by follow research method^^. But let me share what I have.

There a some Layout that need the panel being specified in certain size and resulting the panel cannot being re-sized (fixed), they are Flow Layout and Spring Layout.. The rest is able to resize (no need setPreferredSize on Panel)..

My method of testing is simple. I just set the layout using setLayout() on the LPanel what Layout I want to try. Then I add the component (I add 2 components) and in some case if needed I will specified the exact position (e.g. BorderLayout need pre-specified position [PAGE_START, CENTER, LINE_END, etc.])

You may test by yourself if you curious more.. hehe^^ if you find any finding outside of what I have explained then I'll appreciate if you also share through comment below..^^

Hope this can inspire you.

Happy Expressing..!!

Sunday, December 30, 2012

Update Windows 7

Here I have small habit in updating my Window 7. I regularly update Windows 7 but until now I never get detected.

The key is manually choose which part to update. First set the automatic update off. Control Panel - Windows Update - Change Setting (On the left of window) - Choose "Check for update but let me choose whether to download and install them".

Now back to Windows Update. do check update.. after several minute (depend on internet connection speed). After donw now there are 2 option that can be click near Install Update button, "XX important update are available" and "XX optional update are available" .. XX indicate the number.

click the important one.. you may update all except this items.. which are:
Update for Windows 7 (KB971033)
   "This update to Windows Activation Technologies detects activation exploits and tampering to key Windows system files."

Another update that I never done are:
1. Update for Windows 7 (KB2718704)
     "Install this update to resolve an issue which requires an update to the certificate revocation list omn Windows system and to keep your systems certificate list up to date."
2. Update for Windows  7 (KB2779562)
     "Install this update to resolve issued caused by revised daylight saving time and time zone laaws in several countries."

The last 2 update that I mention, I never update that because I think it didn't useful to me, (at least in my opinion). The rest you can update, it can enhanced your PC performance like mine.

When the first time I use Windows 7 Ultimate before SP1, my windows explorer often get crash and force restart, especially when I open big file/folder (ex. contain HD/BlueRay movie). But now.. It's fine the crash rarely happen..

Until now this habit give me no problem. My OS almost perfectly work fine. So, hope this small post can be useful to you..

Happy Expressing..!!

My Exprerience with Dell N4010 Webcam

Hello.. here I post my experience regarding my Laptop Dell 4010 (Inspiron 14R) Webcam.

When I newly bought this laptop the webcam worked perfectly!! but because I re-set my own setting to BIOS about how the laptop reading OS source. I mean, I make my laptop boot from very first removable device such as disket or USB port, to second, from CD/DVD ROM, the the last my master Harddisk. Because that setting, My laptop often failed to boot my Windows 7 OS..(when boooting come out a message that tell me I don't have OS installed).. I think It's because of the OS itself (wrong installation maybe) so I format all drive and re-install new Windows 7 Ultimate. Because of that I lost all driver for hardware installed inside..

SO, from that day I cannot use Bluetooth, Webcam, Receive/Transmit Wifi for almost A YEAR..!ckckck.!! SO now let's just focus on Webcam..

So I start searching in Internet the driver for my Webcam.. but nothing convincing because most my tries failed (I forgot what I tried) But the driver I tried (which left till today) is R168730. I install and result nothing.. If I try recall again maybe I ever try another software such as Live!Cam and etc.. But all nothing..

I gave up for several month and when I met my friend who sold me this laptop, I ask him the resolve. He told me to install "Webcam Max" so I can use Webcam (You may search in Google the download link, it not hard to find... and it's need a license key, you know what to do).. After I found the crack. WOW!! I can do webcam now.!!! hahaha...

after that I left it until yesterday, yesterday I try once again to re-upgrade my Laptop.. Do updating all my software including driver.. Update driver can be done on www.dell.com/support/ So I found out the new driver R230103. This driver also well known as Dell Webcam Central.. You can find the download link in Google.. but mine from http://ftp.dell.com/monitors/Dell_SX2210-Monitor_Webcam%20SW%20RC1.1_%20R230103.exe  Surprisingly.. after I install this.. My Webcam become better (now I can set Anti-blur motion, thanks to the Webcam Central just installed).. Without Webcam Max I still can do webcam using Dell Webcam Central.

Now if you can install and use other support software that use webcam such as Live!Cam.

Some people encounter the Imaging Device - Integrated Webcam undetected. I don't know if I ever encounter this problem.. but If I do, I will try do updating my Window 7.. be careful not updating your windows and get a warning regarding ingenuity of your OS. you may see my another small post about Update Windows 7. If it didn't do any good, you may seeking help from Google again..

Hope this small post of my experience can inspire you in troubleshoot your 'trouble'.

Happy Expressing..!!

Tuesday, December 25, 2012

Solved : SHM along x-axis;(d2x/dt2) + Ax =0

A point moves with SHM along an x-axis according to the equation
 
The period of this motion is...

Answer:

Sunday, December 23, 2012

Ceritaku Mendaki Sibayak

Hello... hari ini gw baru saja kembali dari luar medan.. darimana? dari hutan..!! hahah
capeknya kaki gw.. masuk hutan naik turun naik turun naik turun.. becek lagi.. hahaha.. napa gw masuk hutan?? itu dia.. gw ikut teman-teman daki gunung..!!! WOW gitu? haha.. iya daki gunuung sibayak, (demi SUNRISE!!)

Awalnya gw tertarik dengan mendaki, lantaran gw pernah ikut guru SMA gw ke Air Terjun Dua Warna.. (jgn tanya dimana itu? klo ga tau tanya mbah Google).. itu lumayan perjalanan kira-kira 3 jam, kok lama? ya karena rombongannya makin jalan makin memanjang (macam karet aja.. hehe) itu ga terlalu capek.. jadi rasanya ingin lagi..

Saturday, December 15, 2012

Solved : An Object Held by 4 Strings in Moving Car

For this post I post a solved question of Physics subject. Here the question :

An object with mass m held by two horizontal strings and two vertical strings inside a stationary vehicle. If the vehicle start to accelerate by a, but the object inside still stay stationary toward the vehicle(object's position didn't change). Then, the distance did the car traveled for t time is...(answer in term of T1, T2, T3, T4, g, and t)
The answer is :

Friday, December 14, 2012

Solved! : √3 csc⁡ 20° - sec ⁡20°

 Here the mathematics problem that has been solved. √3 csc 20 - sec 20..


Note: sin 50 = cos 40

Yepp.. this is it.. hope this can help you.. ^_^.
Credit to : Yudi Rizky

Happy Expressing..!!