// Basic drawing template setup // This program show how to create a java program that draws simple // 2D graphical shapes without animation. import java.awt.*; import javax.swing.*; public class drawingtemplate extends JFrame { protected int width; // dimensions of graphical window protected int height; // system-dependent: may include menu bar protected Graphics display; // used to draw images // constructor public drawingtemplate(int x, int y) // constructor sets width,height { width=x; height=y; this.setBounds(0,0,width,height); setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); display = this.getGraphics(); display.setColor(Color.white); // background color display.fillRect(0,0,width,height); // clear background try { Thread.sleep(500); } catch (Exception e) {} // synch with sys draw(); // user-define drawing function } protected void draw() { display.setColor(Color.blue); // draw line between x1,y2 and x2,y2, 0,0 is top-left corner display.drawLine(0,0,width-1,height-1); //diagonal line // draw 80x60 rectangle with top-left corner at point 50,250: display.drawRect(50,250,80,60); display.setColor(Color.red); // change color display.fillRect(100,50,40,50); // draws a solid rectangle // draws an oval bounded by a rectangle: display.drawOval(200,150,60,60); // draws a circle display.setColor(Color.green); display.fillOval(width/2-50,height-200,100,60); // solid oval display.setColor(Color.black); display.drawString("This is the drawing template program",400,50); }//triangle public void paint(Graphics g) {} // override autopaint method // main must create an instance of triangles class: public static void main(String[] args) { drawingtemplate t = new drawingtemplate(800,600); } } /* To write your own drawing program, you can create a file with a subclass of drawingtemplate and override the draw() method: import java.awt.*; import javax.swing.*; public class mydraw extends drawingtemplate { public mydraw() // custom constructor { super(640,480); // calls superclass constructor } @Override public void draw() { display.setColor(Color.yellow); display.fillRect(0,0,width,height); // yellow background display.setColor(Color.green); display.fillOval(width/2-100,height/2-100,200,200); } public static void main(String[] av) { mydraw mywindow = new mydraw(); } }//mydraw subclass of drawingtemplate */