CubeCanvas.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code=CubeCanvas height=500 width=500>
</applet>
*/
public class CubeCanvas extends Applet
{
cube c=new cube();
public void init()
{
add(c);
c.setSize(500,500);
}
}
class cube extends Canvas implements MouseMotionListener,MouseListener
{
int flag=0;
public void init()
{
}
public void paint(Graphics g)
{
addMouseMotionListener(this);
addMouseListener(this);
if(flag==0)
{
flag=1;
g.setColor(Color.yellow);
g.fillRect(140,140,150,111);
g.setColor(Color.blue);
g.fillRect(200,200,160,111);
g.setColor(Color.black);
g.drawRect(139,139,150,112);
g.setColor(Color.black);
g.drawRect(200,200,160,111);
g.drawLine(139,139,200,200);
g.drawLine(290,140,360,200);
g.drawLine(140,250,200,310);
g.drawLine(290,250,360,310);
}
else
{
g.setColor(Color.blue);
g.fillRect(140,140,150,111);
g.setColor(Color.yellow);
g.fillRect(200,200,160,111);
g.setColor(Color.black);
g.drawRect(139,139,150,112);
g.setColor(Color.black);
g.drawRect(200,200,160,111);
g.drawLine(139,139,200,200);
g.drawLine(290,140,360,200);
g.drawLine(140,250,200,310);
g.drawLine(290,250,360,310);
}
}
public void mouseMoved(MouseEvent me)
{
//showStatus("X = "+me.getX()+" "+ "Y ="+me.getY());
}
public void mouseDragged(MouseEvent e)
{
//showStatus("Mouse Dragged");
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
if(flag==0)
{
flag=1;
}
else
{
flag=0;
}
repaint();
}
public void mousePressed(MouseEvent e)
{
if(flag==0)
{
flag=1;
}
else
{
flag=0;
}
repaint();
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
Note :- Run this program Step 1.java CubeCanvas.java
Step 2appletviewer CubeCanvas.java
No comments:
Post a Comment