Java Practicals

2. Create an applet showing the string "Hello World" which is executed by an appletviewer.


            import java.applet.Applet;
            import java.awt.Color;
            import java.awt.Graphics;
            
            // 
            //  
            
            public class Applet2 extends Applet {
               
                public void paint(Graphics g){
                    g.setColor(Color.red);
                    g.drawString("hello world", 20, 20);
                }
                
            }
            
                    

Output


3. Create an applet in Java to draw a circle.


            /*
            
            
            */
            import java.awt.*;
            import java.applet.*;
            
            public class a7039applets3 extends Applet {
            
              public void init()
              {
                // set size
                setSize(300, 300);
            
                repaint();
              }
            
              // paint the applet
              public void paint(Graphics g)
              {
                // set Color for rectangle
                g.setColor(Color.red);
            
                // draw a ellipse
                // g.drawOval(100, 100, 150, 100);
                      g.drawOval(50,50,150,100);
            
              }
            }
            
                    

Output


5. Create an applet in Java to draw a rectangle.


            class RectangleApplet extends java.applet.Applet {
              public void paint(java.awt.Graphics g) {
                  g.drawRect(50, 50, 200, 100);
              }
          }
                    

Output


8. Create an applet in Java to draw a line.


            import java.applet.Applet;  
            import java.awt.*;  
              
            public class GraphicsDemo extends Applet{  
              
            public void paint(Graphics g)
            {  
            g.setColor(Color.red);  
            g.drawLine(20,30,20,300);  }}
            
            /*   */
                    
        
      
      

Output


9. Create an applet in Java to draw a oval.


            import java.applet.Applet;
            import java.awt.Graphics;
            
            class Applet_9 extends Applet {
                public void paint(Graphics g) {
                    // Draw an oval with the specified coordinates and dimensions
                    int x = 50;
                    int y = 50;
                    int width = 200;
                    int height = 100;
                    g.drawOval(x, y, width, height);
                }
            }
                    

Output


11. Create an applet in Java to change the background color.


            import java.applet.Applet;
            import java.awt.Color;
            import java.awt.Graphics;
            
            class Applet7104prog11 extends Applet {
                /*
                 * Topic = Applet
                 * prog.no 11 Java Program to change background color in applet.
                 */
                public Applet7104prog11() {
                    setBackground(Color.red);
                }
            
                public void paint(java.awt.Graphics g) {
            
                    g.drawString("Background Color Changed", 50, 50);
                }
                /*
                 * 
                 * 
                 */
            }
                    

Output


12. Create an applet in Java to draw rounded rectangle.


            import java.applet.Applet;
            import java.awt.Graphics;
            import java.awt.Graphics2D;
            import java.awt.geom.RoundRectangle2D;
            
            class Applet_12 extends Applet {
                public void paint(Graphics g) {
                    // Cast Graphics to Graphics2D for more advanced shapes
                    Graphics2D g2d = (Graphics2D) g;
            
                    // Draw a rounded rectangle with the specified coordinates and dimensions
                    int x = 50;
                    int y = 50;
                    int width = 200;
                    int height = 100;
                    int arcWidth = 30; // The width of the arc
                    int arcHeight = 30; // The height of the arc
            
                    RoundRectangle2D roundedRectangle = new RoundRectangle2D.Double(x, y, width, height, arcWidth, arcHeight);
                    g2d.draw(roundedRectangle);
                }
            }
                    

Output