Results 1 to 3 of 3
  1. #1
    shawleigh17 is offline Member
    Join Date
    Jul 2009
    Posts
    9
    Rep Power
    0

    Default Way to group graphics together?

    Is there a way to group graphics into one graphic? I have a rectangle with a string inside, and I want to use it for a drag and drop function, but right now it is only moving the rectangle, and the string stays put.

    Also, can i make the string editable? I want the use to be able to click on it and change the text.

    Any help is appreciated.

    Thanks,
    Shawna

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Question one:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    
    public class GraphicDragAndDrop extends JPanel {
        Rectangle rect = new Rectangle(100,100,150,75);
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.blue);
            g2.draw(rect);
            String s = "hello world";
            Font font = g2.getFont().deriveFont(24f);
            g2.setFont(font);
            FontRenderContext frc = g2.getFontRenderContext();
            // Some prefer to use TextLayout for this:
            float width = (float)font.getStringBounds(s, frc).getWidth();
            LineMetrics lm = font.getLineMetrics(s, frc);
            float height = lm.getAscent() + lm.getDescent();
            float x = rect.x + (rect.width - width)/2;
            float y = rect.y + (rect.height - height)/2 + lm.getAscent();
            g2.setPaint(Color.red);
            g2.drawString(s, x, y);
        }
    
        public void setRect(int x, int y) {
            rect.setLocation(x, y);
            repaint();
        }
    
        public static void main(String[] args) {
            GraphicDragAndDrop test = new GraphicDragAndDrop();
            new GraphicDragController(test);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    }
    
    class GraphicDragController extends MouseInputAdapter {
        GraphicDragAndDrop component;
        Point offset = new Point();
        boolean dragging = false;
    
        public GraphicDragController(GraphicDragAndDrop gdad) {
            component = gdad;
            component.addMouseListener(this);
            component.addMouseMotionListener(this);
        }
    
        public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
            Rectangle r = component.rect;
            if(r.contains(p)) {
                offset.x = p.x - r.x;
                offset.y = p.y - r.y;
                dragging = true;
            }
        }
    
        public void mouseReleased(MouseEvent e) {
            dragging = false;
        }
    
        public void mouseDragged(MouseEvent e) {
            if(dragging) {
                int x = e.getX() - offset.x;
                int y = e.getY() - offset.y;
                component.setRect(x, y);
            }
        }
    }

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    can i make the string editable? I want the use to be able to click on it and change the text
    One possibility is to add a pre–configured JTextField to the panel at the location of the rect or text and have the user press the enter key to save the edit. Add an ActionListener to the textField (pre–configure) which will receive the "enter" key event. In the listener remove the editor, save the text and draw ot on the panel.
    You'll have to figure out a mouse gesture to trigger the edit. Maybe a right–click. SwingUtilities has ways to detect mouse buttons.

Similar Threads

  1. First Group Project!!!!!!!!
    By Zosden in forum Advanced Java
    Replies: 28
    Last Post: 11-02-2010, 09:30 AM
  2. Group Demonstration
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:47 PM
  3. Group Projects
    By Zosden in forum Advanced Java
    Replies: 11
    Last Post: 06-03-2008, 03:47 AM
  4. New To The Group
    By javarishi in forum Introductions
    Replies: 5
    Last Post: 04-10-2008, 04:34 AM

Posting Permissions

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