Showing posts with label JApplet. Show all posts
Showing posts with label JApplet. Show all posts

Friday, July 13, 2012

JFrame to JApplet Conversion Project | Dragging JButton

Program Description:

This is a simple Java project, a conversion project from JFrame to JApplet which was requested by someone in my Fan Page. I am trying to make the code as simple as possible and as short as possible so it can be understood easily.

Output:




Original Code:

import java.awt.event.*;
import javax.swing.*;

public class Demo extends JFrame implements MouseMotionListener{
JButton btn;
Demo(){
setSize(300, 300);
setVisible(true);
btn = new JButton("Drag me");
getContentPane().setLayout(null);
add(btn);
btn.setBounds(50, 50, 100, 40);
btn.addMouseMotionListener(this);
}
public static void main(String[] args){
new Demo();
}
public void mouseDragged(MouseEvent e) {
int x=(int) getMousePosition().getX();
int y=(int) getMousePosition().getY();
btn.setLocation(x, y);
}
public void mouseMoved(MouseEvent e) {

}

}

Code:

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class DemoApplet extends JApplet implements MouseMotionListener{
JButton btn = new JButton("Drag Me");

public void init() {
btn.setBounds(40,40,100,40);
Container pane = getContentPane();
FlowLayout flow = new FlowLayout();
pane.setLayout(flow);
pane.add(btn);
btn.addMouseMotionListener(this);
}

public void mouseDragged(MouseEvent e) {
int x=(int) getMousePosition().getX();
int y=(int) getMousePosition().getY();
btn.setLocation(x, y);
}
public void mouseMoved(MouseEvent e) {
}
}

Thursday, May 3, 2012

My First JApplet | How to create an Applet

Program Description:

Today we are going to create our first basic java applet program using JApplet component. This is just a very short and simple java code that you can really understand.

JApplet is a subclass of Applet, a class in the java.applet package. An applet is a visual window inside a webpage that means they need to be run as part of a World Wide Web page.

In creating an Applet it involves two files the java file where our java codes resides of course and the HTML file where our java codes will be running using the ".class" file.

In creating our class it should always extends to JApplet. Example:

public class myFirstApplet extends JApplet {

}

then next to our java applet code is our HTML code where we embed our java code using the ".class" file. Here is the basic code on how to embed our java applet code in a webpage.




1. Starts with an opening HTML tag <html>
2. Followed by an opening Applet tag <applet>
3. specify your java applet code by locating the ".class" file which is usually created if you compile your java codes.
4. Specify the width and the height of your applet in the webpage.
5. Closing the applet tag </applet>
6. Closing the html tag </html>

Note: Compile your java code and your HTML code then run the HTML file. I am using JCreatorPro so I automatically load the HTML file, then compile, and run. See output below:

Output:
Code:

Java Code

/**
* File: myFirstApplet.java
* Tiltle: My First JApplet | How to create an Applet
* Author: http://java-code-complete.blogspot.com/
*/

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;

public class myFirstApplet extends JApplet {

//Creating paint method so we can draw text on applet's background
public void paint(Graphics g) {

//Call the method paint
super.paint(g);

//Draw 3 strings at different locations
g.drawString("Welcome to my First Java Applet",30,30);
g.drawString("Welcome to Java Programming",30,50);
g.drawString("Thank you for Visiting: http://java-code-complete.blogspot.com",30,90);
}
}

HTML Code: