السلام عليكم و رحمة الله
واجهني مشكل في مشروعي
المتمثل في تكبير الصورة وتصغيرها بحيث يمنك تحريك الصورة بستخدام scroll
اتممت عملية التكبير و التصغير على image
image = new ImageIcon(getClass().getResource(pathe)).getImage();
ووجدت شرح لتكحم في الصورة وتحريكها وطبقتع على label بستخدام scroll
المشكل انه صرا هناك صورتين صورة عندا التكبير و التصغير و صورة عندا التحريك
صورة توضيحية

هاذا كود المشروع
class MainPanel extends JPanel {
private static Image image;
private JPanel Panel;
private double zomming = 1.0; // sc zooming
JLabel label = new JLabel();
//method cone
public MainPanel(String pathe) {
super(new BorderLayout());
image = new ImageIcon(getClass().getResource(pathe)).getImage();
label.setIcon(new ImageIcon(getClass().getResource(pathe)));
JScrollPane scroll = new JScrollPane(label,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
HandLcalss hand = new HandLcalss(label);
JViewport v = scroll.getViewport();
v.addMouseListener(hand);
v.addMouseMotionListener(hand);
v.addMouseWheelListener(hand);
add(scroll);
scroll.setPreferredSize(new Dimension(800, 600));
}
public static void ShowGUI(String pathe) {
JFrame frame = new JFrame("AutoScroll");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel(pathe));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class HandLcalss implements MouseListener,MouseMotionListener,MouseWheelListener{
private static final int SPEED = 4;
private static final int DELAY = 10;
private final Cursor dc;
private Cursor hc = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
private Timer scroller;
private JComponent label;
private Point startPt = new Point();
private Point move = new Point();
public HandLcalss(JComponent comp) {
this.label = comp;
this.dc = comp.getCursor();
this.scroller = new Timer(DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
@Override public void mouseDragged(MouseEvent e) {
JViewport vport = (JViewport)e.getSource();
Point pt = e.getPoint();
int dx = startPt.x - pt.x;
int dy = startPt.y - pt.y;
Point vp = vport.getViewPosition();
vp.translate(dx, dy);
label.scrollRectToVisible(new Rectangle(vp, vport.getSize()));
move.setLocation(SPEED*dx, SPEED*dy);
startPt.setLocation(pt);
}
@Override
public void mousePressed(MouseEvent e) {
((JComponent)e.getSource()).setCursor(hc);
startPt.setLocation(e.getPoint());
move.setLocation(0, 0);
scroller.stop();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
switch(e.getModifiers()){
case InputEvent.BUTTON1_MASK:{
if(e.getClickCount() == 2) {
zoomIn();
}
break;
}
}
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
int notches = e.getWheelRotation();
if (notches < 0) {
zoomIn();
} else {
zoomOut();
}
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
@Override
public void paint(Graphics g){
System.out.println("zomming"+zomming);
Graphics2D g2=(Graphics2D)g;
super.paintComponents(g2);
g2.scale(zomming/3, zomming/3);
if (zomming != 1) {
AffineTransform transformationZoom = AffineTransform.getScaleInstance(zomming, zomming);
g2.drawImage(image, transformationZoom, this);
} else {
g2.drawImage(image, -10, -10, this); //////
}
}
public void zoomIn(){
zomming=zomming+.1;
repaint(); // new draw
}
public void zoomOut(){
if(zomming>1){
zomming=zomming-.1;
}
repaint();
}
public static void ImageSize() {
int imgWidth = image.getWidth(null);
int imgHeight = image.getHeight(null);
System.out.println("imgHeight: "+imgHeight);
System.out.println("imgWidth: "+imgWidth);
}
}