I have two classes. The first, JPanelImage
, adds an Image
to my JPanel
. The second, myObjet
, represents the object I want to add on my Image
. The Image
can move and can zoom.
The problem is that when I move the image, the object remains fixed.
Class JImagePanel
:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
class JImagePanel extends Panel{
private static final long serialVersionUID = 5375994938523354306L;
private MediaTracker tracker;
private Image img;
private Dimension imgSize,iniSize;
private int zoom = 0 ;
private int MouseX;
private int MouseY;
int transX=0;
int transY=0;
public JImagePanel(String file){
//setSize(100,200);
img=Toolkit.getDefaultToolkit().getImage(file);
setLayout(null);
tracker=new MediaTracker(this);
tracker.addImage(img,0);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
MouseX = e.getX();
MouseY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
transX += e.getX()-MouseX;
transY += e.getY()-MouseY;
MouseX = e.getX();
MouseY = e.getY();
repaint();
}
});
try{
tracker.waitForAll();
}
catch(Exception ie){}
imgSize=iniSize=new Dimension(img.getWidth(this),img.getHeight(this));
}
public Dimension getPreferredSize(){
return new Dimension(imgSize);
}
public void paint(Graphics g){
super.paint(g);
if(imgSize.width<=iniSize.width) {
imgSize=iniSize;
}
g.drawImage(this.img, (getWidth()-imgSize.width)/2+transX, (getHeight()-imgSize.height)/2+transY, imgSize.width,imgSize.height,this);
}
public void zoomIn(){
int x=10*imgSize.width/100;
int y=10*imgSize.height/100;
imgSize=new Dimension(imgSize.width+x,imgSize.height+y);
if(imgSize.width>iniSize.width){
setSize(imgSize);
getParent().doLayout();
}
repaint();
}
public void zoomOut(){
int x=10*imgSize.width/100;
int y=10*imgSize.height/100;
imgSize=new Dimension(imgSize.width-x,imgSize.height-y);
if(getWidth()>iniSize.width)
{
setSize(imgSize);
getParent().doLayout();
}
repaint();
}
public int getZoom() {
return zoom;
}
Class myObjet
:
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class myObjet extends JPanel {
JLabel lblName,lblAct,lblSeuil ;
JPanel panelObjet;
public myObjet(String NameObjet ,double activite )
{
ImageIcon img = createImageIcon("images/Source.png");
lblName = new JLabel(img);
lblAct = new JLabel(String.valueOf(activite));
panelObjet = new JPanel();
panelObjet.setToolTipText(NameObjet);
panelObjet.setLayout(new BorderLayout());
panelObjet.add("North",lblName);
panelObjet.add("South",lblAct);
add(panelObjet);
isOpaque();
}
public ImageIcon createImageIcon(String path) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
JOptionPane.showMessageDialog(null, "Cette image n'existe pas : " + path, "Erreur", JOptionPane.ERROR_MESSAGE);
// System.err.println("L'image n'est pas dans : " + path);
return null;
}
}
public boolean isOpaque()
{
return true ;
}
}
MYObject is alone , i adds this Object in Panel of this Image .
Here is a concrete example of how I use these classes
public static void (String [] args )
{
imagePanel = new JImagePanel("/home/Image.png");
p = new JPanel();
p.setLayout(new FlowLayout());
// p.setBounds(0,0,0,0);
p.add(getImagePanel());
ple2.add("Center",p);
}
2/ in actionPerformed :
public void actionPerformed(ActionEvent ev) {
Object sourceEv = ev.getSource() ;
if(sourceEv == action.jpfI.btnFrame[4])
{
df = new DecimalFormat("0.00");
int x = Integer.valueOf(action.jpfI.lblTxt[4].getText());
int y =Integer.valueOf(action.jpfI.lblTxt[5].getText()) ;
x =(int)(x/0.26) ;
y =(int)(y/0.26):
objet = new myObjet("islem","0.002");
objet.setBounds(x,y , 50,50);
action.getImagePanel().addImage(objet);
action.repaint();
}