Radio Buttons In Java Applets[How to] Radio Buttons in Java Goal-- Write a java applet program in which we can select shapes using Radio Buttons and Enlarge or Shrink them using buttons. Explanation:- Explanation is given in program through Comments. Program:- /*<applet code=applet width=400 height=400></applet>*/ import java.awt.*; import java.applet.*; import java.awt.event.*; public class applet extends Applet implements ActionListener,ItemListener { //Radio Buttons to be Selected. Checkbox c1; Checkbox c2; Checkbox c3; //Button to Click Button b1; Button b2; int x,y; CheckboxGroup cb;//Necessary to only allow one radio button to be selected at the same time public void init() { x=100; y=100; cb=new CheckboxGroup(); setBackground(Color.pink); // Setting Background color c1=new Checkbox("Circle",false,cb); //Initializing Radio Buttons. c2=new Checkbox("Rectangle",false,cb); c3=new Checkbox("Round Rectangle",false,cb); b1=new Button("Enlarge"); //Initializing Button and give it a text. b2=new Button("Shrink"); add(c1); // Adding Buttons to Applet. add(c2); add(c3); add(b1); add(b2); c1.addItemListener(this); c2.addItemListener(this); c3.addItemListener(this); b1.addActionListener(this); b2.addActionListener(this); } public void paint(Graphics g) { if(c1.getState()) //Getting state of C1 radio button { g.setColor(Color.cyan); g.fillOval(150,150,x,y); } if(c2.getState()) //Getting state of C2 radio button { g.setColor(Color.magenta); g.fillRect(150,150,x,y); } if(c3.getState()) //Getting state of C3 radio button { g.setColor(Color.orange); g.fillRoundRect(150,150,x,y,15,15); } } public void actionPerformed(ActionEvent e) //Called just After the user perform Action. { String s=e.getActionCommand(); if(s.equals("Enlarge")) { x+=10; y+=10; repaint(); } if(s.equals("Shrink")) { x-=10; y-=10; repaint(); } } public void itemStateChanged(ItemEvent i) //invoked when state of any item changes. { x=100; y=100; repaint(); } } Output:- This article is contributed by Shubham Gupta. If you want to share more information about topic discussed above or find anything incorrect Please do comments. Radio Buttons In Java Applets[How to] Radio Buttons in Java Goal-- Write a java applet program in which we can select shapes using Radio Buttons and Enlarge or Shrink the... + Read more »
Java Program to Draw INDIAN Flag(Applet) [How to] INDIAN FLAG BY JAVA APPLETSSteps:-1.Firstly Draw flag on a paper and calculate its Dimensions. 2.After that Analyze Flag and extract different shapes from it. 3.Then draw those shapes using pre-defined function. Program:-import java.applet.*;import java.awt.*;import java.lang.*;/*<applet code='flag.java' height=500 width=800></applet>*/public class flag extends Applet {public void init() { } public void paint(Graphics g) { Color c4= new Color(173,216,230); setBackground(c4); int x[]={490,500,505,515}; int y[]={90,100,100,90}; Color c1=new Color(244,196,48); Color c2=new Color(150,75,0); g.setColor(Color.cyan); g.fillRect(500,100,5,400); g.setColor(c1); g.fillRect(505,102,180,40); g.setColor(Color.white); g.fillRect(505,142,180,40); g.setColor(Color.green); g.fillRect(505,182,180,40); g.drawLine(500,100,490,90); g.drawLine(505,100,515,90); g.drawLine(490,90,515,90); g.setColor(c2); int n=4; g.fillPolygon(x,y,n); g.fillArc(490,80,25,20,0,180); int z[]={500,495,495,475,475,530,530,510,510,505}; int w[]={500,500,505,505,515,515,505,505,500,500}; Color c3= new Color(123,122,192); g.setColor(c3); g.fillPolygon(z,w,10); g.setColor(Color.blue); g.drawOval(575,142,40,40); int x1=595; int y1=162; double x2,y2; Double degre= 0.0; double d=0.0 int i; int r=20; for(i=1;i<=24;i++) { degre=(double)d*(3.14/180); x2=x1+(double)r*Math.cos(degre); y2=y1+(double)r*Math.sin(degre); g.drawLine(x1,y1,(int)x2,(int)y2); d=d+(360/24); } } }How to Run:-First Compile simply using- > javac filename.javaand run using following Command- > appletviewer filename.java Output:- Indian Flag Applet If you want to share more information about topic discussed above or find anything incorrect Please do comments. Java Program to Draw INDIAN Flag(Applet) [How to] INDIAN FLAG BY JAVA APPLETS Steps:- 1.Firstly Draw flag on a paper and calculate its Dimensions. 2.After that Ana... + Read more »