项目要用jsp作网页,里面要用鼠标选定区域动态生成图。
现在用的是java编一个bean来实现的。对java也是不怎么懂,边摸边画。
不知道,下面这个可不可行。请各位大虾帮我看看哦。
package mouse;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.image.*;
import java.applet.*;
/*为了达到画线的功能,我们分别implements MouseListener与MouseMotionListener.
*/
public class mouseBean extends JFrame implements MouseListener,MouseMotionListener{
String str;
BufferedImage image;
Image rect;
int flag;
int x=0;
int y=0;
int startx,starty,endx,endy;//起始坐标与终点坐标
int imgOrig = 50000000;
int zoomLength = imgOrig;
int imgStart = 0;
int imgEnd = imgStart + zoomLength;
public mouseBean(){
Container contentPane=getContentPane();
contentPane.addMouseListener(this);
contentPane.addMouseMotionListener(this);
setSize(1000,1000);
setVisible(true);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
public void drawCoordinate(Graphics g) //draw coordinate
{
int interval = zoomLength>>2;
for(int i=imgStart; i<imgEnd; i+=interval)
{
g.drawLine(((int)(i-imgStart)/zoomLength*800),130,((int)(i-imgStart)/zoomLength*800),135);
if(i>1000000)
{
int j=i/1000000;
g.drawString(String.valueOf(j)+"M",((int)(i-imgStart)/zoomLength*800),133);
}
else if((i>1000)&&(i<1000000))
{
int j=i/1000;
g.drawString(String.valueOf(j)+"K",((int)(i-imgStart)/zoomLength*800),133);
}
else
g.drawString(String.valueOf(i),((int)(i-imgStart)/zoomLength*800),133);
}
}
/*由mousePressed(),mouseReleased()取得示拖曳的开始与结束坐标*/
public void mousePressed(MouseEvent e){
startx=e.getX();
starty=e.getY();
}
public void mouseReleased(MouseEvent e){
endx=e.getX();
endy=e.getY();
Container c=(Container)e.getSource();
Graphics g=c.getGraphics();
//Graphics g = null;
int zoom;
int zoomTemp;
if(endx > startx){
zoomTemp = (endx-startx)/800*zoomLength;
imgStart = imgStart + ((startx - /*imgleftx*/100)/800*zoomLength);
//imgEnd = imgStart + zoomLength;
zoomLength = zoomTemp;
zoom = imgOrig/zoomLength;
if(zoom <= 30){
flag = 1;
paint(g);
}
if((zoom>30)&&(zoom<=900)){
flag = 2;
paint(g);
}
if((zoom>900)&&(zoom<=27000)){
flag = 3;
paint(g);
}
if((zoom>27000)&&(zoom<=810000)){
flag = 4;
paint(g);
}
}
else{
int exchange = startx;
startx = endx;
endx = exchange;
zoomTemp = 800/(endx-startx)*zoomLength;
zoomLength = zoomTemp;
zoom = imgOrig/zoomLength;
if(zoomTemp>imgOrig)
{
zoomLength = imgOrig;
imgStart = 0;
imgEnd = imgStart + zoomLength;
}
if(zoom<=30){
flag = 1;
paint(g);
}
if((zoom>30)&&(zoom<=900)){
flag = 2;
paint(g);
}
if((zoom>900)&&(zoom<=27000)){
flag = 3;
paint(g);
}
if((zoom>27000)&&(zoom<=810000)){
flag = 4;
paint(g);
}
imgStart = imgStart + (zoomLength>>1) - (int)(zoomTemp%2.0); //zoom out in center
}
}
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void mouseClicked(MouseEvent e){ }
/*mouseMoved(),mouseDragged()取得鼠标移动的每一个坐标,并调用repaint()方法*/
public void mouseMoved(MouseEvent e){
//flag=1;
x=e.getX();
y=e.getY();
repaint();
}
public void mouseDragged(MouseEvent e){
//flag=2;
//x=e.getX();
//y=e.getY();
//repaint();
}
public void update(Graphics g){
g.setColor(this.getBackground());
g.fillRect(100,100,/*getWidth()*/800,/*getHeight()*/500);
paint(g);
}
public void paint(Graphics g){
int geneWidth;
int geneNum;
int queryNum;
String sbuf;
if(flag==1)
{
g.setColor(Color.blue);
g.fillRect(100,150,/*getWidth*/800,/*getHeight*/500);
geneNum = findGenes(imgStart,imgEnd).length; //find genes from whole_genes.txt and no mark symbol
gene[] gn = new gene[geneNum];
for(int i=0; i<geneNum; i++)
{
geneWidth = gn[i].txEnd - gn[i].txStart;
g.fillRect((int)(800/zoomLength*(gn[i].txStart-imgStart)),150,((int
geneWidth/zoomLength*800),20); //show every gene in this region
}
}
if(flag == 2)
{
g.setColor(Color.blue);
g.drawLine(100,150,/*getWidth*/800,150);
geneNum = findGenes(imgStart,imgEnd).length;
gene[] gn = new gene[geneNum];
for(int i=0; i<geneNum; i++)
{
geneWidth = gn[i].txEnd - gn[i].txStart;
g.fillRect((int)(/*getWidth*/800/zoomLength*(gn[i].txStart-imgStart)),140,((int)geneWidth/zoomLength*/*getWidth*/800),20); //show genes & mark genes' name
g.drawString("gn[geneNum].symbol",(int)(/*getWidth*/800/zoomLength*(gn[i].txStart-imgStart)),135);
}
}
if(flag == 3)
{
g.setColor(Color.blue);
g.drawLine(100,150,/*getWidth*/800,150);
geneNum = findGenes(imgStart,imgEnd).length;
gene[] gn = new gene[geneNum];
for(int i=0; i<geneNum; i++)
{
geneWidth = gn[i].txEnd - gn[i].txStart;
g.fillRect((int)(800/zoomLength*(gn[i].txStart-imgStart)),140,((int)(geneWidth/zoomLength*800)),20); //show genes
g.drawString("gn[geneNum].symbol",(int)(800/zoomLength*(gn[i].txStart-imgStart)),135); //mark genes' name
for(int j=0; j<gn[geneNum].exonCount; j++) //show exons
{
int exonLen = gn[i].exonEnds[j] - gn[i].exonStarts[j];
int exonWidth = exonLen/zoomLength*800;
g.fillRect((int)(800/zoomLength*(gn[i].exonStarts[j]-imgStart)),140,exonWidth,20);
}
}
queryNum = findQueries(imgStart,imgEnd).length;
query[] qr = new query[queryNum];
for(int j=0; j<queryNum; j++) //show queries & mark queries' position
{
int qrLen = qr[j].qrEnd - qr[j].qrStart;
int qrWidth = qrLen/zoomLength*/*getWidth*/800;
g.setColor(Color.red);
g.drawLine((int)(/*getWidth*/800/zoomLength*(qr[j].qrStart-imgStart)),170,(int)(/*getWidth*/800/zoomLength*(qr[j].qrStart-imgStart))+ qrWidth,170);
}
}
if(flag == 4) //show their sequences
{
g.setColor(Color.blue);
char cbuf[] = new char[1000];
sbuf = getGeneSeq(imgStart,imgEnd);
g.drawString(sbuf,100,150);
g.setColor(Color.red);
queryNum = findQueries(imgStart,imgEnd).length;
query[] qr = new query[queryNum];
for(int i=0; i<queryNum; i++)
{
g.drawString(qr[i].seq,(int)(800/zoomLength*(qr[i].qrStart-imgStart)),170); //how to decide its position? is it right?
}
}
}
public gene[] findGenes(int x,int y)
{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\blast\\blast数据库\\genes_chr22.txt")));
String geneline = null;
String[] geneTemp = geneline.split(" ");
int Start = Integer.parseInt(geneTemp[2]);
int End = Integer.parseInt(geneTemp[3]);
gene[] gn = new gene[100];
for(int m=0; m<100; m++)
{
gn[m] = new gene();
}
int geneNum = 0;
while((geneline = br.readLine())!= null)
{
geneTemp = geneline.split(" ");
if((Start>x)&&(End<y))
{
geneNum++;
gn[geneNum].txStart = Integer.parseInt(geneTemp[2]);
gn[geneNum].txEnd = Integer.parseInt(geneTemp[3]);
gn[geneNum].symbol = geneTemp[5];
gn[geneNum].exonCount = Integer.parseInt(geneTemp[6]);
for(int i=0; i<gn[geneNum].exonCount; i++)
{
gn[geneNum].exonStarts[i] = Integer.parseInt(geneTemp[6+i*2-1]);
gn[geneNum].exonEnds[i] = Integer.parseInt(geneTemp[6+i*2]);
}
}
geneline = br.readLine();
}
return gn;
}catch(IOException e){
System.out.println(e.getStackTrace());
return null;
}
}
public query[] findQueries(int x,int y)
{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\blast\\blast数据库\\query_chr22.txt")));
String queryline = null;
String[] queryTemp = queryline.split(" ");
int Start = Integer.parseInt(queryTemp[2]);
int End = Integer.parseInt(queryTemp[3]);
query[] qr = new query[100];
for(int n=0; n<100; n++)
{
qr[n] = new query();
}
int queryNum = 0;
while((queryline = br.readLine())!= null)
{
queryTemp = queryline.split(" ");
if((Start > x)&&(End < y))
{
queryNum++;
qr[queryNum].queryIndex = Integer.parseInt(queryTemp[0]);
qr[queryNum].qrStart = Integer.parseInt(queryTemp[2]);
qr[queryNum].qrEnd = Integer.parseInt(queryTemp[3]);
qr[queryNum].seq = queryTemp[4];
qr[queryNum].varNum = Integer.parseInt(queryTemp[5]);
for(int i=0; i<qr[queryNum].varNum; i++)
{
qr[queryNum].varPos[i]= Integer.parseInt(queryTemp[6+i]);
}
}
queryline = br.readLine();
}
return qr;
}
catch(IOException e){
System.out.println(e.getStackTrace());
return null;
}
}
public String getGeneSeq(int x,int y) //read gene sequences
{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\blast\\blast数据库\\seq_"+str+".txt")));
int off = x;
int len = y - x;
char cbuf[] = new char[len];
//String sbuf = cbuf.toString();
br.read(cbuf,off,len);
String sbuf = cbuf.toString();
return sbuf;
}
catch(IOException e){
System.out.println(e.getStackTrace());
return null;
}
}
}
class gene
{
public int txStart;
public int txEnd;
public String symbol;
public int exonCount;
public int exonStarts[];
public int exonEnds[];
}
class query
{
public int queryIndex;
public int qrStart;
public int qrEnd;
public String seq;
public int varNum;
public int varPos[];
public String varInf;
}