Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8310275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:24:06+00:00 2026-06-08T19:24:06+00:00

I am trying to create a panel with pictures that are changed with the

  • 0

I am trying to create a panel with pictures that are changed with the mouse.
I have 10 images (0.png to 9.png). My problem is, I want to move image and I am currently viewing the second picture. I used the JScrollPane to scroll back to the first image, but only the first image is moving. How can I refresh my panel and be able to move all images?

This is my code:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.*;

class MapFrame extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = (long) 1.0;
    private static Image image;
    private static JLabel label = new JLabel();
    private static int ind =0;
    private static JFrame frame;
    private static String str;
   //----------------------------------------------------------------- 
    public MapFrame(){      }

   //----------------------------------------------------------------- 

    public MapFrame(String pathe) {
        super(new BorderLayout());
          image = new ImageIcon(getClass().getResource(pathe)).getImage();
          label = new JLabel(new ImageIcon(image));  
          JScrollPane scroll = new JScrollPane(label,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          add(scroll);

          HandLcalss hand = new HandLcalss(label);
          JViewport v = scroll.getViewport();
          v.addMouseListener(hand);
          v.addMouseMotionListener(hand);
          v.addMouseWheelListener(hand);
    }

   //----------------------------------------------------------------- 


    public static void ShowGUI(String pathe) {
        frame = new JFrame("Bladi_map");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MapFrame(pathe));
        frame.setSize(500,400);  
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

   //----------------------------------------------------------------- 

    public static void ChangImage(String pathe){
        frame.getContentPane().add(new MapFrame(pathe));        
        label.revalidate();
        frame.setVisible(true);     
    }

    //----------------------------------------------------------------- 
class HandLcalss implements MouseListener,MouseMotionListener,MouseWheelListener{
    private static final int DELAY = 10;
    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;
        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()));
        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 mouseWheelMoved(MouseWheelEvent e) {
        int notches = e.getWheelRotation();

        if (notches < 0) {
            ind++;
            if(ind <0){ind=0;}
            if(ind<=9){
            str="/resources/"+ind+".png";       
             ChangImage(str);
            System.out.println("indink"+str);
            }
        } else {
            ind--;
            if(ind >9){ind=8;}
            if(ind>=0){
            str="/resources/"+ind+".png";
            ChangImage(str);
            System.out.println("ind"+ind);
            }             
        }   
      }
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub          
    }

    @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      
    }
}

//----------------------------------------------------------------- 
}

This is the main class:

public class main { 
    public static void main(String[] args) {
        MapFrame.ShowGUI("/resources/0.png");       
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:24:10+00:00Added an answer on June 8, 2026 at 7:24 pm

    You have 2 problems in your code:

    The first:

    public static void ChangImage(String pathe){
        frame.getContentPane().add(new MapFrame(pathe));        
        label.revalidate();
        frame.setVisible(true);     
    } 
    

    When you change image (invoked on mouse wheel event) you simply add new panel (MapFrame) with image to existing container: (Container pane = frame.getContentPane()). The old ones are still there and are overlapping with the new panel. The above method should be:

    public static void ChangImage(String pathe) {
        frame.getContentPane().removeAll();
        frame.getContentPane().add(new MapFrame(pathe));
        label.revalidate();
        frame.setVisible(true);
    }
    

    The second thing:

    Why complicate? (also there is an error on 0 value of ind):

    @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            int notches = e.getWheelRotation() * -1;
            ind += notches;
            if(ind < 0) {
                ind = 0;
            }
            else if(ind > 9) {
                ind = 9;
            }
            str = "/resources/" + ind + ".png";
            ChangImage(str);
        }
    

    And the last thing:

    If you are implementing all mouse interfaces – you can extend MouseAdapter. Now you needn’t override methods you don’t want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to create a menu panel with jQuery that can be
I have a WPF application where I'm trying to create a diagnostics panel that's
What I'm trying to create is web panel that displays console output. The problem
I'm trying to create a top panel menu that contains 5 buttons. I configured
I'm trying to create a view with a panel at the top that contains
I'm trying to create an ascx control that can wrap content like a panel.
I am trying to create a Swing panel whose elements have a different font
I'm trying to create a skin for a Flex 4 spark Panel that will
I am trying to create an image of a Panel and save it to
I'm trying to create a horizontally oriented stack panel that contains a vertically oriented

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.