?? dituimage.java
字號:
package com.ljz.gongJiaoSearch;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.io.Serializable;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.event.*;
public class DiTuImage extends JPanel implements Serializable, MouseListener, MouseWheelListener, MouseMotionListener,ActionListener{
private Image img;
private String imgName;
private int x=-200,y,w=1033,h=732;
private MyFace myface;
private JPopupMenu pop;
JMenuItem mXiuGai,mAdd,mDel;
//chu shi hua you jian cai dan
public void youJianCSH(){
mDel=new JMenuItem("刪除標記(L)",new ImageIcon("Icon/del.jpg"));
mDel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
mDel.setMnemonic('d');//設置alt鍵
mDel.addActionListener(this);
mAdd=new JMenuItem("添加標記(T)");
mAdd.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,InputEvent.CTRL_MASK));
mAdd.setMnemonic('t');//設置alt鍵
mAdd.addActionListener(this);
mXiuGai=new JMenuItem("修改標記(X)");
mXiuGai.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
mXiuGai.setMnemonic('x');//設置alt鍵
mXiuGai.addActionListener(this);
pop=new JPopupMenu();
pop.add(mAdd);
// pop.add(mXiuGai);
pop.addSeparator();
pop.add(mDel);
}
public DiTuImage(String name,MyFace m){
this.addBiaoQian();
this.myface=m;
this.setLayout(null);
this.setImgName(name);
this.addMouseListener(this);
this.addMouseWheelListener(this);
this.addMouseMotionListener(this);
//chu shi hua you jian
youJianCSH();
}
public static Image creatImage(String imgName,Object obj){
URL url=obj.getClass().getResource(imgName);
return new ImageIcon(url).getImage();
}
int bz=0;
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(bz<1){
bz++;
w=1033;h=732;
}
if(img!=null)
g.drawImage(img,x,y,w,h,this);
//g.drawImage(img,0,0,this.getWidth(),this.getHeight(),this);
}
public void setImgName(String s){
this.imgName=s;
img=this.creatImage(s,this);
w=this.getWidth();
h=this.getHeight();
repaint();
}
//采用循環方法
ArrayList list=new ArrayList();
//加入站點標簽
public void addBiaoQian(){
//獲取所有站點信息
list=new ArrayList();
this.removeAll();
DataBaseManager dbm=new DataBaseManager();
String strSQL="select * from zhandianxinxi";
ResultSet rs=dbm.getResult(strSQL);
try{
if(!rs.first())
return;
//rs.first();
while(!rs.isAfterLast()){
debug("you");
double x=rs.getInt(2);
double y=rs.getInt(3);
//檢查類型
String se=rs.getString(4);
if(!se.equals("公交站點"))
continue;
String s=rs.getString(1);
//if(w>=1756)
// return;
double bl=1756.0/w;
debug(""+bl);
x/=bl;
y/=bl;
int x1=(int)x;
int y1=(int)y;
x1+=this.x;
y1+=this.y;
if(this.isAdd(x1,y1)){
JLabel l=new JLabel(s);
this.add(l);
list.add(l);
int f=s.length()*13;
//l.setBounds(x1-f/2,y1-5,f,15);
l.setBounds(x1-10,y1-5,f,15);
l.setVisible(true);
l.setToolTipText(s);
l.addMouseListener(this);
if(aa!=null && aa.indexOf(s)!=-1)
l.setForeground(Color.red);
else
l.setForeground(Color.blue);
l.setOpaque(true);
l.setBackground(Color.white);
}
rs.next();
}
dbm.closeConnection();
}catch(Exception e){
e.printStackTrace();
}
}
public void debug(String s){
System.out.println(s);
}
//kan kan shi fou ke yi jia jin qu
private final int J=50;
public boolean isAdd(int x,int y){
if(x<0|| y<0 || x<this.x||y<this.y||x+10>this.w+this.x||y>this.h+this.y)
return false;
for(int i=0;i<list.size();i++){
int x1=((JLabel)list.get(i)).getX(),y1=((JLabel)list.get(i)).getY(),x3=0;
x3=(int)Math.sqrt(Math.pow(x1-x,2)+Math.pow(y1-y,2));
if(x3<J)
return false;
}
return true;
}
public void setLabelForColor(Color c){
for(int i=0;i<list.size();i++){
((JLabel)list.get(i)).setForeground(c);
aa=null;
}
}
private boolean shujuluru=false;
//shu ju lu ru
public void shuJuLuRu(){
shujuluru=true;
x=-100;
y=-100;
h=1244;
w=1756;
repaint();
addBiaoQian();
}
//鼠標事件開始
int k1,k2;
String aa;
public void mouseClicked(MouseEvent e) {
if(e.getSource() instanceof JLabel && e.getButton()==1 && e.getClickCount()==1){
JLabel b=(JLabel)e.getSource();
if(myface.setQiShiDian(b.getText())){
b.setForeground(Color.red);
aa+=b.getText();
}else{
myface.setChongXinKaiShi();
myface.setQiShiDian(b.getText());
b.setForeground(Color.red);
aa+=b.getText();
}
}else if(e.getButton()==3 && e.getClickCount()==1 && e.getSource()==this && shujuluru){
debug("you ji wo le");
pop.show(this,e.getX(),e.getY());
mAdd.setEnabled(true);
mXiuGai.setEnabled(false);
mDel.setEnabled(false);
}else if(e.getButton()==3 && e.getClickCount()==1 && e.getSource() instanceof JLabel && shujuluru){
JLabel b=(JLabel)e.getSource();
pop.show(b,e.getX(),e.getY());
mAdd.setEnabled(false);
mXiuGai.setEnabled(true);
mDel.setEnabled(true);
}
//記錄數據 準備存儲
if(shujuluru){
if(e.getSource() instanceof JLabel)
jdLabel=(JLabel)e.getSource();
int f1=e.getX()-this.x;
int f2=e.getY()-this.y;
double d=1756.0/this.w;
f1=(int)(f1*d);
f2=(int)(f2*d);
this.p=new Point(f1,f2);
}
}
public void mousePressed(MouseEvent e) {
if(e.getButton()==1){
k1=e.getX();
k2=e.getY();
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
int x1=e.getX();
int y1=e.getY();
x1-=k1;
y1-=k2;
final int bz=50;
//if(x+x1<bz && y+y1<bz && ){
x+=x1;
y+=y1;
//}
repaint();
k1=e.getX();
k2=e.getY();
addBiaoQian();
}
//滾輪鼠標事件
public void mouseWheelMoved(MouseWheelEvent e) {
//if(!shujuluru){
int a=e.getWheelRotation();
if(w<300 && a<0)
return;
final int B=40;
int c=a*B/2,d=B*a;
x-=c;w+=d;
//if(w>=1700 && a>0)
// return;
int h1=h;
h=1244*w/1756;
y-=(h-h1)/2;
repaint();
addBiaoQian();
//}
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
//添加 刪除 修改 數據
private JLabel jdLabel;
private Point p;
public void actionPerformed(ActionEvent e) {
if(e.getSource()==mAdd){
BiaoJiSheZhi bj=new BiaoJiSheZhi(p,this,myface);
}else if(e.getSource()==mDel){
DataBaseManager dbm=new DataBaseManager();
String strSQL="delete * from zhandianxinxi where name='"+jdLabel.getText()+"'";
dbm.updateSql(strSQL);
dbm.closeConnection();
addBiaoQian();
}else if(e.getSource()==mXiuGai){
}
}
public boolean isShujuluru() {
return shujuluru;
}
public void setShujuluru(boolean shujuluru) {
this.shujuluru = shujuluru;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -