You are on page 1of 8

Java AWT and Thread A Simple Painting Program

Version 1.2

What is new in jwPaint version 1.2? Just as in version 1.1, jwPaint Version 1.2 is a simple drawing program written in Java applet. The code will run in a windows-based internet browser or it could run under Java Applet Window Viewer. The added new feature is the toggle button which makes the drawing bounce around in the window frame. This is done by creating a single thread and runs it infinatly until the window is shut down. You can see the Boolean variables move and windowRunning in the run() method in the Runnable thread. Also notice, when run method is exited the thread will stop and would be discarded. The new Structure of the Program The structure of the new version has changed from version 1.1. Inner classes are implemented in this version. Each listener is inside of a class rather than included a big gigantic class. Those classes extend the thread class and are communicating with the thread class using the threads static variable. Notice that when the thread class is compiled, the inner classes are also automatically compiled as well. How to use the program? Download or run the applet through the internet. You must have a web browser and Java Runtime Environment installed for the applet to work. There are twelve color and five shapes to choose from. User may see the selection in the two drop-down menu box at the top of the drawing panel. The cursor coordinate position is also displayed under the drop boxes. (Pleas refer to Figure 1 in Index for screen shot) At start, user will see the drawing coordinates on the top left corner. Also, the frame would include a toggle button and table with two columns on the top follow by a great blank drawing area. To draw a line in black, click on the drop down menu and select the color in black and click on the drawing shape desired on the next drop down list. To change the shape or the color, consult to the correct drop box and choose the desired options available. To make the drawing move, move the mouse to point at the toggle button and click once. To stop the movement, simply click the same toggle button again. On start, the drawing will default in stationary state. Limitations of the program Only one object exists at a time. The object is stored in four integer variables indicating the x and y coordinates and its respective dimensions.

When user draws outside the boundary of the visible window, the movement of the shape will not be display properly. Also, the clear all function is not implemented and left blank. Other aspect of the program The init() function is call whenever the applet is called. The purpose of the init() function is to set up and initialize all the essential parts of the applet. The class also implements several event listeners using the implement keyword. Those interfaces are also implemented in the class; the ones that are not used are included in the class but are left as blank. Some of the listeners implemented are MouseListener, ItemListener, and MoustMotionListener. Also, repaint() is called whenever the mouse is in motion or active within boundary of the applet window. Event Listeners The mousePressed event will listen to users mouse and tries to capture the pressing of the button. If activated, the x and y coordinate are captured and stored in the variables. The mouseRelease event will listen to users button release. When event is captured, the new shape will be repainted. The itemStateChanged event will capture the activity within the drop boxes. It will check against the selected option (which is in String format) and sets the appropriate object attributes. It will then repaint. The mouseDragged event will capture the coordinate of the users mouse as it moves along the applet window and repaint the shape as the user does it. There are six important variables in this program. They are: 1. integer x_start Stores the users starting mouse vertical position 2. integer y_start Stores the users starting mouses horizontal position 3. integer x_end Stores the users ending mouses vertical position 4. integer y_end Stores the users ending mouses horizontal position 5. String selectOpt Stores the selected shape

6. String selectColor Stores the selected color Usage and Agreement The author of the program holds no responsibilities towards the consequences this demonstration might incur to users property. By using this program, the user has agreed to explore this demonstration at his or her own risks. This software is open sourced. Anyone has the authorization to own, duplicate, or alter the source at his or her interest. The author only asks that his credentials remain intact. Contact Information Original Author: Jason T Wu (jtywu at aim.com) Index

Figure 1 (Screen Shot) References http://www.java2s.com/Code/Java/Swing-JFC/BouncingCircle.htm K.N.King, Java Programming from the Beginning, Georgia State University, 2000.

// // // //

jwPaint version 1.2 with Threads and Inner Class demonstrations Source Code from www.java2s.com Modified by Jason T Wu

import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class jwPaint12 extends Applet implements Runnable,MouseListener,MouseMotionListener { // Created one thread for the moving animation Thread animator; // Static Class Variables static boolean move; static String selectColor, selectOpt; // Private Class Variables private boolean windowRunning; private int x_start, y_start, x_end, y_end; private int dx = 1, dy = 1; //projectile public void init() { // initial start looping the threads untill window exits windowRunning = true; addMouseListener(this); addMouseMotionListener(this); Button bUnbounce = new Button("Toggle Action"); bUnbounce.addActionListener(new BListener()); add(bUnbounce); Choice c0 = new Choice(); c0.addItem("Black"); c0.addItem("DarkGray"); c0.addItem("Gray"); c0.addItem("White"); c0.addItem("Blue"); c0.addItem("Red"); c0.addItem("Pink"); c0.addItem("Orange"); c0.addItem("Yellow"); c0.addItem("Green"); c0.addItem("Magenta"); c0.addItem("Cyan"); c0.addItemListener(new ColorListener()); add(c0); Choice c1 = new Choice(); c1.addItem("-clear all-"); c1.addItem("drawLine"); c1.addItem("drawOval"); c1.addItem("drawRect"); c1.addItem("fillOval"); c1.addItem("fill3DRect"); c1.addItemListener(new ShapeListener()); add(c1); } public void paint(Graphics g) { g.drawString(x_start+","+y_start,5,10); g.drawString(x_end+","+y_end,5,20); // SetColor; if(selectColor=="Black") g.setColor(Color.black);

else else else else else else else else else else else

if(selectColor=="White") g.setColor(Color.white); if(selectColor=="Blue") g.setColor(Color.blue); if(selectColor=="Red") g.setColor(Color.red); if(selectColor=="Pink") g.setColor(Color.pink); if(selectColor=="DarkGray")g.setColor(Color.darkGray); if(selectColor=="Gray") g.setColor(Color.gray); if(selectColor=="Yellow")g.setColor(Color.yellow); if(selectColor=="Orange")g.setColor(Color.orange); if(selectColor=="Green") g.setColor(Color.green); if(selectColor=="Magenta")g.setColor(Color.magenta); if(selectColor=="Cyan") g.setColor(Color.cyan);

// Draw if(selectOpt=="-clear all-") ; else if(selectOpt=="drawLine")g.drawLine(x_start,y_start,x_end,y_end); else if(selectOpt=="drawOval") { // drawOval property (have to take care of four different colum positions) if(x_end-x_start>0 && y_endy_start>0)g.drawOval(x_start,y_start,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); else if(x_end-x_start<0 && y_endy_start>0)g.drawOval(x_end,y_start,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); else if(x_end-x_start>0 && y_endy_start<0)g.drawOval(x_start,y_end,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); else if(x_end-x_start<0 && y_endy_start<0)g.drawOval(x_end,y_end,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); } else if(selectOpt=="drawRect") { // drawRect property if(x_end-x_start>0 && y_endy_start>0)g.drawRect(x_start,y_start,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); else if(x_end-x_start<0 && y_endy_start>0)g.drawRect(x_end,y_start,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); else if(x_end-x_start>0 && y_endy_start<0)g.drawRect(x_start,y_end,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); else if(x_end-x_start<0 && y_endy_start<0)g.drawRect(x_end,y_end,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); } else if(selectOpt=="fillOval") { if(x_end-x_start>0 && y_end-y_start>0) g.fillOval(x_start,y_start,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); else if(x_end-x_start<0 && y_endy_start>0)g.fillOval(x_end,y_start,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); else if(x_end-x_start>0 && y_endy_start<0)g.fillOval(x_start,y_end,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); else if(x_end-x_start<0 && y_endy_start<0)g.fillOval(x_end,y_end,Math.abs(x_end-x_start),Math.abs(y_end-y_start)); } else if(selectOpt=="fill3DRect") { if(x_end-x_start>0 && y_endy_start>0)g.fill3DRect(x_start,y_start,Math.abs(x_end-x_start),Math.abs(y_endy_start),true); else if(x_end-x_start<0 && y_endy_start>0)g.fill3DRect(x_end,y_start,Math.abs(x_end-x_start),Math.abs(y_endy_start),true); else if(x_end-x_start>0 && y_endy_start<0)g.fill3DRect(x_start,y_end,Math.abs(x_end-x_start),Math.abs(y_endy_start),true); else if(x_end-x_start<0 && y_endy_start<0)g.fill3DRect(x_end,y_end,Math.abs(x_end-x_start),Math.abs(y_end-y_start),true); } } public void animate() { // Bounce if we've hit an edge on a side of the frame Rectangle bounds = getBounds();

if ((x_start + dx < 0) || (x_end + dy > bounds.width)) dx = -dx; if ((y_start + dy < 0) || (y_end + dy > bounds.height)) dy = -dy; // Move the circle. x_start += dx; y_start += dy; x_end += dx; y_end += dy; repaint(); } public void run() { while(windowRunning) { // thread never stop unless window exits while (move) { animate(); try { Thread.sleep(100); } // Take the thread into a quick sleep before next run abouts catch (InterruptedException e){} // NO interruptions handled } } } public void start() { animator = new Thread(this); move = false; animator.start(); } public void stop() { // Stops the thread when window is exited windowRunning = false; } public public public public public { } public void mousePressed(MouseEvent e) { //remember cursor position at pressed in terms of x and y this.x_start = e.getX(); this.y_start = e.getY(); this.x_end = this.x_start; this.y_end = this.y_start; } public void mouseDragged(MouseEvent e) { this.x_end=e.getX(); this.y_end=e.getY(); repaint(); } } void void void void void mouseClicked(MouseEvent e){} mouseMoved(MouseEvent e){} mouseEntered(MouseEvent e){} mouseExited(MouseEvent e){} mouseReleased(MouseEvent e)

repaint();

class BListener extends jwPaint12 implements ActionListener { // Listens to the button actions public void actionPerformed(ActionEvent e) { if(move) move = false; else move = true; } } class ColorListener extends jwPaint12 implements ItemListener { public void itemStateChanged(ItemEvent e) { // Color List Listener this.selectColor=(String)e.getItem(); } } class ShapeListener extends jwPaint12 implements ItemListener { public void itemStateChanged(ItemEvent e) { // Shape List Listener this.selectOpt=(String)e.getItem(); } }

You might also like