1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
/* Draw 3D Rectangle & Square in Applet Window Example This java example shows how to draw 3-D rectangles and squares in an applet window using draw3DRect method of Graphics class. It also shows how to draw a filled 3-D rectangles and squares. */ /* <applet code="Draw3DRectanglesExample" width=200 height=200> </applet> */ import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; public class Draw3DRectanglesExample extends Applet{ public void paint(Graphics g){ g.setColor(Color.green); /* * To draw a 3-D rectangle in an applet window use, * void draw3DRect(int x1,int y1, int width, int height, boolean raised) * method. * * This method draws a 3-D rectangle of specified width and * height at (x1,y1) */ //this will draw a 3-D rectangle of width 50 & height 100 at (10,10) g.draw3DRect(10,10,50,100,true); /* * If you speficy same width and height, the draw3DRect method * will draw a 3-D square! */ //this will draw a 3-D square g.draw3DRect(100,100,50,50,true); g.setColor(Color.orange); /* * To draw a filled 3-D rectangle in an applet window use, * void fill3DRect(int x1,int y1, int width, int height, boolean raised) * method. * * This method draws a filled 3-D rectangle of specified width and * height at (x1,y1) */ //this will draw a filled 3-D rectangle of width 50 & height 100 at (10,10) g.fill3DRect(10,150,50,100,true); /* * If you speficy same width and height, the fill3DRect method * will draw a filled 3-D square! */ //this will draw a filled 3-D square g.fill3DRect(100,200,50,50,true); } } |
Example Output
how to call this function…i mean how can u pass the argument ‘g’
u cant instantiate any instance of graphics before calling the function so that u can pass that instance as an argument to the function .
Graphics g = new Graphics(); // this is wrong
even if u will do
Graphics g =getGraphics();
will give an error as it is not initialised
please help
to use paint you have to extend a class like JFrame or JPanel, which have paint so you can override it
how to draw 10 ovals in horizontal zigzag manner