Share the ideas and thoughts, become united ...
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Saturday, June 11, 2011

DateTime Picker in JDK 7

JDK 7 is in draft stage. The feature list of JDK 7 is great. Existing Java developer will instantly notice the feature list as a blessing :D .

Today I am going to show a JDK 7 SwingX Component, JXDatePicker. As JDK 7 is still in draft phase, You cannot get official JDK7 release. Instead you can download JDK7 Snapshot from this Link.
Now once you have got the JDK7 release you can test out the DatePicker.
JXDatePicker dtPicker = new JXDatePicker(System.currentTimeMillis());
dtPicker.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
          JOptionPane.showMessageDialog(null, datePicker.getDate().toString());
    }
});
Let the JDK prevail ...

Saturday, December 19, 2009

Simple Image Resizing Code ...

Some times you need to quickly resize your image to a different dimension. you may need to resize it to a smaller version (saving bandwidth, computation time etc) or need to scale it to a larger version (displaying).

you can use the following code to resize your image as per your requirement.

LANGUAGE: C#/JAVA. Can also be easily convert to C++ and other languages.


public static int[] rescaleRGBArray(int[] rgb, int oldWidth, int oldHeight, int newWidth, int newHeight) {
    int out[] = new int[newWidth*newHeight];
    for (int yy = 0; yy < newHeight; yy++) {
    int dy = yy * oldHeight / newHeight;
    for (int xx = 0; xx < newWidth; xx++) {
    int dx = xx * oldWidth / newWidth;
    out[(newWidth*yy)+xx]=rgb[(oldWidth*dy)+dx];
    }
    }
   return out; 

}

Hope this help you in your work.

Raise the desired event in java - it's possible and easy

I, myself is a beginner in programming languages. So, i encounter problem now and then. Recently i have faced a very interesting problem. As you know it is pretty much easy in C# to raise the desire event (or you don't know). But i was wondering how to do it in JAVA. Well i found it pretty much amazingly easy !!!

you just have to call dispatchEvent(AWTEvent e).

Explanation : This method takes the event that you want to raise and dispatches it (handles it).

Sample Code:

public class MainWindow extends JFrame implements WindowListener {
/* properties + methods */
public MainWindow() {
// bla bla codes
this.addWindowListener(this); // i am listening this window and the event handler is also def. in this class
// more codes
}
public void windowClosing(WindowEvent e) { // an event handler
// codes ...
System.out.println("dude this window is closing... save it.."); // code i am going to execute
// and still coding
}
public void raiseWindowCloseEvnt() { // user def. fn to raise the event
// last words before you close your window :)
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); // will i am dispatching a new event which is associated with this window (via this pointer). and i am likely to raise the Window_Closing event. Now i can raise the event just by calling this function.
}
public static void main(String[] args) {
MainWindow mw = new MainWindow();
mw.setVisible(true);
mw.raiseWindowCloseEvnt(); // see the output console :D
}
}

Pretty much easy isn't it? :)