⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 draw.java

📁 一个非常好的
💻 JAVA
字号:
/* Applet to solve the Traveling Salesman Problem
 * Copyright (C) 2002 Tobias Oetzel (Tobias.Oetzel@gmx.de)
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 2 of the License, or (at your option)
 * any later version. This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA. 
*/



// Draw Field 
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class draw extends JPanel implements MouseListener
{
  // user are allowed to click on Panel 
  protected boolean allowClick = true;
  protected boolean clicked = false;
  // maximum right, bottom bounds a city can located
  int DrawRight  = 480;
  int DrawBottom = 360;
  // pointer to main instance               
  private Image OffImage; 
  private Graphics OffGraphics;
  private tsp TspInstance;             

  private Color BGColor = new Color(230,230,230);

               
  // constructor, adding mouselistener
  public draw(tsp MyInstance)
  {
    TspInstance=MyInstance;

    Font myFont = new Font("System",0,12);
    setBackground(BGColor);
    setBounds(20,25,DrawRight,DrawBottom);
    addMouseListener(this);

  }
 
  public void paint(Graphics g) 
  {
    super.paint(g);
    if (OffImage == null)
    {
      OffImage = this.createImage(DrawRight,DrawBottom);
      OffGraphics = this.OffImage.getGraphics();
      OffGraphics.setColor(BGColor);
      OffGraphics.fillRect(0,0,DrawRight,DrawBottom);
    }
      
    g.drawImage(OffImage,0,0,this);

  }

  public void ClearAll()
  {
    if (OffImage == null)
    {
      OffImage = this.createImage(DrawRight,DrawBottom);
      OffGraphics = this.OffImage.getGraphics();
      OffGraphics.setColor(BGColor);
      OffGraphics.fillRect(0,0,DrawRight,DrawBottom);
    } 
    else
    {
      OffGraphics.setColor(BGColor);
      OffGraphics.fillRect(0,0,DrawRight,DrawBottom);
    }  
    paint(getGraphics());
  }  
  
 
  // nothing to do here, but must be implemented
  // due MouseListener-Interface issues
  public void mousePressed(MouseEvent e) 
  {
  }

  // nothing to do here, but must be implemented
  // due MouseListener-Interface issues
  public void mouseReleased(MouseEvent e) 
  {
  }

  // mouse cursor enters the panel
  // set the rrigght cursor
  public void mouseEntered(MouseEvent e) 
  {
    if (allowClick)
    {
      this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.HAND_CURSOR));
    } 
    else
    {
      this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
    }
  }

  // mouse cursor leaves the panel
  public void mouseExited(MouseEvent e) 
  {
    this.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
  }

  // mouse click, manage whether city must be added/remove
  // or all cities must remove 
  public void mouseClicked(MouseEvent e) 
  {
    if (allowClick)
    {
      clicked = true;
      if ((e.getModifiers())==e.BUTTON1_MASK)
      {
      	int tempx=e.getX();
     	  int tempy=e.getY();
     	
       	int result=TspInstance.MyCities.ManageCity(tempx,tempy);
     	  if (result==1)
     	  {
          DrawCity(tempx,tempy);
          paint(getGraphics());
        } 
        else if (result==2)
        {
          RemoveCity(tempx,tempy);
          paint(getGraphics());
        }  
      }
      else if ((e.getModifiers())==e.BUTTON3_MASK)
      {
        TspInstance.MyCities.RemoveAllCities();
        ClearAll();
      }	
    }
  }
  
  // draw a city
  public void DrawCity(Graphics g, int x,int y)
	{
     g.setColor(Color.red);
     g.fillOval(x,y,5,5);
	}

  public void DrawCity(int x,int y)
  {
     OffGraphics.setColor(Color.red);
     OffGraphics.fillOval(x,y,5,5);
  }

  public void DrawCity(Graphics g, city MyCity)
	{
     g.setColor(Color.red);
     g.fillOval(MyCity.x,MyCity.y,5,5);
	}

  public void DrawCity(city MyCity)
  {
     OffGraphics.setColor(Color.red);
     OffGraphics.fillOval(MyCity.x,MyCity.y,5,5);
  }

  // draw a removed city
  public void RemoveCity(Graphics g,city MyCity)
	{
     g.setColor(BGColor);
     g.fillOval(MyCity.x,MyCity.y,5,5);
	}

  public void RemoveCity(city MyCity)
  {
     OffGraphics.setColor(BGColor);
     OffGraphics.fillOval(MyCity.x,MyCity.y,5,5);
  }

  public void RemoveCity(int x,int y)
  {
     OffGraphics.setColor(BGColor);
     OffGraphics.fillOval(x,y,5,5);
  }
  
  // draw all cities
  public void DrawAllCities(Graphics g)
  {
	  city MyCity;
    for(int i=0;i<TspInstance.MyCities.GetMyCitiesSize();i++)
	  {
      MyCity=TspInstance.MyCities.GetCityAt(i);
	    DrawCity(g,MyCity);
    }
    repaint();
  }

  public void DrawAllCities()
  {
    city MyCity;
    // create offscreenImage
    if (OffImage == null)
    {
      paint(getGraphics());
    }  
    for(int i=0;i<TspInstance.MyCities.GetMyCitiesSize();i++)
    {
      MyCity=TspInstance.MyCities.GetCityAt(i);
      DrawCity(MyCity);
    }
    repaint();
  }
  
  // draw connectors between two cities
  public void DrawCityConnector(Graphics g,city ACity, city BCity)
  {
    g.setColor(Color.darkGray);
    g.drawLine((int)ACity.x+2,(int)ACity.y+2,(int)BCity.x+2,(int)BCity.y+2);
  }
  
  public void DrawCityConnector(city ACity, city BCity)
  {
    OffGraphics.setColor(Color.darkGray);
    OffGraphics.drawLine((int)ACity.x+2,(int)ACity.y+2,(int)BCity.x+2,(int)BCity.y+2);
  }

  public void DrawCityConnector(Graphics g,Color c,city ACity, city BCity)
  {
    g.setColor(c);
    g.drawLine((int)ACity.x+2,(int)ACity.y+2,(int)BCity.x+2,(int)BCity.y+2);
  }
  
  // draw a whole configuration
  // use a offscreenimage to avoid flickering
  public void DrawConfiguration(Vector Config)
  {
    // manage offscreen Image
    OffGraphics.setColor(BGColor);
    OffGraphics.fillRect(0,0,DrawRight,DrawBottom);

    
    // Howmany cities?
    int howmany;
    howmany=Config.size();
    
    // first draw all connectors
    for (int i=0;i<howmany-1;i++)
    {
      int a=((Integer)Config.elementAt(i)).intValue();
      int b=((Integer)Config.elementAt(i+1)).intValue();
      DrawCityConnector(OffGraphics,TspInstance.MyCities.GetCityAt(a),TspInstance.MyCities.GetCityAt(b));
    }
    
    // close the tour - make a circle...
    int a=((Integer)Config.elementAt(howmany-1)).intValue();
    int b=((Integer)Config.elementAt(0)).intValue();
    DrawCityConnector(OffGraphics,TspInstance.MyCities.GetCityAt(a),TspInstance.MyCities.GetCityAt(b));
    
    // ok than all cities plz
    DrawAllCities(OffGraphics);
    
  }  
  
  // calculates a edgecolor
  // and return the "heat"-color
  private Color CalcColor(double d, double max, double min)
  {
    min = 0;
    double InitialTrailIntensity=0.1;
    double DifferenceMinMax= max-min;
//    double StepDifference=DifferenceMinMax/6;

    d -= min;
    
    Color c; 
         if( d >= DifferenceMinMax*0.64 ) c = new Color(   0,   0, 200 );
    else if( d >= DifferenceMinMax*0.32 ) c = new Color(  40,  40, 200 );
    else if( d >= DifferenceMinMax*0.16 ) c = new Color(  80,  80, 200 );
    else if( d >= DifferenceMinMax*0.08 ) c = new Color( 120, 120, 200 );
    else if( d >= DifferenceMinMax*0.04 ) c = new Color( 160, 160, 200 );
    else if( d >= DifferenceMinMax*0.02 ) c = new Color( 200, 200, 200 );
    else c = BGColor;
    

    return c;
    
    /*
    if (d<=(min+(0.5*StepDifference)))
    {
      return BGColor;
    }
    else if (d<=(min+(2*StepDifference)))
    {
      return Color.darkGray;
    }
    else if (d<=(min+(3*StepDifference)))
    {
      return Color.green;
    }
    else if (d<=(min+(4*StepDifference)))
    {
      return Color.yellow;
    }
    else if (d<=(min+(5*StepDifference)))
    {
      return Color.orange;
    }
    else if (d<=(min+(6*StepDifference)))
    {
      return Color.red;
    } 
    else
    {
      return BGColor;
    } 
    */
  }  
  
  // draw the Edges and Cities, the edges in Realtion to the trail intensity
  // use a offscreenimage to avoid flickering
  public void DrawAnts(double[][] TrailIntensity, double max, double min)
  {
    // manage offscreen Image
    OffGraphics.setColor(BGColor);
    OffGraphics.fillRect(0,0,DrawRight,DrawBottom);

    // first draw all connectors
    for (int i=0;i<TrailIntensity.length;i++)
    {
      for (int j=0;j<TrailIntensity.length;j++)
      {
        Color c = CalcColor(TrailIntensity[i][j],max,min);
        if (c!=BGColor)
        {
          DrawCityConnector(OffGraphics,c,TspInstance.MyCities.GetCityAt(i),TspInstance.MyCities.GetCityAt(j));
        }  
      }  
    }  
    
    // all cities
    DrawAllCities(OffGraphics);
    // draw offscreen
   
  }  
  


	

	
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -