import java.awt.
*;
import javax.swing.JFrame;
public class DisplayGraphics extends Frame {
DisplayGraphics(){
setSize(400,400);
setLayout(null);
setVisible(true);}
public void paint(Graphics g) {
g.drawString("Hello",40,40);
setBackground(Color.WHITE);
g.fillRect(130, 30,100, 80);
g.drawOval(30,130,50, 60);
setForeground(Color.RED);
g.fillOval(130,130,50, 60);
g.drawArc(30, 200, 40,50,90,60);
g.fillArc(30, 130, 40,50,180,40);
public static void main(String[] args) {
// DisplayGraphics m=new DisplayGraphics();
new DisplayGraphics() ;
// m.setSize(400,400);
// m.setLayout(null);
//m.setVisible(true);
}
2.
import java.awt.event.*;
import java.awt.*;
public class GraphicsDemo extends Frame {
public GraphicsDemo(){
setSize(new Dimension(400,800));
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
dispose();
});
public void paint(Graphics g){
setBackground(Color.yellow);
//lines
g.drawLine(20,40,100,90);
setForeground(Color.blue);
g.drawLine(20,90,100,40);
//setForeground(Color.red);
g.setColor(Color.green);
g.drawLine(40,45,250,80);
// Draw Rectangles
g.setColor(Color.red);
g.drawRect(20,150,60,150);
g.fillRect(110,150,60,50);
g.drawRoundRect(200,150,60,50, 15,15);
g.fillRoundRect(290,150,60,50,30,40);
// Draw ellipses and circles
g.drawOval(20,250,50,50);
g.fillOval(100,250,75,50);
g.drawOval(200,260,100,40);
// Draw arc
g.drawArc(20,350,70,70,0,-180);
g.fillArc(70,350,70,70,0,75);
//Draw polygon
int xpoints[]={20,200,20,200,20};
int ypoints[]={450,450,650,650,450};
int num =5;
g.drawPolygon(xpoints, ypoints,num);
public static void main(String args[]){
new GraphicsDemo();
}
3.
import javax.swing.*;
import java.awt.*;
class JTextFieldExample extends JFrame
JTextFieldExample()
setLayout(new FlowLayout());
JLabel lblRollno = new JLabel("Rollno : ");
JTextField txtRollno = new JTextField(5);
JLabel lblName = new JLabel("Name :");
JTextField txtName = new JTextField("xyz",15);
add(lblRollno); add(txtRollno);
add(lblName); add(txtName);
class JTextFieldJavaExample
public static void main(String args[])
JTextFieldExample frame = new JTextFieldExample();
frame.setTitle("JTextField in Java Swing Example");
frame.setBounds(200,250,150,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
4.
import java.awt.*;
import java.awt.event.*;
public class MouseEventDemo extends Frame implements MouseListener, MouseMotionListener{
String msg =" ";
String msg1 ="You are in mouse demo";
int mousex=0, mousey=0;
public MouseEventDemo(){
addMouseListener(this);
addMouseMotionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
dispose();
});
setSize(300,300);
setVisible(true);
public void mouseClicked (MouseEvent me){
msg1="You are in mouse clicked";
msg="-- click received";
repaint();
}
public void mouseEntered (MouseEvent me){
msg1="You are in mouse entered";
mousex=100; mousey=100;
msg="mouse entered";
repaint();
public void mouseExited (MouseEvent me){
msg1="You are in mouse exited";
mousex=100; mousey=100;
msg="mouse exited";
repaint();
public void mousePressed (MouseEvent me){
msg1="You are in mouse pressed";
mousex=me.getX(); mousey=me.getY();
msg="mouse press";
repaint();
public void mouseReleased (MouseEvent me){
msg1="You are in mouse Released";
mousex=me.getX(); mousey=me.getY();
msg="mouse Release";
repaint();
public void mouseDragged (MouseEvent me){
msg1="You are in mouse Dragged";
mousex=me.getX(); mousey=me.getY();
msg="mouse at" + mousex + "," + mousey;
repaint();
public void mouseMoved (MouseEvent me){
msg1="You are in mouse moved";
mousex=me.getX(); mousey=me.getY();
msg="mouse move at" + mousex + "," + mousey;
repaint();
public void paint( Graphics g){
g.drawString(msg1,50,50);
g.drawString(msg,mousex,mousey);
public static void main (String args[]){
new MouseEventDemo();
5.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class PaintPanel extends JPanel{
Insets ins;
Random rand;
PaintPanel(){
setBorder(BorderFactory.createLineBorder(Color.RED,5));
rand = new Random();
protected void paintComponent (Graphics g){
super.paintComponent(g);
int x,y,x2,y2;
int height = getHeight();
int width = getWidth();
ins = getInsets();
for(int i=0;i<10;i++){
// x=rand.nextInt(width-ins.left);
//y=rand.nextInt(height-ins.bottom);
//x2=rand.nextInt(width-ins.left);
//y2=rand.nextInt(height-ins.bottom);
// setForeground(Color.red);
x=rand.nextInt(width);
y=rand.nextInt(height);
x2=rand.nextInt(width);
y2=rand.nextInt(height);
System.out.print("x=" +x);
System.out.print(", y=" +y);
System.out.print(", x2=" +x2);
System.out.println(", y2=" +y2);
g.drawLine(x,y,x2,y2);
class PaintDemo{
JLabel jlab;
PaintPanel pp;
PaintDemo(){
JFrame jfrm=new JFrame("Paint Demo");
jfrm.setSize(300,300);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pp=new PaintPanel();
jfrm.add(pp);
jfrm.setVisible(true);
public static void main(String args[]){
new PaintDemo();
6.
import java.awt.*;
import javax.swing.*;
public class SmileyApp extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.YELLOW);
g.fillOval(30, 30, 200, 200);
// draw Eyes
g.setColor(Color.BLACK);
g.fillOval(55, 65, 30, 30);
g.fillOval(135, 65, 30, 30);
// draw Mouth
g.fillOval(50, 110, 120, 60);
// adding smile
g.setColor(Color.YELLOW);
g.fillRect(50, 110, 120, 30);
g.fillOval(50, 120, 120, 40);
public static void main(String[] args) {
SmileyApp smiley = new SmileyApp();
JFrame app = new JFrame("Smiley App");
app.add(smiley, BorderLayout.CENTER);
app.setSize(300, 300);
//app.setLocationRelativeTo(null);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);