JAVA WEEK12(B)
/* Week 12 : b) Write a Java program that allows the user to draw lines, rectangles
and ovals. */



import...
JAVA WEEK12(B)
    ovals.addActionListener(this);
    rect.addActionListener(this);
    free.addActionListener(this);
    ...
Upcoming SlideShare
Loading in …5
×

Java Week12(B) Notepad

428 views
386 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
428
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java Week12(B) Notepad

  1. 1. JAVA WEEK12(B) /* Week 12 : b) Write a Java program that allows the user to draw lines, rectangles and ovals. */ import java.awt.*; import java.awt.event.*; import java.applet.*; /* < applet code=shape width=300 height=300> < /applet> */ public class LabPro19 extends Applet implements MouseListener,ActionListener,MouseMotionListener { Button lines,ovals,rect,free; int ch; int x,y,x2,y2,k; public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { x=e.getX(); y=e.getY(); } public void mouseReleased(MouseEvent e) { x2=e.getX(); y2=e.getY(); repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) {if(k==1){ x2=e.getX(); y2=e.getY(); repaint();} } public void init() { lines=new Button("line"); ovals=new Button("oval"); rect =new Button("rect"); free=new Button("free hand"); add(lines);add(ovals);add(rect);add(free); lines.addActionListener(this); Page 1
  2. 2. JAVA WEEK12(B) ovals.addActionListener(this); rect.addActionListener(this); free.addActionListener(this); addMouseListener(this); addMouseMotionListener(this); } public void actionPerformed(ActionEvent ki) { if(ki.getSource()==lines) { ch=1; k=0;} else if(ki.getSource()==ovals) { ch=2; k=0;} else if(ki.getSource()==rect) {ch=3; k=0;} else if(ki.getSource()==free) { ch=4; k=1;} } public void paint(Graphics g) { switch (ch) { case 1:g.drawLine(x,y,x2,y2); break; case 2:g.drawOval(x,y,(x2-x),(y2-y)); break; case 3:g.drawRect(x,y,(x2-x),(y2-y)); break; case 4:repaint(); break; } } /*public void update(Graphics g) { paint(g); }*/ } Page 2

×