Jak stworzyć panel na którym będą małe panele i będzie także przeprowadzona animacja grafiki(kwadraty,linie,koła). Do tego chce mieć możliwość przesuwania tych wewnętrznych paneli co mam zrealizowane ale po przeciążeniu metody paint() wszystko znika.
Mam coś takiego:
public class JPanelSuper extends JPanel implements MouseMotionListener{
static public JPanelSuper INSTANCE ;
public int x,y;
public JLabel jLabel;
public JPanelMy jPanelMy;// = new JPanelMy();
public JPanelMy jPanelMy2;// = new JPanelMy();
public Line2D line2D;
/* Constructor */
public JPanelSuper() {
INSTANCE = this;
this.setLayout(null);
jLabel = new JLabel();
jLabel.setBackground(Color.green);
jLabel.setText("Label");
jLabel.setLocation(100, 20);
jLabel.setSize(200, 20);
this.add(jLabel);
// for (int i = 0; i < 2; i++) {
// this.add(new JPanelMy());
// }
jPanelMy = new JPanelMy();
jPanelMy2 = new JPanelMy();
this.add(jPanelMy);
this.add(jPanelMy2);
this.addMouseMotionListener(this);
/*MyCanvas*/
// MyCanvas canvas = new MyCanvas();
// canvas.setSize(50, 50);
// canvas.setLocation(20, 20);
// canvas.setBackground(Color.red);
// canvas.setPencilMode();
// canvas.setLineMode();
// //canvas.addComponentListener(new JPanelMy());
// this.add(canvas);
}
public void mouseDragged(MouseEvent e) {
//throw new UnsupportedOperationException("mouseDragged");
}
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
jLabel.setText(x+" "+y);
//throw new UnsupportedOperationException("mouseMoved");
}
public void paintNode() {
Graphics graphics = this.getGraphics();
Graphics2D g2 = (Graphics2D) graphics;
this.update(g2);
g2.setColor( new Color( 20, 20, 100));
//g2.drawLine(30, 30, 200, 200);
//g2.drawLine(jPanelMy.getX(), jPanelMy.getY(), jPanelMy2.getX(), jPanelMy2.getY());
int tx1,tx2,ty1,ty2;
tx1 = jPanelMy.getX() + (jPanelMy.getHeight()/2);
ty1 = jPanelMy.getY() + (jPanelMy.getWidth()/2);
tx2 = jPanelMy2.getX() + (jPanelMy2.getHeight()/2);
ty2 = jPanelMy2.getY() + (jPanelMy2.getWidth()/2);
g2.drawLine(tx1, ty1, tx2, ty2);
//line2D.contains(1, 1, 50, 30);
//line2D.setLine(1, 1, 100, 20);
//this.removeAll();
}
@Override
public void updateUI() {
super.updateUI();
}
// @Override
// public void paint ( Graphics g ) {
// jPanelMy.repaint();//this.repaint();
////przesuniecie układu wspolrzednych
// g.setColor( new Color( 20, 20, 100));
// g.drawLine(30, 30, 200, 200);
// }
}
class Singleton {
private static final Singleton INSTANCE = new Singleton();
//public JPanelSuper jPanelSuper = new JPanelSuper();
public static JPanelSuper jPanelSuper;// = JPanelSuper.INSTANCE;
private Singleton() {}
public static Singleton getInstance() {
jPanelSuper = JPanelSuper.INSTANCE;
return INSTANCE;
}
}
class JPanelMy extends JPanel
implements /*ActionListener,*//*DragGestureListener,DragSourceListener,*/ MouseListener, MouseMotionListener{
private Singleton singleton;
public int tx,ty;
static int id;
public int idSuper;
//konstruktor i metody
}