I Think I have found What I needed... Actually I need to put my drawingPanel into a ScrollPane. and if my drawing is larger than the scrollpane. the Scrollbar should appear for me to scroll my drawPanel.
But it doesn't seems to work.. anyone can help?
here's part of my coding.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;
public class StackApplet extends JApplet implements Runnable {
LinkedList link = new LinkedList();
ButtonPanel buttonPanel = new ButtonPanel();
DrawingPanel drawPanel = new DrawingPanel();
JButton addLinkedListButton, deleteLinkedListButton;
JTextField addField;
private int maxWidth , maxHeight ; // animation boundary
// new stuff for dbl-buffering
Graphics offscreen ;
Image imageForOffscreen ;
Thread linkThread = null;
int i;
int startX; // Used to set the Start of the Position of the first Data in the Linked List
int startY; // Used to set the Start of the Position of the first Data in the Linked List
int fontX, fontY; // Used to keep track of the position(x,y) of data that is supposed to be drawn
int refX, refY;
boolean moveNewItem = false;
boolean moveHead = false;
boolean moveFirstRow = false;
boolean firstRowCompleted = false;
boolean secondRowCompleted = true;
int movingHeadX, movingNewItemX;
int allLinkedListX, movingLinkedListX;
int movingToNextRowX, movingToNextRowY;
int tempX;
boolean deleteButton = false;
boolean addButton = false;
public void init(){
// To on the setting of BorderLayout
////////////////////////////////////////
startX = 20;
startY = 100;
getContentPane().setLayout(new BorderLayout());
getContentPane().add(buttonPanel, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane(drawPanel);
scrollPane.setPreferredSize(new Dimension(200,200));
getContentPane().add(scrollPane, BorderLayout.CENTER);
maxWidth = 800;
maxHeight = 600;
imageForOffscreen = createImage( maxWidth , maxHeight );
offscreen = imageForOffscreen.getGraphics();
//linkThread = new Thread(this);
////////////////////////////////////////
}
// override update to prevent it from erasing the background
public void update( Graphics g )
{
paint( g );
}
public void run()
{
addLinkedListButton.setEnabled(false);
deleteLinkedListButton.setEnabled(false);
if(addButton){
if(link.size() ==1){
refX = startX;
refY = startY;
repaint();
pause(500);
while(refX <120){
refX= refX + 5;
repaint();
pause(100);
}
}
if(link.size()>1){
tempX = 120;
while(!firstRowCompleted){
repaint();
pause(1000);
moveNewItem = true;
moveHead = true;
if(moveNewItem == true){
for(movingNewItemX = -80; movingNewItemX <=20 ; movingNewItemX = movingNewItemX+5){
pause(100);
movingHeadX = 120;
repaint();
}
}// end of if(moveNewItem == true)
if(moveHead ==true){
pause(1000);
for( movingHeadX = 120; movingHeadX >20; movingHeadX = movingHeadX -5){
repaint();
pause(50);
}
moveFirstRow = true;
}// end of if(moveHead == true)
if(moveFirstRow == true){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
for(movingLinkedListX = 20; movingLinkedListX< 120; movingLinkedListX= movingLinkedListX + 5){
allLinkedListX = movingLinkedListX;
repaint();
pause(50);
}
firstRowCompleted = true;
}//end of if(moveFirstRow ==true)
}//end of while Loop (!firstRowCompleted)
if(link.size()>4)
secondRowCompleted = false;
while(!secondRowCompleted){
movingToNextRowY = startY;
movingToNextRowX = 520;
for(movingToNextRowY = startY; movingToNextRowY <=150; movingToNextRowY = movingToNextRowY +5){
repaint();
pause(100);
}
for(movingToNextRowX = 520; movingToNextRowX > 20; movingToNextRowX = movingToNextRowX -5){
repaint();
pause(100);
}
while(movingToNextRowY<240){
repaint();
pause(100);
movingToNextRowY = movingToNextRowY + 5;
}
secondRowCompleted = true;
}
}
}
if(deleteButton){
}
addLinkedListButton.setEnabled(true);
deleteLinkedListButton.setEnabled(true);
moveFirstRow = moveNewItem = moveHead = firstRowCompleted = addButton = deleteButton = false;
}
public void pause( int milliSec){
try{
Thread.sleep(milliSec);
}catch(InterruptedException e){
}
}
class DrawingPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
offscreen.setColor( getBackground() );
offscreen.fillRect( 0 ,0 ,maxWidth , maxHeight );
if(addButton){
if(link.size()==1){
paintHead(offscreen, refX, refY);
paintNode(offscreen, refX, refY);
paintListEnd(offscreen, refX, refY);
}
if(link.size()>1){
if(moveFirstRow==false){
refX = 120;
refY = 100;
for(i=1; i < link.size(); i++){
paintWholeLinkedList(offscreen, refX, refY);
if((i==1)&&(moveNewItem == false)&&(moveHead==false)){
paintHead(offscreen, refX, refY);
}
refX = refX + 100;
}
}
if((moveNewItem== true)&&(moveFirstRow==false)){
i = 0;
paintNode(offscreen, movingNewItemX, refY);
paintPointToNextNode(offscreen, movingNewItemX, refY);
paintTempHead(offscreen, movingNewItemX, refY);
}
if ((moveHead == true) &&(moveFirstRow==false)){
paintHead(offscreen, movingHeadX, refY);
}
if ((moveFirstRow == true)&&(firstRowCompleted == false)){
for(i = 0 ;i < link.size(); i++){
paintWholeLinkedList(offscreen, allLinkedListX, refY);
if(i==0)
paintHead(offscreen, allLinkedListX, refY);
allLinkedListX = allLinkedListX + 100;
}
}
if((firstRowCompleted == true) && (secondRowCompleted == false)){
for(i=0 ; i<link.size(); i++){
if(i !=4)
paintWholeLinkedList(offscreen, tempX, refY);
if(i == 4){
paintNode(offscreen, movingToNextRowX, movingToNextRowY);
System.out.println("X and Y :" + movingToNextRowX + " "+ movingToNextRowY);
}
if(i==0)
paintHead(offscreen, tempX, refY);
tempX = tempX + 100;
if(tempX>520)
tempX = 120;
}
}
}
}
if(deleteButton){
}
g.drawImage( imageForOffscreen , 0 , 0 , this );
}
public void paintWholeLinkedList(Graphics g, int x, int y){
//paintHead ( g , x, y);
paintNode ( g , x, y);
Graphics2D offscreen = (Graphics2D) g;
offscreen.setStroke( new BasicStroke(3) );
if(( i%4 == 3 ) && ( i != 0 ) && ( i+1 != link.size())&&(firstRowCompleted==true)){
// To Draw the Linked List to next row
paintPointToNextRow( g , x, y);
//refY = refY + 80;
//x = startX;
}
else if( i+1 == link.size() ){
// To The End Of Linked List
paintListEnd( g , x, y);
}
else{
//To Draw Arrow To Point To Next Linked List
paintPointToNextNode( g , x, y);
}
}
public void paintHead(Graphics g, int x, int y){
Graphics2D offscreen = (Graphics2D) g;
offscreen.setStroke( new BasicStroke(3) );
offscreen.setColor(Color.black);
offscreen.drawLine( x, y-20, x, y-60);
offscreen.drawLine( x, y-20, x-5, y-25);
offscreen.drawLine( x, y-20, x+5, y-25);
}
public void paintTempHead(Graphics g, int x, int y){
Graphics2D offscreen = (Graphics2D) g;
float dashes[] = {3,3};
offscreen.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,10, dashes, 0));
offscreen.setColor(Color.gray);
offscreen.drawLine( x, y-20, x, y-60);
offscreen.drawLine( x, y-20, x-5, y-25);
offscreen.drawLine( x, y-20, x+5, y-25);
}
public void paintNode(Graphics g, int x, int y){
// To Draw The Main Square Where Data is stored in linked list
Graphics2D offscreen = (Graphics2D) g;
offscreen.setColor(Color.red);
offscreen.fill3DRect(x, y, 40, 40, true);
// To Draw Square that links to the next Data
offscreen.setColor(Color.yellow);
offscreen.fill3DRect(x + 40, y + 10, 20, 20, true);
//To Draw a Circle To represent the Node of the Link List
offscreen.setColor(Color.black);
offscreen.fillOval(x + 46, y + 16, 8, 8);
// Because The Value In The Box May Not Be Centralized
// Therefore There's a Need to customize so that the Value Is
//Centralized in the Data Square.
if(String.valueOf(link.peek(i)).length() == 1){
fontX = 15;
fontY = 25;
}
else if(String.valueOf(link.peek(i)).length() == 2){
fontX = 9;
fontY = 25;
}
else{
fontX = 3;
fontY = 25;
}
Font type = new Font ("Monospaced", Font.BOLD, 20);
offscreen.setFont(type);
offscreen.drawString(String.valueOf(link.peek(i)), x + fontX, y + fontY);
}
public void paintListEnd(Graphics g, int x, int y){
Graphics2D offscreen = (Graphics2D) g;
offscreen.setStroke (new BasicStroke (3));
offscreen.drawLine( x + 50, y + 20, x + 50, y - 5 );
offscreen.drawLine( x + 50, y - 5, x + 80, y - 5 );
offscreen.drawLine( x + 80, y - 5, x + 80, y + 20 );
offscreen.drawLine( x + 70, y + 20, x + 90, y + 20 );
offscreen.drawLine( x + 74, y + 25, x + 86, y + 25 );
offscreen.drawLine( x + 78, y + 30, x + 82, y + 30 );
}
public void paintPointToNextNode(Graphics g, int x, int y){
Graphics2D offscreen = (Graphics2D) g;
offscreen.setStroke (new BasicStroke (3));
offscreen.drawLine( x + 50, y + 20, x + 50, y - 15);
offscreen.drawLine( x + 50, y - 15, x + 100, y - 15);
offscreen.drawLine( x + 100, y - 15, x + 100, y );
offscreen.drawLine( x + 100, y, x + 105, y - 5 );
offscreen.drawLine( x + 100, y, x + 95, y - 5 );
}
public void paintPointToNextRow(Graphics g, int x, int y){
Graphics2D offscreen = (Graphics2D) g;
offscreen.setStroke (new BasicStroke (3));
offscreen.drawLine( x + 50, y + 20, x + 50, y - 15 );
offscreen.drawLine( x + 50, y - 15, x + 80, y - 15 );
offscreen.drawLine( x + 80, y - 15, x + 80, y + 50 );
offscreen.drawLine( x + 80, y + 50, 20, y + 50 );
offscreen.drawLine( 20, y + 50, 20, y + 80 );
offscreen.drawLine( 20, y + 80, 25, y + 75 );
offscreen.drawLine( 20, y + 80, 15, y + 75 );
}
}
class ButtonPanel extends JPanel{
public ButtonPanel(){
setLayout(new FlowLayout());
setBackground(Color.black);
addField = new JTextField(3);
add(addField);
// creates an instance of button event handling
ButtonHandler handler = new ButtonHandler();
addLinkedListButton = new JButton( "Add" );
addLinkedListButton.setToolTipText("Click To Add Value Into Linked List");
addLinkedListButton.setBackground(Color.orange);
addLinkedListButton.addActionListener (handler);
add( addLinkedListButton);
deleteLinkedListButton = new JButton( "Delete");
deleteLinkedListButton.setToolTipText("Click To Delete From Linked List");
deleteLinkedListButton.setBackground(Color.orange);
deleteLinkedListButton.addActionListener(handler);
add( deleteLinkedListButton);
}
}
Thanks in advance!!