BLOGGER TEMPLATES AND Blogger Templates »

Kamis, 10 November 2011

"Text Moving" Web Application Using Java Applet (jApplet)


One of the reason why Java becomes a very popular language is because it can run on multiple platforms, be it a PC, mobile phone, or even in a web browser!

In this post, you will see one simple application that can run in a Chrome browser (or any other browser with JRE available) which involves buttons, text, event-handling and of course: the Java Applet form.

The "Text-Moving" Application Running on Chrome

The application above is used with eight different buttons, each representing directions. When each button is pressed, the text on the center will move to the direction the button is representing, so pressing "Up" button will make the text move up, "Down" button to go down, and so on.

Here's some important point of the source code to show you how it works. In order for the application to be able to run on a browser, we must use the jApplet form.



int xPos = 0;
int yPos = 0;
int delta = 10;
public void getPos()
{
    xPos = jLabel1.getX();
    yPos = jLabel1.getY();
}

private void upButtonMouseClicked(java.awt.event.MouseEvent evt)                                     
{                                         
    getPos();
    yPos = yPos - delta;
    jLabel1.setLocation(xPos, yPos);
}                                    

private void upRightButtonMouseClicked(java.awt.event.MouseEvent evt)                                          
{                                              
    getPos();
    yPos = yPos - delta;
    xPos = xPos + delta;
    jLabel1.setLocation(xPos, yPos);
}                                         

private void rightButtonMouseClicked(java.awt.event.MouseEvent evt)                                        
{                                            
    getPos();
    xPos = xPos + delta;
    jLabel1.setLocation(xPos, yPos);
}                                       

private void downRightButtonMouseClicked(java.awt.event.MouseEvent evt)                                            
{                                                
    getPos();
    xPos = xPos + delta;
    yPos = yPos + delta;
    jLabel1.setLocation(xPos, yPos);
}                                           
private void downButtonMouseClicked(java.awt.event.MouseEvent evt)                                       
{                                           
    getPos();
    yPos = yPos + delta;
    jLabel1.setLocation(xPos, yPos);
}                                      
private void downLeftButtonMouseClicked(java.awt.event.MouseEvent evt)                                           
{                                               
    getPos();
    xPos = xPos - delta;
    yPos = yPos + delta;
    jLabel1.setLocation(xPos, yPos);
}                                          
private void leftButtonMouseClicked(java.awt.event.MouseEvent evt)                                       
{                                           
    getPos();
    xPos = xPos - delta;
    jLabel1.setLocation(xPos, yPos);
}                                      
private void upLeftButtonMouseClicked(java.awt.event.MouseEvent evt)                                         
{                                             
    getPos();
    xPos = xPos - delta;
    yPos = yPos - delta;
    jLabel1.setLocation(xPos, yPos)
}

0 komentar: