title: Picture output.(Java)
body: <p>I have a problem with the code, can you tell what's wrong with it?</p>
  
  <p>Here's the code:</p>
  
  <pre><code>package Game;
  
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import javax.imageio.ImageIO;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class Game extends JPanel {
  
      private static final long serialVersionUID = 1L;
  
      private BufferedImage image;
  
      public static final int WIDTH = 600;
      public static final int HEIGHT = 500;
  
      public static void main(String avg[]) throws IOException {
          Game abc = new Game();
      }
  
      public Game() {
          try {
              JFrame frame = new JFrame();
              frame.setSize(WIDTH, HEIGHT);
              frame.setVisible(true);
              frame.getContentPane().setBackground(Color.BLACK);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
              image = ImageIO.read(new File(
                      "C:\\Users\\дНМ\\workspace\\Game\\image\\heroG.png"));
          } catch (IOException ex) {
              // handle exception...
          }
      }
  
      @Override
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.drawImage(image, 0, 0, null); 
      }
  
  }
  </code></pre>
  
  <p>I get a window, but the picture does not show.If you write what the problem is, it would be just great!And if you still have fixed - complemented the code, it would be all super!</p>
  
  <p>Thank you for attention.</p>
  
  <p><strong>UPD</strong></p>
  
  <p>Thank you all.
  Updated the code like this, the image is brought out, but the background is no longer black!</p>
  
  <pre><code>public Game() {
          try {   
              image = ImageIO.read(new File(
                      "C:\\Users\\дНМ\\workspace\\Game\\image\\heroG.png"));
  
              JFrame frame = new JFrame();
              frame.setSize(WIDTH, HEIGHT);
              frame.setVisible(true);
              frame.getContentPane().setBackground(Color.BLACK);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
              frame.add(this);
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  </code></pre>
  
  <p>How to return me a black background?)</p>
  
  <p><strong>UPD2</strong></p>
  
  <p>Here is a code works for me, thank you all.</p>
  
  <pre><code>package Game;
  
  import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import javax.imageio.ImageIO;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  
  public class Game extends JPanel {
  
      private static final long serialVersionUID = 1L;
  
      private BufferedImage image;
  
      public static final int WIDTH = 600;
      public static final int HEIGHT = 500;
  
      public static void main(String avg[]) throws IOException {
          Game abc = new Game();
  
      }
  
      public Game() {
          try {
              JFrame frame = new JFrame();
              image = ImageIO.read(new File(
                      "C:\\Users\\дНМ\\workspace\\Game\\image\\heroG.png"));
  
              frame.setSize(WIDTH, HEIGHT);
              frame.setVisible(true);
              frame.getContentPane().add(this);
              this.setBackground(Color.BLACK);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
  
      @Override
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.drawImage(image, 0, 0, null); // see javadoc for more info on the
                                          // parameters
      }
  
  }
  </code></pre>