Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Resizer image to rotate freely without effecting other panels of frame using swings

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Resizer image to rotate freely without effecting other panels of frame using swings

    In my project i want image logo and some text in header part(which is in panel2) and background image is in panel3. when i click my tools button(which is in panel3) iam getting path of logo from another frame and sets to resizer for adjustment. similarly iam getting background also, but my problem is until i click my mouse in frame my resizer logo is not appearing,when i moved the resizer logo the panel3 background image is disappearing....... here is my code follows.......
    JFrame f1;
    JPanel panel2= new JPanel();
    JPanel panel3= new MyDrawPanel1();
    JPanel area= new MyDrawPanel();
    JPanels j;
    private Resizable resizer;
    Image image;
    Image image1;
    String s;
    Color icon = null;
    Color c1=null;
    int count=0;
    String path1;
    String path;
    JPanel frameArea = new JPanel(new BorderLayout(735,500));
    JLabel label1=new JLabel();
    JLabel label2=new JLabel();
    public frame()
    {

    addMouseListener(new MouseAdapter()
    {
    public void mousePressed(MouseEvent me)
    {

    requestFocus();
    resizer.repaint();
    }
    });

    JPanel panel1= new JPanel(new BorderLayout());
    panel1.setPreferredSize(new Dimension(735,100));
    JMenuBar menuBar = new JMenuBar();
    JMenu menu1 = new JMenu("file");
    menuBar.add(menu1);
    panel1.add(menuBar,BorderLayout.NORTH);
    panel1.setBackground(Color.LIGHT_GRAY);
    JToolBar toolbar = new JToolBar();
    JButton tools = new JButton("Tools");
    toolbar.add(tools);
    tools.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event) {
    click++;

    j = new JPanels(f1);

    count=JPanels.getCount();

    if(count==1)
    {

    try
    {
    f1.remove(panel3);
    path1=JPanels.path;
    image1=new ImageIcon(""+path1).getImage();
    System.out.println("path in tools:"+path1);
    panel3();
    }
    }

    if(count==2)
    {
    path=JPanels.path1;
    image=new ImageIcon(""+path).getImage();
    /*resizer.setBounds(50, 50, 200, 150);
    image = new ImageIcon(""+path).getImage();
    resizer.add(area);
    //setLocationRelativeTo(null);
    resizer.setVisible(true);*/
    }


    }

    });
    panel1.add(toolbar,BorderLayout.SOUTH);
    panel1.setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.RAISED), new MatteBorder(5, 5, 5, 5, icon)));


    panel2.setPreferredSize(new Dimension(735,110));
    panel2.setBackground(Color.WHITE);
    System.out.println("string in panel:"+str);

    label1.setText(str);
    panel2.add(label1,BorderLayout.WEST);
    frameArea.add(panel2, BorderLayout.NORTH);

    panel3();

    JPanel panel4= new JPanel();
    panel4.setPreferredSize(new Dimension(735,50));
    panel4.setBackground(Color.pink);
    System.out.println("string in panel:"+str);
    panel4.add(label2);
    frameArea.add(panel4, BorderLayout.SOUTH);

    f1 = new JFrame("Frame in Java Swing");
    f1.setSize(new Dimension(709, 600));
    f1.setResizable(false);
    f1.setPreferredSize(new Dimension(735,660));
    f1.setVisible(true);
    area.setBackground(Color.white);
    resizer = new Resizable(area);
    resizer.setBounds(0,95, 90, 118);
    image = new ImageIcon(""+path).getImage();
    resizer.add(area);
    //setLocationRelativeTo(null);
    // resizer.setVisible(true);
    f1.add(resizer,BorderLayout.CENTER);
    f1.add(panel1, BorderLayout.NORTH);

    f1.add(frameArea);

    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void panel3()
    {
    panel3.setPreferredSize(new Dimension(735,400));
    panel3.setBackground(Color.darkGray);
    panel3.setBounds(0,110, 703, 310);
    System.out.println("entered panel3:");

    panel3.setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.RAISED), new MatteBorder(5, 5, 5, 5, icon)));
    frameArea.add(panel3, BorderLayout.CENTER);

    }

    public class MyDrawPanel1 extends JPanel
    {

    private static final long serialVersionUID = 1L;

    public void paintComponent(Graphics g){

    Dimension d = getSize();
    g.drawImage(image1, 0,0,d.width,d.height,this);
    System.out.println("entered drawpanel1....");

    }
    }

    public class MyDrawPanel extends JPanel{
    public void paintComponent(Graphics g){

    Dimension d = getSize();
    g.drawImage(image, 0,0,d.width,d.height,this);

    }

    public static void main(String[] args)
    {

    new frame();
    }

    }


    and remaining resizer related classes iam not including.. my question is that how to add a resizer image to a Jpanel without disturbing other panels in a frame, and make that resizer to rotate and move freely on all panels.when getting path of logo without mouse clicking how it will display logo in its place. please help me....to complete this task... iam waiting for reply......


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Resizer image to rotate freely without effecting other panels of frame using swin

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Resizer image to rotate freely without effecting other panels of frame using swin

    thanks for ur response......

    panel1 --> tools buttons (to get paths of images for logo and background)

    panel2 --> to place header part

    panel3 --> to place background image

    panel4 --> to place footer part.


    when iam getting path from another frame to this frame,at that time i want resizer logo to appear on my frame.

    now i want resizer to keep my logo any where in frame without effecting dimensions of frame and panels, so that i can move my resizer freely over panels.

    1more mistake iam getting when i return to this frame by getting path from another frame immediately the logo must appear, but iam not getting untill i click mouse on that resizer, its not appearing. please test my code and correct me to get the required output.


    the resizer image code goes here....


     
    <      JFrame f1;
    JPanel panel2= new JPanel();
    JPanel panel3= new MyDrawPanel1();
    JPanel area= new MyDrawPanel();
    JPanels j;
    private Resizable resizer;
    Image image;
    Image image1;
    String s;
    Color icon = null;
    Color c1=null;
    int count=0;
    String path1;
    String path;
    JPanel frameArea = new JPanel(new BorderLayout(735,500));
    JLabel label1=new JLabel();
    JLabel label2=new JLabel();
    public frame()
    {
     
    addMouseListener(new MouseAdapter()
    {
    public void mousePressed(MouseEvent me)
    {
     
    requestFocus();
    resizer.repaint();
    }
    });
     
    JPanel panel1= new JPanel(new BorderLayout());
    panel1.setPreferredSize(new Dimension(735,100));
    JMenuBar menuBar = new JMenuBar();
    JMenu menu1 = new JMenu("file");
    menuBar.add(menu1);
    panel1.add(menuBar,BorderLayout.NORTH);
    panel1.setBackground(Color.LIGHT_GRAY);
    JToolBar toolbar = new JToolBar();
    JButton tools = new JButton("Tools");
    toolbar.add(tools);
    tools.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event) {
    click++;
     
    j = new JPanels(f1);
     
    count=JPanels.getCount();
     
    if(count==1)
    {
     
    try
    {
    f1.remove(panel3);
    path1=JPanels.path;
    image1=new ImageIcon(""+path1).getImage();
    System.out.println("path in tools:"+path1);
    panel3();
    }
    }
     
    if(count==2)
    {
    path=JPanels.path1;
    image=new ImageIcon(""+path).getImage();
    /*resizer.setBounds(50, 50, 200, 150);
    image = new ImageIcon(""+path).getImage();
    resizer.add(area);
    //setLocationRelativeTo(null);
    resizer.setVisible(true);*/
    }
     
     
    }
     
    });
    panel1.add(toolbar,BorderLayout.SOUTH);
    panel1.setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.RAISED), new MatteBorder(5, 5, 5, 5, icon)));
     
     
    panel2.setPreferredSize(new Dimension(735,110));
    panel2.setBackground(Color.WHITE);
    System.out.println("string in panel:"+str);
     
    label1.setText(str);
    panel2.add(label1,BorderLayout.WEST);
    frameArea.add(panel2, BorderLayout.NORTH);
     
    panel3();
     
    JPanel panel4= new JPanel();
    panel4.setPreferredSize(new Dimension(735,50));
    panel4.setBackground(Color.pink);
    System.out.println("string in panel:"+str);
    panel4.add(label2);
    frameArea.add(panel4, BorderLayout.SOUTH);
     
    f1 = new JFrame("Frame in Java Swing");
    f1.setSize(new Dimension(709, 600));
    f1.setResizable(false);
    f1.setPreferredSize(new Dimension(735,660));
    f1.setVisible(true);
    area.setBackground(Color.white);
    resizer = new Resizable(area);
    resizer.setBounds(0,95, 90, 118);
    image = new ImageIcon(""+path).getImage();
    resizer.add(area);
    //setLocationRelativeTo(null);
    // resizer.setVisible(true);
    f1.add(resizer,BorderLayout.CENTER);
    f1.add(panel1, BorderLayout.NORTH);
     
    f1.add(frameArea);
     
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
     
    public void panel3()
    {
    panel3.setPreferredSize(new Dimension(735,400));
    panel3.setBackground(Color.darkGray);
    panel3.setBounds(0,110, 703, 310);
    System.out.println("entered panel3:");
     
    panel3.setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.RAISED), new MatteBorder(5, 5, 5, 5, icon)));
    frameArea.add(panel3, BorderLayout.CENTER);
     
    }
     
    public class MyDrawPanel1 extends JPanel
    {
     
    private static final long serialVersionUID = 1L;
     
    public void paintComponent(Graphics g){
     
    Dimension d = getSize();
    g.drawImage(image1, 0,0,d.width,d.height,this);
    System.out.println("entered drawpanel1....");
     
    }
    }
     
    public class MyDrawPanel extends JPanel{
    public void paintComponent(Graphics g){
     
    Dimension d = getSize();
    g.drawImage(image, 0,0,d.width,d.height,this);
     
    }
     
    public static void main(String[] args)
    {
     
    new frame();
    }
     
    }
     >
     
    //resizer border class 
     
     
    <
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Cursor;
    import java.awt.Graphics;
    import java.awt.Insets;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
     
    import javax.swing.SwingConstants;
    import javax.swing.border.Border;
     
    // ResizableBorder.java 
     
    public class ResizableBorder implements Border {
      private int dist = 8;
     
      int locations[] = 
      {
        SwingConstants.NORTH, SwingConstants.SOUTH, SwingConstants.WEST,
        SwingConstants.EAST, SwingConstants.NORTH_WEST,
        SwingConstants.NORTH_EAST, SwingConstants.SOUTH_WEST,
        SwingConstants.SOUTH_EAST
      };
     
      int cursors[] =
      { 
        Cursor.N_RESIZE_CURSOR, Cursor.S_RESIZE_CURSOR, Cursor.W_RESIZE_CURSOR,
        Cursor.E_RESIZE_CURSOR, Cursor.NW_RESIZE_CURSOR, Cursor.NE_RESIZE_CURSOR,
        Cursor.SW_RESIZE_CURSOR, Cursor.SE_RESIZE_CURSOR
      };
     
      public ResizableBorder(int dist) {
        this.dist = dist;
      }
     
      public Insets getBorderInsets(Component component) {
          return new Insets(dist, dist, dist, dist);
      }
     
      public boolean isBorderOpaque() {
          return false;
      }
     
      public void paintBorder(Component component, Graphics g, int x, int y,
                              int w, int h) {
          g.setColor(Color.RED);
          g.drawRect(x + dist / 2, y + dist / 2, w - dist, h - dist);
     
          if (component.hasFocus()) {
     
     
            for (int i = 0; i < locations.length; i++) {
              Rectangle rect = getRectangle(x, y, w, h, locations[i]);
              g.setColor(Color.WHITE);
              g.fillRect(rect.x, rect.y, rect.width - 1, rect.height - 1);
              g.setColor(Color.BLACK);
              g.drawRect(rect.x, rect.y, rect.width - 1, rect.height - 1);
            }
          }
      }
     
      private Rectangle getRectangle(int x, int y, int w, int h, int location) {
          switch (location) {
          case SwingConstants.NORTH:
              return new Rectangle(x + w / 2 - dist / 2, y, dist, dist);
          case SwingConstants.SOUTH:
              return new Rectangle(x + w / 2 - dist / 2, y + h - dist, dist,
                                   dist);
          case SwingConstants.WEST:
              return new Rectangle(x, y + h / 2 - dist / 2, dist, dist);
          case SwingConstants.EAST:
              return new Rectangle(x + w - dist, y + h / 2 - dist / 2, dist,
                                   dist);
          case SwingConstants.NORTH_WEST:
              return new Rectangle(x, y, dist, dist);
          case SwingConstants.NORTH_EAST:
              return new Rectangle(x + w - dist, y, dist, dist);
          case SwingConstants.SOUTH_WEST:
              return new Rectangle(x, y + h - dist, dist, dist);
          case SwingConstants.SOUTH_EAST:
              return new Rectangle(x + w - dist, y + h - dist, dist, dist);
          }
          return null;
      }
     
      public int getCursor(MouseEvent me) {
          Component c = me.getComponent();
          int w = c.getWidth();
          int h = c.getHeight();
     
          for (int i = 0; i < locations.length; i++) {
              Rectangle rect = getRectangle(0, 0, w, h, locations[i]);
              if (rect.contains(me.getPoint()))
                  return cursors[i];
          }
     
          return Cursor.MOVE_CURSOR;
      }
    }
     
    >
    // resizable class 
    <
     
     
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Cursor;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
     
    import javax.swing.JComponent;
    import javax.swing.event.MouseInputAdapter;
    import javax.swing.event.MouseInputListener;
     
    // Resizable.java 
     
    public class Resizable extends JComponent {
     
      /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
     
    public Resizable(Component comp) {
        this(comp, new ResizableBorder(8));
      }
     
      public Resizable(Component comp, ResizableBorder border) {
        setLayout(new BorderLayout());
        add(comp);
        addMouseListener(resizeListener);
        addMouseMotionListener(resizeListener);
        setBorder(border);
      }
     
      private void resize() {
          if (getParent() != null) {
            ((JComponent)getParent()).revalidate();
          }
      }
     
      MouseInputListener resizeListener = new MouseInputAdapter() {
        public void mouseMoved(MouseEvent me) {
          if (hasFocus()) {
              ResizableBorder border = (ResizableBorder)getBorder();
              setCursor(Cursor.getPredefinedCursor(border.getCursor(me)));
          }
        }
     
        public void mouseExited(MouseEvent mouseEvent) {
           setCursor(Cursor.getDefaultCursor());
        }
     
        private int cursor;
        private Point startPos = null;
     
        public void mousePressed(MouseEvent me) {
          ResizableBorder border = (ResizableBorder)getBorder();
          cursor = border.getCursor(me);
          startPos = me.getPoint();
          requestFocus();
          repaint();
        }
     
        public void mouseDragged(MouseEvent me) {
     
          if (startPos != null) {
     
            int x = getX();
            int y = getY();
            int w = getWidth();
            int h = getHeight();
     
            int dx = me.getX() - startPos.x;
            int dy = me.getY() - startPos.y;
     
            switch (cursor) {
              case Cursor.N_RESIZE_CURSOR:
                if (!(h - dy < 50)) {
                  setBounds(x, y + dy, w, h - dy);
                  resize();
                }
                break;
     
              case Cursor.S_RESIZE_CURSOR:
                if (!(h + dy < 50)) {
                  setBounds(x, y, w, h + dy);
                  startPos = me.getPoint();
                  resize();
                }
                break;
     
              case Cursor.W_RESIZE_CURSOR:
                if (!(w - dx < 50)) {
                  setBounds(x + dx, y, w - dx, h);
                  resize();
                }
                break;
     
              case Cursor.E_RESIZE_CURSOR:
                if (!(w + dx < 50)) {
                  setBounds(x, y, w + dx, h);
                  startPos = me.getPoint();
                  resize();
                }
                break;
     
              case Cursor.NW_RESIZE_CURSOR:
                if (!(w - dx < 50) && !(h - dy < 50)) {
                  setBounds(x + dx, y + dy, w - dx, h - dy);
                  resize();
                }
                break;
     
              case Cursor.NE_RESIZE_CURSOR:
                if (!(w + dx < 50) && !(h - dy < 50)) {
                  setBounds(x, y + dy, w + dx, h - dy);
                  startPos = new Point(me.getX(), startPos.y);
                  resize();
                }
                break;
     
              case Cursor.SW_RESIZE_CURSOR:
                if (!(w - dx < 50) && !(h + dy < 50)) {
                  setBounds(x + dx, y, w - dx, h + dy);
                  startPos = new Point(startPos.x, me.getY());
                  resize();
                }
                break;
     
              case Cursor.SE_RESIZE_CURSOR:
                if (!(w + dx < 50) && !(h + dy < 50)) {
                  setBounds(x, y, w + dx, h + dy);
                  startPos = me.getPoint();
                  resize();
                }
              break;
     
              case Cursor.MOVE_CURSOR:
                Rectangle bounds = getBounds();
                bounds.translate(dx, dy);
                setBounds(bounds);
                resize();
              }
     
     
              setCursor(Cursor.getPredefinedCursor(cursor));
            }
         }
     
       public void mouseReleased(MouseEvent mouseEvent) {
         startPos = null;
        }
      };
    }
     
     
    >
    Last edited by swathi1stjune; May 5th, 2012 at 07:56 AM. Reason: i want brief clarrification that where i did mistake

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Resizer image to rotate freely without effecting other panels of frame using swin

    Quote Originally Posted by swathi1stjune View Post
    thanks for ur response......

    panel1 --> tools buttons (to get paths of images for logo and background)

    panel2 --> to place header part

    panel3 --> to place background image

    panel4 --> to place footer part.


    when iam getting path from another frame to this frame,at that time i want resizer logo to appear on my frame.

    now i want resizer to keep my logo any where in frame without effecting dimensions of frame and panels, so that i can move my resizer freely over panels.

    1more mistake iam getting when i return to this frame by getting path from another frame immediately the logo must appear, but iam not getting untill i click mouse on that resizer, its not appearing. please test my code and correct me to get the required output.


    the resizer image code goes here....
     
    <      JFrame f1;
    JPanel panel2= new JPanel();
    JPanel panel3= new MyDrawPanel1();
    JPanel area= new MyDrawPanel();
    JPanels j;
    private Resizable resizer;
    Image image;
    Image image1;
    String s;
    Color icon = null;
    Color c1=null;
    int count=0;
    String path1;
    String path;
    JPanel frameArea = new JPanel(new BorderLayout(735,500));
    JLabel label1=new JLabel();
    JLabel label2=new JLabel();
    public frame()
    {
     
    addMouseListener(new MouseAdapter()
    {
    public void mousePressed(MouseEvent me)
    {
     
    requestFocus();
    resizer.repaint();
    }
    });
     
    JPanel panel1= new JPanel(new BorderLayout());
    panel1.setPreferredSize(new Dimension(735,100));
    JMenuBar menuBar = new JMenuBar();
    JMenu menu1 = new JMenu("file");
    menuBar.add(menu1);
    panel1.add(menuBar,BorderLayout.NORTH);
    panel1.setBackground(Color.LIGHT_GRAY);
    JToolBar toolbar = new JToolBar();
    JButton tools = new JButton("Tools");
    toolbar.add(tools);
    tools.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event) {
    click++;
     
    j = new JPanels(f1);
     
    count=JPanels.getCount();
     
    if(count==1)
    {
     
    try
    {
    f1.remove(panel3);
    path1=JPanels.path;
    image1=new ImageIcon(""+path1).getImage();
    System.out.println("path in tools:"+path1);
    panel3();
    }
    }
     
    if(count==2)
    {
    path=JPanels.path1;
    image=new ImageIcon(""+path).getImage();
    /*resizer.setBounds(50, 50, 200, 150);
    image = new ImageIcon(""+path).getImage();
    resizer.add(area);
    //setLocationRelativeTo(null);
    resizer.setVisible(true);*/
    }
     
     
    }
     
    });
    panel1.add(toolbar,BorderLayout.SOUTH);
    panel1.setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.RAISED), new MatteBorder(5, 5, 5, 5, icon)));
     
     
    panel2.setPreferredSize(new Dimension(735,110));
    panel2.setBackground(Color.WHITE);
    System.out.println("string in panel:"+str);
     
    label1.setText(str);
    panel2.add(label1,BorderLayout.WEST);
    frameArea.add(panel2, BorderLayout.NORTH);
     
    panel3();
     
    JPanel panel4= new JPanel();
    panel4.setPreferredSize(new Dimension(735,50));
    panel4.setBackground(Color.pink);
    System.out.println("string in panel:"+str);
    panel4.add(label2);
    frameArea.add(panel4, BorderLayout.SOUTH);
     
    f1 = new JFrame("Frame in Java Swing");
    f1.setSize(new Dimension(709, 600));
    f1.setResizable(false);
    f1.setPreferredSize(new Dimension(735,660));
    f1.setVisible(true);
    area.setBackground(Color.white);
    resizer = new Resizable(area);
    resizer.setBounds(0,95, 90, 118);
    image = new ImageIcon(""+path).getImage();
    resizer.add(area);
    //setLocationRelativeTo(null);
    // resizer.setVisible(true);
    f1.add(resizer,BorderLayout.CENTER);
    f1.add(panel1, BorderLayout.NORTH);
     
    f1.add(frameArea);
     
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
     
    public void panel3()
    {
    panel3.setPreferredSize(new Dimension(735,400));
    panel3.setBackground(Color.darkGray);
    panel3.setBounds(0,110, 703, 310);
    System.out.println("entered panel3:");
     
    panel3.setBorder(new CompoundBorder(new EtchedBorder(EtchedBorder.RAISED), new MatteBorder(5, 5, 5, 5, icon)));
    frameArea.add(panel3, BorderLayout.CENTER);
     
    }
     
    public class MyDrawPanel1 extends JPanel
    {
     
    private static final long serialVersionUID = 1L;
     
    public void paintComponent(Graphics g){
     
    Dimension d = getSize();
    g.drawImage(image1, 0,0,d.width,d.height,this);
    System.out.println("entered drawpanel1....");
     
    }
    }
     
    public class MyDrawPanel extends JPanel{
    public void paintComponent(Graphics g){
     
    Dimension d = getSize();
    g.drawImage(image, 0,0,d.width,d.height,this);
     
    }
     
    public static void main(String[] args)
    {
     
    new frame();
    }
     
    }
     >
     
    //resizer border class 
     
     
    <
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Cursor;
    import java.awt.Graphics;
    import java.awt.Insets;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
     
    import javax.swing.SwingConstants;
    import javax.swing.border.Border;
     
    // ResizableBorder.java 
     
    public class ResizableBorder implements Border {
      private int dist = 8;
     
      int locations[] = 
      {
        SwingConstants.NORTH, SwingConstants.SOUTH, SwingConstants.WEST,
        SwingConstants.EAST, SwingConstants.NORTH_WEST,
        SwingConstants.NORTH_EAST, SwingConstants.SOUTH_WEST,
        SwingConstants.SOUTH_EAST
      };
     
      int cursors[] =
      { 
        Cursor.N_RESIZE_CURSOR, Cursor.S_RESIZE_CURSOR, Cursor.W_RESIZE_CURSOR,
        Cursor.E_RESIZE_CURSOR, Cursor.NW_RESIZE_CURSOR, Cursor.NE_RESIZE_CURSOR,
        Cursor.SW_RESIZE_CURSOR, Cursor.SE_RESIZE_CURSOR
      };
     
      public ResizableBorder(int dist) {
        this.dist = dist;
      }
     
      public Insets getBorderInsets(Component component) {
          return new Insets(dist, dist, dist, dist);
      }
     
      public boolean isBorderOpaque() {
          return false;
      }
     
      public void paintBorder(Component component, Graphics g, int x, int y,
                              int w, int h) {
          g.setColor(Color.RED);
          g.drawRect(x + dist / 2, y + dist / 2, w - dist, h - dist);
     
          if (component.hasFocus()) {
     
     
            for (int i = 0; i < locations.length; i++) {
              Rectangle rect = getRectangle(x, y, w, h, locations[i]);
              g.setColor(Color.WHITE);
              g.fillRect(rect.x, rect.y, rect.width - 1, rect.height - 1);
              g.setColor(Color.BLACK);
              g.drawRect(rect.x, rect.y, rect.width - 1, rect.height - 1);
            }
          }
      }
     
      private Rectangle getRectangle(int x, int y, int w, int h, int location) {
          switch (location) {
          case SwingConstants.NORTH:
              return new Rectangle(x + w / 2 - dist / 2, y, dist, dist);
          case SwingConstants.SOUTH:
              return new Rectangle(x + w / 2 - dist / 2, y + h - dist, dist,
                                   dist);
          case SwingConstants.WEST:
              return new Rectangle(x, y + h / 2 - dist / 2, dist, dist);
          case SwingConstants.EAST:
              return new Rectangle(x + w - dist, y + h / 2 - dist / 2, dist,
                                   dist);
          case SwingConstants.NORTH_WEST:
              return new Rectangle(x, y, dist, dist);
          case SwingConstants.NORTH_EAST:
              return new Rectangle(x + w - dist, y, dist, dist);
          case SwingConstants.SOUTH_WEST:
              return new Rectangle(x, y + h - dist, dist, dist);
          case SwingConstants.SOUTH_EAST:
              return new Rectangle(x + w - dist, y + h - dist, dist, dist);
          }
          return null;
      }
     
      public int getCursor(MouseEvent me) {
          Component c = me.getComponent();
          int w = c.getWidth();
          int h = c.getHeight();
     
          for (int i = 0; i < locations.length; i++) {
              Rectangle rect = getRectangle(0, 0, w, h, locations[i]);
              if (rect.contains(me.getPoint()))
                  return cursors[i];
          }
     
          return Cursor.MOVE_CURSOR;
      }
    }
     
    >
    // resizable class 
    <
     
     
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Cursor;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
     
    import javax.swing.JComponent;
    import javax.swing.event.MouseInputAdapter;
    import javax.swing.event.MouseInputListener;
     
    // Resizable.java 
     
    public class Resizable extends JComponent {
     
      /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
     
    public Resizable(Component comp) {
        this(comp, new ResizableBorder(8));
      }
     
      public Resizable(Component comp, ResizableBorder border) {
        setLayout(new BorderLayout());
        add(comp);
        addMouseListener(resizeListener);
        addMouseMotionListener(resizeListener);
        setBorder(border);
      }
     
      private void resize() {
          if (getParent() != null) {
            ((JComponent)getParent()).revalidate();
          }
      }
     
      MouseInputListener resizeListener = new MouseInputAdapter() {
        public void mouseMoved(MouseEvent me) {
          if (hasFocus()) {
              ResizableBorder border = (ResizableBorder)getBorder();
              setCursor(Cursor.getPredefinedCursor(border.getCursor(me)));
          }
        }
     
        public void mouseExited(MouseEvent mouseEvent) {
           setCursor(Cursor.getDefaultCursor());
        }
     
        private int cursor;
        private Point startPos = null;
     
        public void mousePressed(MouseEvent me) {
          ResizableBorder border = (ResizableBorder)getBorder();
          cursor = border.getCursor(me);
          startPos = me.getPoint();
          requestFocus();
          repaint();
        }
     
        public void mouseDragged(MouseEvent me) {
     
          if (startPos != null) {
     
            int x = getX();
            int y = getY();
            int w = getWidth();
            int h = getHeight();
     
            int dx = me.getX() - startPos.x;
            int dy = me.getY() - startPos.y;
     
            switch (cursor) {
              case Cursor.N_RESIZE_CURSOR:
                if (!(h - dy < 50)) {
                  setBounds(x, y + dy, w, h - dy);
                  resize();
                }
                break;
     
              case Cursor.S_RESIZE_CURSOR:
                if (!(h + dy < 50)) {
                  setBounds(x, y, w, h + dy);
                  startPos = me.getPoint();
                  resize();
                }
                break;
     
              case Cursor.W_RESIZE_CURSOR:
                if (!(w - dx < 50)) {
                  setBounds(x + dx, y, w - dx, h);
                  resize();
                }
                break;
     
              case Cursor.E_RESIZE_CURSOR:
                if (!(w + dx < 50)) {
                  setBounds(x, y, w + dx, h);
                  startPos = me.getPoint();
                  resize();
                }
                break;
     
              case Cursor.NW_RESIZE_CURSOR:
                if (!(w - dx < 50) && !(h - dy < 50)) {
                  setBounds(x + dx, y + dy, w - dx, h - dy);
                  resize();
                }
                break;
     
              case Cursor.NE_RESIZE_CURSOR:
                if (!(w + dx < 50) && !(h - dy < 50)) {
                  setBounds(x, y + dy, w + dx, h - dy);
                  startPos = new Point(me.getX(), startPos.y);
                  resize();
                }
                break;
     
              case Cursor.SW_RESIZE_CURSOR:
                if (!(w - dx < 50) && !(h + dy < 50)) {
                  setBounds(x + dx, y, w - dx, h + dy);
                  startPos = new Point(startPos.x, me.getY());
                  resize();
                }
                break;
     
              case Cursor.SE_RESIZE_CURSOR:
                if (!(w + dx < 50) && !(h + dy < 50)) {
                  setBounds(x, y, w + dx, h + dy);
                  startPos = me.getPoint();
                  resize();
                }
              break;
     
              case Cursor.MOVE_CURSOR:
                Rectangle bounds = getBounds();
                bounds.translate(dx, dy);
                setBounds(bounds);
                resize();
              }
     
     
              setCursor(Cursor.getPredefinedCursor(cursor));
            }
         }
     
       public void mouseReleased(MouseEvent mouseEvent) {
         startPos = null;
        }
      };
    }
     
     
    >


    The posted code will not compile and can not be tested.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Image without a frame, is it possible?
    By knightmetal in forum AWT / Java Swing
    Replies: 5
    Last Post: August 15th, 2012, 03:52 AM
  2. Replies: 1
    Last Post: January 19th, 2012, 03:44 PM
  3. adding or removing panels to panels
    By luisp88 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 1st, 2011, 04:37 PM
  4. Frame not showing Image until resized
    By gamalytical in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 13th, 2011, 07:09 AM
  5. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM

Tags for this Thread