/***
* Problem: Insert the missing code in the following methods of THIS program to modify the
* contents of the ArrayList variable named 'shapeList'
*
* Methods to insert shapes in 'shapeList'
* - addToIndex()
* - addToFront()
* - addToBack()
*
* Method to remove existing shapes from 'shapeList'
* - deleteAtIndex()
*
* Methods to rearrange the order of shapes in 'shapeList'
* - sendToBack()
* - bringToFront()
* - sendBackward()
* - bringForward()
*/
package prelims;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* @(#)ZOrder.java
*
* JFC ZOrder application
*
* @author
* @version 1.00 2011/7/7
*/
public class ZOrder extends JPanel {
static final long serialVersionUID = 1;
static final int WIDTH = 600;
static final int HEIGHT = 400;
static final ArrayList shapeList = new ArrayList();
/***
* DO NOT MODIFY THE CODE BEFORE THIS COMMENT
*/
public void addToFront(ZOrderShape shape) {
// add 'shape' so that it appears in front of ALL the other shapes ...
shapeList.add( 0,shape ); // --- just a sample statement (which does NOT 'add to front')
// ... and update the display on the panel
repaint();
}
public void addToBack(ZOrderShape shape) {
// add 'shape' so that it appears at the back of ALL the other shapes ...
shapeList.add( shape ); // put the shape at the back...
// ... and update the display on the panel
repaint();
}
public void addToIndex(ZOrderShape shape, int index) {
// add 'shape' at the specified 'index'...
shapeList.add ( shape );
// ... and update the display on the panel
repaint();
}
public void deleteAtIndex(int index) {
// remove from the list the shape that is found on the specified 'index' ...
shapeList.remove( index );
// ... and update the display on the panel
repaint();
}
public void sendToBack(int index) {
// move the shape with the specified index so that it will appear
// at the back of ALL the other shapes...
shapeList.add(shapeList.get(index));
shapeList.remove ( index );
// ... and update the display on the panel
repaint();
}
public void bringToFront(int index) {
// move the shape with the specified index so that it will appear
// in front of ALL the other shapes...
ZOrderShape shape = shapeList.remove (index);
shapeList.add ( 0,shape );
// ... and update the display on the panel
repaint();
}
public void sendBackward(int index) {
// move the shape with the specified index so that it will appear
// at the back of the next shape before it...
ZOrderShape shape = shapeList.remove (index+1);
shapeList.remove (index - 1);
shapeList.add ( 0,shape );
// ... and update the display on the panel
repaint();
}
public void bringForward(int index) {
// move the shape with the specified index so that it will appear
// in front of the next shape after it...
ZOrderShape shape = shapeList.remove (index-1);
shapeList.add ( shape );
// ... and update the display on the panel
repaint();
}
/***
* DO NOT MODIFY THE CODE AFTER THIS COMMENT
*/
// just a constructor
private ZOrder() {
ZOrderListener listener = new ZOrderListener(this);
// the following are methods inherited from the extended JPanel class
this.setBorder( new EtchedBorder(EtchedBorder.RAISED) );
this.setPreferredSize( new Dimension(WIDTH, HEIGHT) );
this.setBackground( Color.WHITE );
this.addMouseListener( listener );
this.addMouseMotionListener( listener );
}
// overloaded JPanel method - draws the ZOrderShapes on the panel
public void paintComponent(Graphics g) {
// invokes the paintComponent() method of the extended class, i.e. the JPanel class
super.paintComponent( g );
Graphics2D canvas = (Graphics2D)g;
canvas.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
// draw the shapes in 'shapeList' from LAST to FIRST...
for (int i=shapeList.size()-1; i>=0; i--) {
ZOrderShape shape = shapeList.get(i);
shape.draw( canvas );
}
}
// program execution starts here...
public static void main(String[] args) {
JFrame frame;
JPanel panel = ZOrderGUI.createInterface( new ZOrder() );
frame = new JFrame("Z-Order");
frame.setContentPane( panel );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
}
}
i have a problem in methods:
addToIndex()
deleteAtIndex()
sendBackward()
bringForward()
Anyone can help me?
- 8 views