Results 1 to 3 of 3
- 06-17-2011, 12:53 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
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.
Java 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); } }Java 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(); } }
Last edited by gameaddict123; 06-17-2011 at 12:58 AM.
- 06-17-2011, 03:11 AM #2
Try move the call to setVisible after you create the rotator2 object.
- 06-17-2011, 03:52 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
[Help] W/ background image
By gundum584 in forum New To JavaReplies: 9Last Post: 01-10-2011, 05:48 AM -
Image not showing up!!
By ThrashingBoy in forum New To JavaReplies: 1Last Post: 11-08-2010, 10:22 PM -
showing image in web application
By Juuno in forum Advanced JavaReplies: 3Last Post: 05-11-2009, 04:36 AM -
Background image
By leiferouis in forum New To JavaReplies: 9Last Post: 03-08-2009, 05:49 PM -
Image as background
By Java.child in forum AWT / SwingReplies: 2Last Post: 10-02-2008, 11:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks