public class mousedrag extends javax.swing.JApplet
{
Imagepanel image;
@Override
public void init()
{
Container cont = getContentPane();
cont.setLayout(new BorderLayout());
setPreferredSize(new Dimension(300, 300));
JScrollPane scroll = new JScrollPane();
getContentPane().add(scroll, BorderLayout.CENTER);
scroll.getViewport().add(new Imagepanel());
}
private class Imagepanel extends JPanel implements MouseListener,MouseMotionListener{
public Imagepanel()
{
panel();
addMouseMotionListener(this);
addMouseListener(this);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//MouseEvent e1 = this.e;
//g2.setColor(getBackground());
//g2.fillRect(0, 0, getWidth(), getHeight());
//g2.setColor(getForeground());
g2.setColor(getBackground());
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setColor(getForeground());
drawdig(g2);
drawCalendar(g2);
}
public void drawdig(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(new Color(0,157,250));
System.out.println(rectangles.size()+"SIZE");
for(Rectangle ret : rectangles)
{
g2.draw(ret);
g2.fill(ret);
}
public void mouseDragged(MouseEvent e)
{
Point p = e.getPoint();
Rectangle[] r = rectangles.toArray(new Rectangle[rectangles.size()]);
for(int i=0, j = rectangles.size(); i<j ; i++)
{
Point lastP = (Point)lastPos.get(r[i]);
if(r[i].contains(p))
{
r[i].translate(e.getX(), e.getY());
this.repaint();
}
lastPos.put(r[i], p);
}
public void mouseMoved(MouseEvent e)
{
lastPos.clear();
}
}