image and background not showing.
Well my frame and panel have compiled successfully, but when I try to run it, the background is gray(despite the code telling it to be black), and the picture of the rectangle is not showing. Here is my code.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
public class rotator2 extends JPanel{
Image myimg;
public rotator2(){
setSize(400, 400);
setDoubleBuffered(true);
setBackground(Color.BLACK);
setFocusable(true);
ImageIcon ii=new ImageIcon(this.getClass().getResource("untitled.jpg"));
myimg=ii.getImage();
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d=(Graphics2D)g;
AffineTransform origXform=g2d.getTransform();
AffineTransform newXform=(AffineTransform)(origXform.clone());
int Xrot=this.getWidth()/2;
int Yrot=this.getHeight()/2;
newXform.rotate(Math.toRadians(50), Xrot, Yrot);
g2d.setTransform(newXform);
int x=(getWidth()-myimg.getWidth(this))/2;
int y=(getHeight()-myimg.getHeight(this))/2;
g2d.drawImage(myimg, x, y, this);
g2d.setTransform(origXform);
}
}
Code:
import javax.swing.JFrame;
public class drawframe2 extends JFrame{
public drawframe2(){
setSize(400, 400);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setTitle("kjasljklsd");
add(new rotator2());
}
public static void main(String[] args){
drawframe2 nn=new drawframe2();
}
}
http://img810.imageshack.us/img810/8586/myprogram.jpg