Startup
Since PaintApp.java is a Frame and implements the WindowListener, MouseListener, and MouseMotionListener interfaces, the program must set the size of the applet, add event listeners, and make the applet visible on startup.
19 | public class PaintApp extends Frame implements WindowListener, MouseListener, MouseMotionListener { |
public class PaintApp extends Frame implements WindowListener, MouseListener, MouseMotionListener {
42 43 44 45 46 47 48 49 50 51 52 53 54 | public static void main(String args[]) { // The main method handles the application's framework (such as event listeners) and makes the window visible PaintApp paintProgram = new PaintApp(); paintProgram.addWindowListener(paintProgram); paintProgram.addMouseListener(paintProgram); paintProgram.addMouseMotionListener(paintProgram); paintProgram.appWidth = 761 ; paintProgram.appHeight = 380 ; paintProgram.setSize(paintProgram.appWidth, paintProgram.appHeight); paintProgram.setVisible( true ); paintProgram.createApp(); } |
public static void main(String args[]) { // The main method handles the application's framework (such as event listeners) and makes the window visible PaintApp paintProgram = new PaintApp(); paintProgram.addWindowListener(paintProgram); paintProgram.addMouseListener(paintProgram); paintProgram.addMouseMotionListener(paintProgram); paintProgram.appWidth = 761; paintProgram.appHeight = 380; paintProgram.setSize(paintProgram.appWidth, paintProgram.appHeight); paintProgram.setVisible(true); paintProgram.createApp(); }
When createApp() is called, the menu bar, canvas, layers array, and toolbar are created. The menu is "click-able" but cannot be viewed yet until paint() is called.
56 57 58 59 60 61 62 63 64 | public void createApp() { // Menu bar, canvas, layers array, and toolbar are created menuHeight = 75 ; createCanvas(); layersArray = new Layers( this ); // The menu bar's selection boxes are created but not the buttons that you can see; i.e. buttons are invisible menuBar = new Menu( this , layersArray); toolbar = new Tools( this , menuBar, layersArray); firstPaint = true ; } |
public void createApp() { // Menu bar, canvas, layers array, and toolbar are created menuHeight = 75; createCanvas(); layersArray = new Layers(this); // The menu bar's selection boxes are created but not the buttons that you can see; i.e. buttons are invisible menuBar = new Menu(this, layersArray); toolbar = new Tools(this, menuBar, layersArray); firstPaint = true; }
After the main() has finished executing, then paint() is automatically called. During the first call to paint(), the menu bar is drawn and layers are created.
66 67 68 69 70 71 72 73 74 75 76 | public void paint(Graphics g) { // Called after paintProgram became visible and whenever the graphical output needs to be updated or repainted g2Frame = (Graphics2D) g; // The abstract Graphics2D class extends the abstract Graphics class if (firstPaint) { menuBar.createMenu(); // Menu items are drawn onto the menu off-screen image g2Frame.drawImage(menuBar.menuImage, 0 , 0 , this ); // Off-screen image is drawn onto the application; menu becomes visible firstPaint = false ; continuePaint = true ; } } |
public void paint(Graphics g) { // Called after paintProgram became visible and whenever the graphical output needs to be updated or repainted g2Frame = (Graphics2D) g; // The abstract Graphics2D class extends the abstract Graphics class if (firstPaint) { menuBar.createMenu(); // Menu items are drawn onto the menu off-screen image g2Frame.drawImage(menuBar.menuImage, 0, 0, this); // Off-screen image is drawn onto the application; menu becomes visible firstPaint = false; continuePaint = true; } }