G
gerrymcc
Hello, I hope it's okay to post to the three groups simultaneously.
I've got a set of four applets, all of which use the same GUI,
except for one change. I want to reuse the 100+ lines of setup code
for the GUI, but can't seem to do that because of the single line that
changes. Cutting and pasting the redundant code works, but it's a
pretty dull solution.
The idea of the following is that I have an Answer class (subclass of
Panel) called studentWork. studentWork contains an array of
EditableMeasure types (which are Canvas subclasses). In applet Two I
have a mix of EditableMeasures and EditableMeasureTypeTwos, in applet
Three I have a mix of EditableMeasures and EditableMeasureTypeThrees,
etc. This is the only change to the GUI, and have had to make the
single change in init() each time - studentWork = new Answer(this,
selectedMode); or studentWork = new AnswerType2(this, selectedMode);
or new AnswerType3( etc). The code below doesn't work, I guess
because the call to super.init() must use the setStudentWork() from
class One, rather than the definition I want, in class Two.
Is there any way the code from init() and itemStateChanged can be
reused and still create the relevant type of studentWork?
Thanks for any help,
Gerard
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class One extends Applet implements ActionListener,
ItemListener{
Answer studentWork;
Panel workArea;
int [] selectedMode;
public void init() {
setStudentWork();
// 100+ lines of gui setup follows
workArea = new Panel();
workArea.add( studentWork);
}
void setStudentWork(){
studentWork = new Answer( this, selectedMode);
}
public void itemStateChanged( ItemEvent ie){
switch( modeChoice.getSelectedIndex()){
case 0: selectedMode = aChoice;
break;
. ...
}
setStudentWork();
workArea.add(studentWork);
}
public void actionPerformed( ActionEvent e){...}
}
class EditableMeasure extends Canvas implements MouseListener,
MouseMotionListener{
// hundreds of lines
}
class Answer extends Panel{
public EditableMeasure [] bar;
public Answer() {}
public Answer(One a, int [] part){
setLayout( new FlowLayout( FlowLayout.LEFT,0,0));
bar = new EditableMeasure[ part.length];
for( int i = 0; i<part.length; i++){
if( i == 0)
bar = new EditableMeasure( a, 1);
else if( i == part.length - 1)
bar = new EditableMeasure( a, 2);
else
bar = new EditableMeasure( a);
add( bar);
}
}
}
class Two extends One{
public void init(){
super.init();
}
void setStudentWork(){
studentWork = new AnswerType2( this, selectedMode);
}
}
class EditableMeasureType2 extends Canvas etc{}
class AnswerType2 extends Answer{
public AnswerType2 (Two a, int [] part){
setLayout( new FlowLayout( FlowLayout.LEFT,0,0));
bar = new EditableMeasure[ part.length];
for( int i = 0; i<part.length; i++){
if( i == 0)
bar = new EditableMeasureType2( a, 1);
else if( i == part.length - 1)
bar = new EditableMeasure( a, 2);
else
bar = new EditableMeasureType2( a);
add( bar);
}
}
}
I've got a set of four applets, all of which use the same GUI,
except for one change. I want to reuse the 100+ lines of setup code
for the GUI, but can't seem to do that because of the single line that
changes. Cutting and pasting the redundant code works, but it's a
pretty dull solution.
The idea of the following is that I have an Answer class (subclass of
Panel) called studentWork. studentWork contains an array of
EditableMeasure types (which are Canvas subclasses). In applet Two I
have a mix of EditableMeasures and EditableMeasureTypeTwos, in applet
Three I have a mix of EditableMeasures and EditableMeasureTypeThrees,
etc. This is the only change to the GUI, and have had to make the
single change in init() each time - studentWork = new Answer(this,
selectedMode); or studentWork = new AnswerType2(this, selectedMode);
or new AnswerType3( etc). The code below doesn't work, I guess
because the call to super.init() must use the setStudentWork() from
class One, rather than the definition I want, in class Two.
Is there any way the code from init() and itemStateChanged can be
reused and still create the relevant type of studentWork?
Thanks for any help,
Gerard
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class One extends Applet implements ActionListener,
ItemListener{
Answer studentWork;
Panel workArea;
int [] selectedMode;
public void init() {
setStudentWork();
// 100+ lines of gui setup follows
workArea = new Panel();
workArea.add( studentWork);
}
void setStudentWork(){
studentWork = new Answer( this, selectedMode);
}
public void itemStateChanged( ItemEvent ie){
switch( modeChoice.getSelectedIndex()){
case 0: selectedMode = aChoice;
break;
. ...
}
setStudentWork();
workArea.add(studentWork);
}
public void actionPerformed( ActionEvent e){...}
}
class EditableMeasure extends Canvas implements MouseListener,
MouseMotionListener{
// hundreds of lines
}
class Answer extends Panel{
public EditableMeasure [] bar;
public Answer() {}
public Answer(One a, int [] part){
setLayout( new FlowLayout( FlowLayout.LEFT,0,0));
bar = new EditableMeasure[ part.length];
for( int i = 0; i<part.length; i++){
if( i == 0)
bar = new EditableMeasure( a, 1);
else if( i == part.length - 1)
bar = new EditableMeasure( a, 2);
else
bar = new EditableMeasure( a);
add( bar);
}
}
}
class Two extends One{
public void init(){
super.init();
}
void setStudentWork(){
studentWork = new AnswerType2( this, selectedMode);
}
}
class EditableMeasureType2 extends Canvas etc{}
class AnswerType2 extends Answer{
public AnswerType2 (Two a, int [] part){
setLayout( new FlowLayout( FlowLayout.LEFT,0,0));
bar = new EditableMeasure[ part.length];
for( int i = 0; i<part.length; i++){
if( i == 0)
bar = new EditableMeasureType2( a, 1);
else if( i == part.length - 1)
bar = new EditableMeasure( a, 2);
else
bar = new EditableMeasureType2( a);
add( bar);
}
}
}