import java.awt.*; import java.lang.*; /** * This class visualizes the level of the actual generation. * * coding: h.sarg * modified: 01/98 */ public class RecombCanvas extends java.awt.Canvas implements java.awt.event.MouseListener { private final int NR=20; private boolean fontSet= false; private Font f; private FontMetrics fm; private int fw, fh; private int selectsize=0; private int popsize=0; private Individual[] indiv=null; private Population pop=null; private Population mut=null; private TripFrame[] trips=new TripFrame[NR]; private int framecount=0; private TravelData data; private boolean showtowns; private int largefonts; private int grid; private int mm; private boolean drawnew = false; private Image buffered_image = null; private ScrollPane scroll=null; private String nrstring="Nr"; private String parentsstring="Selection"; private String childstring="Rekombination"; private String mutstring="Mutation"; private String fitstring="Fitness"; /** * RecombCanvas constructor comment. */ public RecombCanvas() { super(); this.addMouseListener(this); } /** * sets the necessary data to drawing object * @param s pointer to the scrollpane * @param ps populationsize * @param ss selectionsize * @param a object which contains the values of the towns * @param b indicates if the towns should be labeled or not * @param c indicates the font size * @param grid value for the call of the subroutine setData * @param mm mutation method */ public void setRecombData(ScrollPane s, int ps, int ss, TravelData a, boolean b, int c, int grid, int mm) { this.data=a; this.showtowns=b; this.largefonts=c; this.grid=grid; fontSet = false; drawnew=false; pop=null; indiv=null; mut=null; this.mm=mm; if(2*ps > ss) { this.selectsize=ss; this.popsize=ps-1; } else { this.popsize=ps; this.selectsize=ss; } scroll = s; repaint(); } /** * sets the recombinated population to be drawn * @param pop population */ public void setPop(Population pop) { this.pop=pop; drawnew=true; repaint(); } /** * sets the mutated population to be drawn * @param pop population */ public void setMut(Population pop) { this.mut=pop; drawnew=true; repaint(); } /** * sets the selected individuals to be drawn * @param indiv array of individuals */ public void setSelection(Individual[] indiv) { this.indiv=indiv; drawnew=true; repaint(); } /** * redefines the update method for faster double buffering * @param g java.awt.Graphics */ public void update(Graphics g) { paint(g); } /** * create the used font and measure his size * @param g java.awt.Graphics */ private void setFont(Graphics g) { if(fontSet) return; if(largefonts==1) { f = new Font("Monospaced",Font.PLAIN, 12); } if(largefonts==0) { f = new Font("Monospaced",Font.PLAIN, 11); } if(largefonts==2) { f = new Font("Monospaced",Font.PLAIN, 13); } fm =g.getFontMetrics(f); fw =fm.stringWidth("A"); fh =fm.getAscent(); fontSet=true; } /** * disposes all TripFrames and is called before the parent window is closed */ public void disposeFrames() { for(int i=0;i-1) { trips[index]=trips[i]; index+=1; } } else { if(index==-1) index=i; framecount--; state=true; } } //System.out.println("showing:" + framecount+ "index= "+index+"\n"); return state; } /** * redefines the paint method, implements double buffering * @param a java.awt.Graphics */ public void paint(java.awt.Graphics g) { int i,j,x,y; int length=0; int oldlength=0; int mut1, mut2; String helpstr=""; String drawstr=""; //int help; if(drawnew) { setFont(g); // calculate the needed size of the Canvas if(indiv!=null) { x=fm.stringWidth(indiv[0].toPopString(false) )+3*fw; y=(int) ((selectsize)*(fh+4.5)+(fh+3)+fh+2); if(pop!=null) { x+=fm.stringWidth(pop.getIndiv(0).toPopString(false) )+2*fw; } if(mut!=null) { x+=fm.stringWidth(mut.getIndiv(0).toPopString(true) )+2*fw; } this.setSize(x, y); scroll.doLayout(); } buffered_image = this.createImage(this.getSize().width, this.getSize().height); Graphics gbuff = buffered_image.getGraphics(); gbuff.setColor(java.awt.Color.magenta); setFont(gbuff); gbuff.setFont(f); if(indiv!=null) { gbuff.drawString(nrstring,2*fw,fh+3); gbuff.drawString(parentsstring,5*fw,fh+3); gbuff.setColor(java.awt.Color.black); //draw the selected individuals j=0; for(i=0;i= 2) { x=e.getX(); y=e.getY(); if(indiv!=null) { ls=fm.stringWidth(indiv[0].toPopString(false) )+2*fw; if(x>(2*fw) && (x=(fh+5)) { y-=fh+5; a=(int) (y/(fh+3+1.5)); if(als && (x=(fh+5)) { y-=fh+5; a=(int) (y/(2*(fh+3)+3)); if(alp && (x=(fh+5)) { y-=fh+5; a=(int) (y/(2*(fh+3)+3)); if(a