Results 1 to 3 of 3
  1. #1
    Skye is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Help using JPanel as containers + being able to move them

    Hi all,

    I'm a 3rd year student Electronics-ICT and am developing some kind of Domotics program for a software development course.

    I'm having trouble getting my panels to work correctly. Essentially I would like a virtual representation of the real objects. The hierarchy is as follows:

    JFrame containing everything (being the container for module-containers)
    Module Containers (which are containers for...) <== are the house floors
    Location Containers (which are containers for...) <== are the house rooms
    Devices

    Basically what I would like to achieve is the following:
    One can move around a location container to other module containers (devices in the location container moves along with it). You should also be able to move devices from one location to another. And in the end also being able to move modules (which are essentially the floors of the house).

    I've been looking allover the internet for a decent tutorial on how to be able to drag images around and after some hours I finally was able to do so by loading an image into a panel. The problem is the "contains" method which checks if the mousepressed event is inside of the panel doesn't seem to work, so wherever I click and drag, my panel (displaying an image of a lamp for example) will move.

    I'm not sure if this is the correct way of doing things. My uni instructor told me to find the solution myself but I have no idea what's wrong.

    Here's the code that I've been testing with:

    The GDevice is the graphical representation of a device object (such as a lamp)

    Java Code:
    public class GDevice extends JPanel
    {
    
        private Device device;
        private BufferedImage img;
        private int x;
        private int y;
        private int width;
        private int height;
        private Point mousePoint;
    
        public GDevice(Device dev, String imgURI, int x, int y)
        {
            addMouseMotionListener(new MouseMotionHandler());
            addMouseListener(new MouseHandler());
            prepareImage(imgURI);
            setSize(img.getWidth(), img.getHeight());
            width = img.getWidth();
            height = img.getHeight();
            this.x = x;
            this.y = y;
        }
    
        private void prepareImage(String imgURI)
        {
            Image image = getToolkit().getImage(imgURI);
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(image, 1);
            try
            {
                mt.waitForAll();
            }
            catch (Exception e)
            {
                System.out.println("Exception while loading image.");
            }
            if (image.getWidth(this) == -1)
            {
                System.out.println("no gif file");
                System.exit(0);
            }
            img = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_ARGB);
            Graphics2D big = img.createGraphics();
            big.drawImage(image, 0, 0, this);
        }
    
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2D = (Graphics2D) g;
    
            g2D.drawImage(img, x, y, this);
        }
    
        public void translate(int dx, int dy)
        {
            x += dx;
            y += dy;
        }
    
        public boolean contains(Point2D p)
        {
            return x <= p.getX() && p.getX() <= x + width
                    && y <= p.getY() && p.getY() <= y + height;
        }
    
        class MouseMotionHandler extends MouseMotionAdapter
        {
    
            public void mouseDragged(MouseEvent event)
            {
                if (mousePoint == null)
                {
                    return;
                }
                Point lastMousePoint = mousePoint;
                mousePoint = event.getPoint();
                double dx = mousePoint.getX() - lastMousePoint.getX();
                double dy = mousePoint.getY() - lastMousePoint.getY();
                translate((int) dx, (int) dy);
                repaint();
    
            }
        }
    
        class MouseHandler extends MouseAdapter
        {
    
            public void mousePressed(MouseEvent event)
            {
                mousePoint = event.getPoint();
                if (!contains(mousePoint))
                {
                    mousePoint = null;
                }
            }
        }
    }
    I've just put this on a frame to test, here is that code:

    Java Code:
    public class Form
    {
    
        public static void main(String[] args)
        {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Device device = new PersistentDevice(1, 1, "Lalala", 123, true, 0, 1);
            frame.add(new GDevice(device, "resources/lamp.png", 200, 200));
            frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
            frame.setVisible(true);
        }
        private static final int FRAME_WIDTH = 400;
        private static final int FRAME_HEIGHT = 400;
    }

  2. #2
    fex
    fex is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    I advice using drag & drop (dnd) instead of mouseEvents and pixelbased movement. That's good when having a smaller project like moving a car or a house shape. But for (what seems) the size of your project, I'd use dnd :)

    Off topic: Your code looks just like (read: it's a copy of) an exemple in the 'OO Design & Patterns' book written by Cay Horstmann. You're a 3rd year EA-ICT student, following the course Software Development. I'm guessing you are a Group T student at Leuven, Belgium. I'm right ? :D
    Last edited by fex; 12-19-2010 at 03:00 PM.

  3. #3
    Skye is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Yeah I am. So you're the guy who's making the Paint-like program :p

    We should help each other out with the drag and dropping ^^

Similar Threads

  1. Using Jscrollpane to move images on jpanel
    By Lazairus in forum New To Java
    Replies: 0
    Last Post: 12-06-2010, 03:46 PM
  2. Move Jpanel to center
    By tpyq in forum NetBeans
    Replies: 1
    Last Post: 12-15-2008, 11:11 AM
  3. Replies: 5
    Last Post: 11-02-2008, 07:41 AM
  4. Swing containers
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-07-2007, 01:06 PM
  5. Replies: 2
    Last Post: 12-01-2007, 08:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •