Виктор Кон, vkBook, Java-Second
Класс butImage
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class butImage extends JDialog implements ActionListener, MouseMotionListener {
public static String ps;
public static int mod,x1,y1,x2,y2;
int kx,ky,nx,ny,ifs,m;
Container pcont; JButton mb; JTextArea ta;
public butImage(Frame own, String tit, ImageIcon img, boolean ok){
super(own); setTitle(tit); setResizable(false); setModal(ok);
m=0; x1=0; y1=0; x2=0; y2=0;
ifs=3; nx=img.getIconWidth(); ny=img.getIconHeight();
mb = new JButton(img); mb.setMargin(new Insets(0,0,0,0));
mb.setActionCommand("but"); mb.addActionListener(this);
mb.addMouseMotionListener(this); JScrollPane scrP = new JScrollPane(mb);
ta = new JTextArea("(0,0)"); ta.setEditable(false);
pcont = getContentPane();
pcont.add(Box.createVerticalStrut(ifs), BorderLayout.NORTH);
pcont.add(Box.createHorizontalStrut(ifs), BorderLayout.WEST);
pcont.add(scrP, BorderLayout.CENTER);
pcont.add(Box.createHorizontalStrut(ifs), BorderLayout.EAST);
pcont.add(ta, BorderLayout.SOUTH);
Dimension scrS = Toolkit.getDefaultToolkit().getScreenSize();
if( nx < scrS.width && ny < scrS.height-60 ){pack();}else{setSize(scrS.width,scrS.height);}
setLocationRelativeTo(null);
setDefaultCloseOperation(2); this.show();
}
public void actionPerformed(ActionEvent e) {
if("but".equals(e.getActionCommand())){
mod=e.getModifiers(); ps=e.paramString(); this.setVisible(false); this.dispose();
}}
public void mouseMoved(MouseEvent e) {
kx=e.getX()-2; if(kx < 1){kx=1;} if(kx > nx){kx=nx;}
ky=ny-e.getY()+3; if(ky < 1){ky=1;} if(ky > ny){ky=ny;}
ta.setText(" (" + kx + "," + ky + ") of [" + nx + "," + ny + "]");
x1=kx; y1=ky;
}
public void mouseDragged(MouseEvent e) {
kx=e.getX()-2; if(kx < 1){kx=1;} if(kx > nx){kx=nx;}
ky=ny-e.getY()+3; if(ky < 1){ky=1;} if(ky > ny){ky=ny;}
ta.setText(" (" + kx + "," + ky + ") of [" + nx + "," + ny + "]");
if(m == 0){m=1; x1=kx; y1=ky;}else{x2=kx; y2=ky;}
}
}