Skip to main content

Example of Canvas in AWT

Canvas

import java.applet.*;
import java.awt.*;
/*<applet code="CanvasDemo" width=500 height=200></applet>*/
class Canvas1 extends Canvas
{
public void paint(Graphics g)
{
g.setColor(Color.lightGray);
Dimension d=getSize();
g.drawRect(0,0,d.width-1,d.height-1);
g.drawLine(0,d.height/2,d.width,d.height/2);
g.drawLine(d.width/2,0,d.width/2,d.height-1);
g.setColor(Color.blue);
double dx=4*Math.PI/d.width;
double x=-2*Math.PI;
int h=d.height;
for(int i=0;i<d.width-1;i++)
{
int y1=(int)((h-h*Math.sin(x))/2);
int y2=(int)((h-h*Math.sin(x+dx))/2);
g.drawLine(i,y1,i+1,y2);
x+=dx;
}
}
}
public class CanvasDemo extends Applet
{
public void init()
{
Canvas1 c1=new Canvas1();
c1.setSize(401,501);
add(c1);
}
}

Comments

Popular posts from this blog

36 to 40 Java questions

36. What is the difference between yielding and sleeping? When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state. 37. What is the difference between preemptive scheduling and time slicing? Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors. 38. What is mutable object and immutable object? If a object value is changeable then we can call it as Mutable object. (Ex., StringBuffer, …) If you are not allowed to change the value of an object, it is immutable object. (Ex., String, Integer, Float, …) 39. What is the purpose of Void class? The Void class is an uninstantiable placehold...

26 to 30 Questions of Java

26. What is the difference between this() and super()? this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor. 27. What is Domain Naming Service(DNS)? It is very difficult to remember a set of numbers(IP address) to connect to the Internet. The Domain Naming Service(DNS) is used to overcome this problem. It maps one particular IP address to a string of characters. For example, www. mascom. com implies com is the domain name reserved for US commercial sites, moscom is the name of the company and www is the name of the specific computer, which is mascom’s server. 28. What is URL? URL stands for Uniform Resource Locator and it points to resource files on the Internet. URL has four components: http://www. address. com:80/index.html, where http – protocol name, address – IP address or host name, 80 – port number and index.html – file path. 29. What is RMI and steps involved in devel...

What is Servet ?

A servlet is a java class that extends an application hosted on a web server. Handles the HTTP request-response process (for our purposes) Often thought of as an applet that runs on a server. Provides a component base architecture for web development, using the Java Platform The foundation for Java Server Pages (JSP). Alternative to CGI scripting and platform specific server side applications.