Last active
November 25, 2017 03:41
-
-
Save ritwickdey/a0b2c0bfb1a32cfa53385a51d68231d7 to your computer and use it in GitHub Desktop.
Everything in one place - Applet Life Cycle, Parameter Passing ,Drawing, ButtonEvent, MouseEvent & DialogBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.applet.Applet; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
/* | |
<applet code="AppletDemo" width="320" height="320"> | |
<param name="myName" value="ritwick dey"/> | |
</applet> | |
*/ | |
public class AppletDemo extends Applet implements ActionListener, MouseMotionListener | |
{ | |
public void init() | |
{ | |
System.out.println("Applet - init"); | |
Button redBtn = new Button("Red"); | |
Button blueBtn = new Button("Blue"); | |
Button greenBtn = new Button("Green"); | |
add(redBtn); | |
add(blueBtn); | |
add(greenBtn); | |
redBtn.addActionListener(this); | |
blueBtn.addActionListener(this); | |
greenBtn.addActionListener(this); | |
addMouseMotionListener(this); | |
} | |
public void start() | |
{ | |
System.out.println("Applet - start"); | |
int num1 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1st Num: ")); | |
int num2 = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 2st Num: ")); | |
JOptionPane.showMessageDialog(null,"Sum = " + (num1+num2)); | |
} | |
public void stop() | |
{ | |
System.out.println("Applet - stop"); | |
} | |
public void destroy() | |
{ | |
System.out.println("Applet - destroy"); | |
} | |
public void paint(Graphics g) | |
{ | |
//parameter passing | |
String myName = getParameter("myName"); | |
if(myName == null) | |
myName = "no name"; | |
//drawing | |
g.drawString(myName, 50,50); | |
g.drawRect(70,70,130,130); | |
g.drawLine(70,70,130,130); | |
g.drawOval(70,70,130,130); | |
} | |
//button event | |
public void actionPerformed(ActionEvent ae) | |
{ | |
switch(ae.getActionCommand()) | |
{ | |
case "Red": | |
setBackground(Color.red); | |
break; | |
case "Green": | |
setBackground(Color.green); | |
break; | |
case "Blue": | |
setBackground(Color.blue); | |
break; | |
} | |
} | |
public void mouseMoved(MouseEvent me) | |
{ | |
System.out.println("Mouse is moving..."); | |
} | |
public void mouseDragged(MouseEvent me) | |
{ | |
System.out.println("Mouse is dragging..."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment