Results 1 to 3 of 3
  1. #1
    gameaddict123 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Try move the call to setVisible after you create the rotator2 object.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    9

    Default

    Also, custom painting is done by overriding the paintComponent() method, NOT the paint() method.

Similar Threads

  1. [Help] W/ background image
    By gundum584 in forum New To Java
    Replies: 9
    Last Post: 01-10-2011, 06:48 AM
  2. Image not showing up!!
    By ThrashingBoy in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 11:22 PM
  3. showing image in web application
    By Juuno in forum Advanced Java
    Replies: 3
    Last Post: 05-11-2009, 04:36 AM
  4. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 06:49 PM
  5. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2008, 11:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •