How to Handle Event in Applet along with Graphics in Core Java

In this tutorial how to handle event in Applet along with Graphics in Core Java is shown.

Code:

MyAppletEven.java

/* <applet code=”MyAppletEven” height=”400″ width=”400″> </applet>*/

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

public class MyAppletEven extends Applet
{
int no;
String flag;
Button b;
Label l;
TextField t;

public void init()
{
b = new Button(“OK”);
l = new Label(“Enter a number:”);
t = new TextField(30);

add(l);add(t);add(b);

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
no=Integer.parseInt(t.getText());

if(no%2==0)
{
flag=”green”;
repaint();
}
else
{
flag=”red”;
repaint();
}
}
});
}

public void paint(Graphics g)
{
if(flag.equals(“green”))
{
g.setColor(Color.green);
g.drawString(“No: “+no,100,200);
}
else
{
g.setColor(Color.red);
g.drawString(“No: “+no,100,200);
}
}
}

https://youtu.be/gsT8aAc7Ju8

Add a Comment

Your email address will not be published. Required fields are marked *