Powered by Blogger.

Java: Drawing Lines, Rectangles, and Ovals Java Applet Program

Drawing Lines, Rectangles, and Ovals Java Applet:-

Code 4-15 presents a variety if Graphics methods for drawing lines, rectangles, and ovals.

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

 public class TestLineRectOval extends JFrame{
   private static final long serialVersionUID=0;

   public TestLineRectOval(){
     super(”Lines Ractangles Ovals”);
     setSize(360, 450);
     show();
   }

   public void paint(Graphics g){
     g.setColor(Color.red);
     g.drawLine(50, 40, 300, 40);

     g.setColor(Color.blue);
     g.drawRect(50 ,60, 100, 60);
     g.fillRect(200, 60, 100, 60);

     g.setColor(Color.cyan);
     g.drawRoundRect(50, 150, 100, 60, 50, 50);
     g.fillRoundRect(200, 150, 100, 60, 50, 50);

     g.setColor(Color.yellow);
     g.draw3DRect(50, 250, 100, 60, true);
     g.fill3DRect(200, 250, 100, 60, true);

     g.setColor(Color.magenta);
     g.drawOval(50, 350, 100, 60);
     g.fillOval(200, 350, 100, 60);
   }

   public static void main(String[]args){
     TestLineRectOval tlro = new TestLineRectOval();
     tlro.addWindowListener(
       new WindowAdapter(){
         public void windowClosing(WindowEvent e){
           System.exit(0);
         }
       }
     );
   }
 }

Code 4-16 provides an password example using Jlabel, JtextField, JpasswordField, and Jbutton.

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

 public class Password extends JFrame{
   private static final long serialVersionUID=0;

   private JLabel l1, l2;
   private JTextField t1;
   private JPas swordField pw;
   private JButton b1, b2;

   public Password(){
     super(”Password Example”);
     Container c = getContentPane();
     c.setLayout(new FlowLayout());

     l1 = new JLabel(”Enter User Name”);
     c.add(l1);

     t1 = new JTextField(15);
     c.add(t1);

     l2 = new JLabel(”Enter Password”);
     c.add(l2);

     pw = new JPas swordField(10);
     c.add(pw);

     b1 = new JButton(”Enter”);
     b1.addActionListener(
       new ActionListener(){
         public void actionPerformed(ActionEvent e){
           if(t1.getText().equals(”Parvez”) && pw.getText(). equals (”123456
              JOptionPane.showMessageDialog(null, ”Welcome to Java”);
           }else{
              JOptionPane.showMessageDialog(null, ”Incorrect user name 38
           }
         }
       }
     );
     c.add(b1);

     b2 = new JButton (”Cancel”);
     b2.addActionListener(
       new ActionListener(){
          public void actionPerformed(ActionEvent e){
             String s = ” ”;
             t1.setText(s);
             pw.setText(s);
          }
       }
     );
     c.add(b2);

     setSize(300, 125);
     show();
     }

     public static void main(String[]args){
       Password PW = new Password();
       PW.addWindowListener(
         new WindowAdapter(){
           public void windowClosing(WindowEvent e){
              System.exit(0);
           }
         }
       );
     }
  }


Example of Jcomponents such as: JcheckBox, JradioButton, JcomboBox, Jlist, and JTextArea provided in code 4-17.

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

 public class TestJComt extends JFrame{
   private static final long serialVersionUID=0;

   private JCheckBox bold, italic;
   private JRadioButton male, female;
   private ButtonGroup radioGroup;
   private JComboBox cBox;
   private String[]str 1 = {”spring”, ”summer”, ”fall”};
   private JList colorList;
   private String[]str 2 = {”Red”, ”Green”, ”Blue”, ”Black”, ”White”, ”15 ”Orange”, ”Pink”, ”Magenta”, ”Sky”, ”Cyan 16 private JTextArea ta1;

   public TestJComt(){
     super(”Test JComponents”);
     Container c = getContentPane();
     c.setLayout(new FlowLayout());

     bold = new JCheckBox(”Bold”);
     c.add(bold);
     italic = new JCheckBox(”Italic”);
     c.add(italic);

     male = new JRadioButton(”Male”);
     c.add(male);
     female = new JRadioButton(”Female”);
     c.add(female);
     radioGroup = new ButtonGroup();
     radioGroup.add(male);
     radioGroup.add(female);

     cBox = new JComboBox(str 1);
     c.add(cBox);

     colorList = new JList(str 2);
     colorList.setVisibleRowCount(5);
     c.add(new JScrollPane(colorList));

     String s = ”Java is a object oriented programming language”;
     ta1 = new JTextArea(s, 10, 15);
     c.add(new JSc rollPane(ta1));

     setSize(200, 350);
     show();
   }

   public static void main(String[]args){
     TestJComt jc = new TestJComt ();
     jc.addWindowListener(
       new WindowAdapter(){
         public void windowClosing(WindowEvent e){
           System.exit(0);
         }
       }
     );
   }
 }

Using Example:

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

 public class OpenFile extends JFrame{
   private static final long serialVersionUID=0;
   private JButton b1;
   private JFileChooser jfc;

   public OpenFile(){
     super(”File Opener”);
     b1 = new JButton(”Open File Chooser”);
     b1.addActionListener(
       new ActionListener(){
          public void actionPer formed( ActionEvent e){
            abc();
          }
       }
     );
     getContentPane().add(b1, BorderLayout.NORTH);
     setSize(300, 200);
     show();
     }

     private void abc(){
       jfc = new JFileChooser();
       jfc.showOpenDialog(this);
     }

     public static void main(String[]args){
       OpenFile of = new OpenFile();
       of.addWindowListener(
          new WindowAdapter(){
              public void windowClosing(WindowEvent e){
                System.exit(0);
              }
          }
       );
     }
  }

0 comments:

Post a Comment