Graphics components, Canvas | |
| |
/* import java.applet.*; import java.awt.*; public class TheComponents extends Applet { Choice SelectThis = new Choice (); String txt; TextField ThisTextField = new TextField (" Here the text field"); Checkbox ThisCheck = new Checkbox("Check a box:"); Label Label1 = new Label("label 1"); Scrollbar ThisScroll = new Scrollbar (Scrollbar.VERTICAL, 0, 8, -100, 100); public void init(){ Choice SelectThis1 = new Choice (); SelectThis.addItem("Coffee"); SelectThis.addItem("Milk"); SelectThis.addItem("Sugar"); SelectThis.addItem("Water"); add (SelectThis); add(ThisTextField); add(ThisCheck); add(Label1); add(new Label("label 2", Label.RIGHT)); add(ThisScroll); } } */ // Extending from convas /** a Canvas is a graphic region where we can draw geometrical figures, such as a rectangle, an oval, etc ... The object g in the method paint() is a canvas. Canvas class can extends from awt ( Abstract Windowing Toolkit ) class; as it does Graphics class (java.awt.Canvas like java.awt.Graphics) The class Canvas contains the methods: public Canvas(); public void addNotify(); public void paint(Graphics g); */ // To run the below, comment the above. --------------------------------- import java.awt.*; import java.applet.*; public class TheComponents extends Applet { public TheComponents() { //setSize(100, 50); not pertinent } public void init() { //Here, we can add the greenRectagle object. this.add(new GreenRectangle()); } String ThisText1 = "a Canvas is a graphic region where we can draw geometrical figures,"; String ThisText2 = "such as a rectangle, an oval, etc ..."; String ThisText3 = "The object g in the method paint is a canvas."; public void paint(Graphics g) { g.drawRect(50, 150, 500, 100); g.setColor(Color.red); g.drawString(ThisText1, 75,175); g.drawString(ThisText2, 75,200); g.drawString(ThisText3, 75,225); } public class GreenRectangle extends Canvas { public void paint(Graphics g) { Dimension d = this.getSize(); g.setColor(Color.green); g.fillRect(0, 10, d.width, d.height); //g.fillRect(100, 200, 100, 200); } public Dimension getMinimumSize() { return new Dimension(200, 200); //return new Dimension(50, 100); } public Dimension getPreferredSize() { return new Dimension(150, 100); // The most considered. } public Dimension getMaximumSize() { return new Dimension(200, 400); } }// End of class GreenRectangle }//End of class TheComponenets //---------------- end. ------------------ © The Scientific Sentence. 2007 |