How to make a button remain still while on top of a rotating image?
I've tried making an interactive menu that has a rotating image in the background and some buttons on top of that background. The background itself is in a JPanel and the button is added directly on that JPanel since the Panel occupies the entire frame. Now, the problem is that the button spins along with the rotated graphics X_X. So...any ideas?
Here's some of the code:
class EyE extends JPanel{
Image im;
public int angx=0;
MediaTracker tracker=new MediaTracker(this);
EyE(){
Toolkit ec=Toolkit.getDefaultToolkit();
im=ec.getImage("Graphics\\hcr1.png");
setPreferredSize(new Dimension (400,400));
setBackground(Color.BLACK);
JButton frog=new JButton("Jerry");
this.add(frog);
setOpaque(false);
setVisible(true);
}
public void paintComponent(Graphics ecr){
Graphics2D g2d=(Graphics2D)ecr;
tracker.addImage(im,1);
try{tracker.waitForID(1);}
catch(InterruptedException ie){}
//code for roatating image (really didn't want to use AffineTransform)
double cs=Math.cos(Math.toRadians(angx)),ss=Math.sin(Math.toRadians(angx));
g2d.rotate(Math.toRadians(angx));
double x2=(200*cs-200*ss),y2=(200*ss+200*cs);
double m,n,d,tetcs1,tetss1,tetcs2,tetss2,ang1,ang2;
d=(Math.sqrt(Math.pow((y2-200),2)+Math.pow((x2-200),2)));
tetcs1=d/(400*Math.sqrt(2));
tetss1=Math.sqrt(1-Math.pow(tetcs1,2));
tetcs2=(160000-Math.pow(d,2))/160000;
tetss2=Math.sqrt(1-Math.pow(tetcs2,2));
ang2=180-2*Math.toDegrees(Math.acos(tetcs1));
ang1=ang2+Math.toDegrees(Math.acos(tetcs1))-45;
n=d*(Math.sin(Math.toRadians(ang1)));
double g=Math.sin(Math.toRadians(ang1));
m=Math.sqrt(Math.pow(d,2)-Math.pow(n,2));
if(angx<=90)g2d.translate(m,-n);
else if(angx<=180)g2d.translate(-m,-n);
else if(angx<=270)g2d.translate(-n,-m);
else if(angx<=360)g2d.translate(-n,m);
//rotating code ends
g2d.drawImage(im, 0,0,400,400, this);
}
}
# 1
class EyE extends JPanel {
Image im;
public int angx=0;
MediaTracker tracker=new MediaTracker(this);
EyE(){
Toolkit ec=Toolkit.getDefaultToolkit();
im=ec.getImage("Graphics\\hcr1.png");
setPreferredSize(new Dimension (400,400));
setBackground(Color.BLACK);
setOpaque(false);
setVisible(true);
}
public void paintComponent(Graphics ecr){
Graphics2D g2d=(Graphics2D)ecr.create();
tracker.addImage(im,1);
try{tracker.waitForID(1);}
catch(InterruptedException ie){}
//code for roatating image (really didn't want to use AffineTransform)
double cs=Math.cos(Math.toRadians(angx)),ss=Math.sin(Math.toRadians(angx));
g2d.rotate(Math.toRadians(angx));
double y2=(200*cs-200*ss),x2=(200*ss+200*cs);
double m,n,d,tetcs1,tetss1,tetcs2,tetss2,ang1,ang2;
d=(Math.sqrt(Math.pow((y2-200),2)+Math.pow((x2-200),2)));
tetcs1=d/(400*Math.sqrt(2));
tetss1=Math.sqrt(1-Math.pow(tetcs1,2));
tetcs2=(160000-Math.pow(d,2))/160000;
tetss2=Math.sqrt(1-Math.pow(tetcs2,2));
ang2=180-2*Math.toDegrees(Math.acos(tetcs1));
ang1=ang2+Math.toDegrees(Math.acos(tetcs1))-45;
n=d*(Math.sin(Math.toRadians(ang1)));
double g=Math.sin(Math.toRadians(ang1));
m=Math.sqrt(Math.pow(d,2)-Math.pow(n,2));
if(angx<=90)g2d.translate(m,-n);
else if(angx<=180)g2d.translate(-m,-n);
else if(angx<=270)g2d.translate(-n,-m);
else if(angx<=360)g2d.translate(-n,m);
//rotating code ends
g2d.drawImage(im, 0,0,400,400, this);
g2d.dispose();
super.paintComponent(ecr);
}
}
# 5
Right, well here's the code with the proper comments:
public void paintComponent(Graphics ecr){
Graphics ecr2=ecr.create(),ecr3;
//creating a copy of the original graphics context held by ecr in ecr2 so I can use this instead of the one on which the other components get drawned
ecr3=ecr; //the initial graphics context and ORIGIN gets saved
ecr=ecr2; //now i'll just use the copy
................
g2d.drawImage(im, 0,0,400,400, this);
g2d.dispose();//the copy is useless so might as well delete it
super.paintComponent(ecr3);//painting the components of the panel in the original graphics context, so the button or another comp. added on this one doesnt rotate as well
}
Hope it clarifies any misunderstandings.