View Javadoc

1   //--------------------------------------
2   //
3   // DotPlot.java
4   // Since: 2010/10/28
5   //
6   //--------------------------------------
7   package org.utgenome.gwt.utgb.server.app;
8   
9   //--------------------------------------
10  //
11  // DotPlot.java
12  // Since: 2010/10/26
13  //
14  //--------------------------------------
15  
16  import java.awt.Color;
17  import java.awt.Graphics2D;
18  import java.awt.geom.Line2D;
19  import java.awt.image.BufferedImage;
20  import java.io.BufferedReader;
21  import java.io.File;
22  import java.io.FileReader;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  
26  import javax.imageio.ImageIO;
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.utgenome.gwt.utgb.server.WebTrackBase;
32  
33  
34  /**
35   * Web action: DotPlot
36   *
37   */
38  public class DotPlot extends WebTrackBase
39  {
40      private static final long	serialVersionUID	= 1L;
41      private static ArrayList<String> list = new ArrayList<String>();
42      private static int counter =0;
43  
44      public DotPlot()
45      {}
46      	
47      public void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
48      {
49  	    // Read text file
50      	try
51          {
52      	  File file = new File("c:/test/test.txt");
53            String line = null;
54            BufferedReader br = new BufferedReader(
55                    new FileReader(file));
56            while ((line = br.readLine()) != null)
57              {
58          	  String[] data = line.split("\t");
59          	  list.add(data[0]);
60          	  list.add(data[1]);
61          	  list.add(data[2]);
62          	  list.add(data[3]);
63                counter++;
64              }
65            br.close();
66          
67          }
68        catch (Exception e)
69          {
70            e.printStackTrace();
71          }
72        
73  	    // set the content type to be generated to PNG image
74  	    response.setContentType("image/png");
75  
76  	    // prepare a buffer for drawing grpahics  
77  	    BufferedImage buffer = new BufferedImage(2000, 2000, BufferedImage.TYPE_INT_ARGB);
78  	    Graphics2D g = (Graphics2D) buffer.getGraphics();
79  
80  		// draw lines
81  		for (int i=0; i < counter; i++) {
82  			float x1 = Float.parseFloat((String) list.get(4*i));
83  			float y1 = Float.parseFloat((String) list.get(4*i+1));
84  			float x2 = Float.parseFloat((String) list.get(4*i+2));
85  			float y2 = Float.parseFloat((String) list.get(4*i+3));
86  			
87  			Color z = new Color(25*i%255, 85*i%255, 200*i%255);  
88  		    g.setColor(z);
89  			g.draw(new Line2D.Float(x1/100, y1/100, x2/100, y2/100));
90  		// output the buffered image as PNG image 
91  
92  		}
93  	ImageIO.write(buffer, "png", response.getOutputStream());
94      }
95      
96  }
97