20-CS-694 Advanced Programming Techniques Spring 2012
Graphics

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

All lectures
Images

This series shows how to use many of the classes associated with drawing images. Click on the class names in the following table to see class details.

Color Graphics Image Toolkit
URL JPanel Canvas MalformedURLException

1.   RB1.java

  -   A color wheel is drawn every time some window event occurs. A JFrame contains only a paint method in which nested for loops paint pixels four at a time.
 
2.   RB2.java

  -   The color wheel is drawn once to a background buffer and the foreground is repainted with the background when a window event occurs. The paint method looks like this:
  public void paint (Graphics g) {  
     g.drawImage(oi, 0, 0, this);  
  }
 
3.   RB3.java

  -   A JPanel is bolted onto the JFrame but is set to be transparent using a.setOpaque(false); so the color wheel still shows.
 
4.   RB4.java

  -   A Panel can be bolted onto a JFrame and hide whatever is supposed to be drawn on the JFrame.
 
5.   Prog1.java

  -   This and the next six applets lead to code that allows a player to move checkers on a checkerboard and to remove them as well. In this applet a Board class is developed to acquire and store the image of a checkerboard from a url using the getImage method of the default Toolkit on a URL object. The Board class extends the Canvas class and is placed in the center of a JFrame called Prog1Frame.
 
6.   Prog2.java

  -   The Board class is expanded to store a checker image which is placed in the center of and on top of the checkerboard.
 
7.   Prog3.java

  -   The Board class implements the MouseListener and MouseMotionListener interfaces to act on mouse events. In mousePressed it is determined whether the mouse cursor is over the checker image. If so, the moving field is set to true. In mouseDragged if moving is true a new mouse position is obtained and the board is repainted with the checker in the new position. In mouseReleased moving is set to false. Since the image is drawn assuming the origin is its top left corner, there is an initial jerk of the checker which puts the mouse cursor on the top left corner. This is remedied in the next applet.
 
8.   Prog4.java

  -   Variables xdiff and ydiff are added to record the x and y position differences between the mouse cursor position and the top left corner of the checker image so no jerk occurs.
 
9.   Prog5.java

  -   The ability to add checkers to the board and move them around is implemented. A Vector of checkers maintains board positions in true pixels from the top left corner of the board for each checker added. A Position class is created and Position objects corresponding to checkers are placed in checkers when new checkers are created. The mousePressed is modified to search through the objects of checkers looking for a checker that the mouse cursor is over. The search occurs by increasing index which enforces the concept that one checker may be "on top of" another: implicitly the lower indexed objects in checkers are on top of the higher indexed objects. To enforce this visually, when painting the board the higher indexed objects in checkers should be drawn before the lower indexed objects - this is done two applets below this. A JButton is added to create new checkers which are displayed in the center of the board.
 
10.   Prog6.java

  -   A JButton is added to allow removing checkers. Click the 'Remove Checker' button and then click on any buttons to be removed. Click on 'Stop Removing' to disable the ability to remove buttons in this way.
 
11.   Prog7.java

  -   An entire board is populated with checkers. Any checker can be moved. Any checker can be removed as above. A checker that is moved becomes the last to be drawn and the first to have its position checked against that of the mouse cursor.