Класс butImage
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class butImage extends JDialog implements ActionListener, MouseMotionListener, KeyListener {
public static String ps;
public static int key,mod,x1,y1,x2,y2,emp,wx,wy,cr,cg,cb;
int kx,ky,nx,ny,ifs,ifm,m,ww,wh,ic;
Container pcont; JButton mb; Image im; JTextArea ta; BufferedImage bi; Color col; Graphics gg;
public butImage(Frame own, String tit, ImageIcon img, boolean ok){
super(own); setTitle(tit); setResizable(false); setModal(ok);
m=0; mod=0; key=0; x1=0; y1=0; x2=0; y2=0; nx=img.getIconWidth(); ny=img.getIconHeight();
im=img.getImage(); bi = new BufferedImage(nx,ny,BufferedImage.TYPE_INT_RGB);
gg = bi.getGraphics(); gg.drawImage(im,0,0,null); ifs=0; ifm=2;
if(nx < 120){ifs=(120-nx)/2;}
mb = new JButton(img); mb.setMargin(new Insets(0,0,0,0));
mb.setActionCommand("but"); mb.addActionListener(this);
mb.addMouseMotionListener(this); mb.addKeyListener(this); JScrollPane scrP = new JScrollPane(mb);
ta = new JTextArea(" (0,0) "); ta.setEditable(false);
pcont = getContentPane();
pcont.add(Box.createVerticalStrut(0), 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();
ww=nx; if(ww > scrS.width){ww=scrS.width;}
wh=ny; if(wh > scrS.height-70){wh=scrS.height-60;}
if( ww == nx && wh == ny){pack();}else{setSize(ww,wh+30);}
switch(emp){
case 0: setLocationRelativeTo(null); break;
case 1: setLocation(wx,wy); break;
} setDefaultCloseOperation(2); this.setVisible(true);
}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){key=e.getKeyCode();
if(key != 16 && key != 17 && key != 18){this.setVisible(false); this.dispose();}}
public void keyReleased(KeyEvent e){}
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()-ifm; if(kx < 1){kx=1;} if(kx > nx){kx=nx;}
ky=ny-e.getY()+ifm; if(ky < 1){ky=1;} if(ky > ny){ky=ny;}
ic=bi.getRGB(kx-1,ny-ky); col = new Color(ic); cr=col.getRed(); cg=col.getGreen(); cb=col.getBlue();
ta.setText(" ["+nx+","+ny+"] x,y = "+kx+","+ky+" RGB = "+cr+","+cg+","+cb);
x1=kx; y1=ky;
}
public void mouseDragged(MouseEvent e) {
kx=e.getX()-ifm; if(kx < 1){kx=1;} if(kx > nx){kx=nx;}
ky=ny-e.getY()+ifm; if(ky < 1){ky=1;} if(ky > ny){ky=ny;}
ta.setText(" ["+nx+","+ny+"] x,y = "+kx+","+ky);
if(m == 0){m=1; x1=kx; y1=ky;}else{x2=kx; y2=ky; drrect();}
}
public void drrect(){
int xf,yf,xs,ys;
xf=Math.min(x1,x2)+ifm; xs=Math.max(x1,x2)+ifm-xf; yf=ny-Math.max(y1,y2)+ifm; ys=ny-Math.min(y1,y2)+ifm-yf;
Graphics g = mb.getGraphics(); g.drawImage(im,ifm,ifm,null); g.drawRect(xf,yf,xs,ys);
}
}