• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending another class help!

 
Jean Paul Martin
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,
Im just learning new stuff on extending classes in java and i have a class InsertSorter and another class InserSortApplet. They are working great but however now i want to test speeds of two InsertSorts at the same time and hence i need help as to how i can define/extend 2 InsertSortApplet with using the same classes InsertSortApplet and InsertSort. In other words, I want to be able to see 2 Applets running at the same time on my html test file...please help! THANKS GUYS.
import java.applet.Applet;
import java.awt.*;
import java.awt.geom.*;
public class InsertSorter{
private int[] a;
private Applet applet;
private int alreadySorted = -1;
private int markPos = -1;
private static final int DELAY = 200;
public InsertSorter (int[] anArray, Applet anApplet){
a=anArray;
applet = anApplet;
}

public void sort() throws InterruptedException{
int n = a.length;
for(int i=1;i<=n-1;i++){
int c = a[i];
int j =0;
while(a[j]<c)j++;
for(int k=i;k>j;k--){
a[k]=a[k-1];
}
a[j]=c;
markPos = j;
alreadySorted =i;
pause(2);
}
}
public void pause(int steps) throws InterruptedException{
if(Thread.currentThread().isInterrupted())
throw new InterruptedException();
applet.repaint();
Thread.sleep(steps*DELAY);
}
public void draw(Graphics2D g2){
int deltaY = (applet.getHeight()-4)/a.length;
for(int i=0;i<a.length;i++){
if(i == markPos)
g2.setColor(Color.red);
else if(i <= alreadySorted && i != markPos)
g2.setColor(Color.blue);
else
g2.setColor(Color.black);
g2.draw(new Line2D.Double(0,2+i*deltaY,a[i],2+i*deltaY));
}
}
} // InsertSorter
=======================
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import com.sun.j3d.utils.applet.MainFrame;
public class InsertSortApplet extends Applet{
private InsertSorter sorter;
private Thread animation;
public InsertSortApplet (){
class MousePressListener extends MouseAdapter{
public void mousePressed(MouseEvent e){
if(animation != null && animation.isAlive())
animation.interrupt();
startAnimation();
}
}
MouseListener listener = new MousePressListener();
addMouseListener(listener);
setBackground(Color.lightGray);
sorter = null;
animation = null;
}
public void paint(Graphics g){
if(sorter == null) return;
Graphics2D g2 = (Graphics2D)g;
sorter.draw(g2);
}
private int[] randomIntArray(int num,int maxLength){
Random gen = new Random();
int[] exArray = new int[num];
for(int i=0;i<num;i++){
exArray[i]= gen.nextInt(maxLength-3)+2;
}
return exArray;
}
public void startAnimation(){
class AnimationThread extends Thread{
public void run(){
try{
sorter.sort();
}catch(InterruptedException e){}
}
}
int[] values = randomIntArray(100,300);
sorter = new InsertSorter(values,this);
animation = new AnimationThread();
animation.start();
}
public static void main(String[] args){
Frame frame = new MainFrame(new InsertSortApplet(),300,300);
}
} // InsertSortApplet
========
<HTML>
<body>
<p>Click on the applet to start the animation of Insertion Sort</p>
<applet code = "InsertSortApplet.class" width="300" height="300">
</applet>
</body>
</HTML>
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

define/extend 2 InsertSortApplet with using the same classes InsertSortApplet and InsertSort. In other words, I want to be able to see 2 Applets running at the same time on my html test file

If the goal is to have two applets running on the same page at the same time, all you have to do is to have two separate <applet> tags on the same HTML page; that's easy.
Otherwise, if you want your single applet to display two different animations, well, that's easy enough to do, but I'm not sure what you're saying about "the same classes InsertSortApplet and InsertSort". Are you comparing two different sets of data with the same algorithm, or two different algorithms with the same data? If it's the latter, then you probably want to create a whole new class InsertSort2 to hold the new algorithm, and then modify the applet to contain two different displays, each using a different sort mechanism.
 
Jean Paul Martin
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS FOR YOUR HELP! I was following your advice about breaking programs into different classes n stuff. It all worked! THANKS AGAIN
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic