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...

46 to 50 Java Questions

46. What is Constructor? A constructor is a special method whose task is to initialize the object of its class. It is special because its name is the same as the class name. They do not have return types, not even void and therefore they cannot return values. They cannot be inherited, though a derived class can call the base class constructor. Constructor is invoked whenever an object of its associated class is created. 47. What is an Iterator ? The Iterator interface is used to step through the elements of a Collection. Iterators let you process each element of a Collection. Iterators are a generic way to go through all the elements of a Collection no matter how it is organized. Iterator is an Interface implemented a different way for every Collection. 48. What is the List interface? The List interface provides support for ordered collections of objects. Lists may contain duplicate elements. 49. What is memory leak? A memory leak is where an unreferenced object that will ne...