Programms
Programms
*;
import javax.swing.JFrame;
DisplayGraphics(){
setSize(400,400);
setLayout(null);
setVisible(true);}
g.drawString("Hello",40,40);
setBackground(Color.WHITE);
g.drawOval(30,130,50, 60);
setForeground(Color.RED);
g.fillOval(130,130,50, 60);
new DisplayGraphics() ;
// m.setSize(400,400);
// m.setLayout(null);
//m.setVisible(true);
}
2.
import java.awt.event.*;
import java.awt.*;
public GraphicsDemo(){
setSize(new Dimension(400,800));
setVisible(true);
addWindowListener(new WindowAdapter(){
dispose();
});
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);
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};
g.drawPolygon(xpoints, ypoints,num);
new GraphicsDemo();
}
3.
import javax.swing.*;
import java.awt.*;
JTextFieldExample()
setLayout(new FlowLayout());
add(lblRollno); add(txtRollno);
add(lblName); add(txtName);
class JTextFieldJavaExample
frame.setBounds(200,250,150,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
4.
import java.awt.*;
import java.awt.event.*;
public MouseEventDemo(){
addMouseListener(this);
addMouseMotionListener(this);
addWindowListener(new WindowAdapter(){
dispose();
});
setSize(300,300);
setVisible(true);
repaint();
}
mousex=100; mousey=100;
msg="mouse entered";
repaint();
mousex=100; mousey=100;
msg="mouse exited";
repaint();
mousex=me.getX(); mousey=me.getY();
msg="mouse press";
repaint();
mousex=me.getX(); mousey=me.getY();
msg="mouse Release";
repaint();
repaint();
mousex=me.getX(); mousey=me.getY();
repaint();
g.drawString(msg1,50,50);
g.drawString(msg,mousex,mousey);
new MouseEventDemo();
5.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
Insets ins;
Random rand;
PaintPanel(){
setBorder(BorderFactory.createLineBorder(Color.RED,5));
super.paintComponent(g);
int x,y,x2,y2;
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);
class PaintDemo{
JLabel jlab;
PaintPanel pp;
PaintDemo(){
jfrm.setSize(300,300);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pp=new PaintPanel();
jfrm.add(pp);
jfrm.setVisible(true);
new PaintDemo();
6.
import java.awt.*;
import javax.swing.*;
public class SmileyApp extends JPanel {
super.paintComponent(g);
g.setColor(Color.YELLOW);
// draw Eyes
g.setColor(Color.BLACK);
// draw Mouth
// adding smile
g.setColor(Color.YELLOW);
app.add(smiley, BorderLayout.CENTER);
app.setSize(300, 300);
//app.setLocationRelativeTo(null);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);