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