Note that there are some explanatory texts on larger screens.

plurals
  1. POI can not get an image to pop up using Swing
    primarykey
    data
    text
    <p>I have googled this and searched through this site to try and get an Image to show up using Swing, but every time i implement the code i find online it doesn't work. I have no idea what i am doing wrong, can anyone help me?</p> <pre><code>package Game; import java.awt.Dimension; import java.awt.Graphics; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JPanel; public class Amazing { private int width = 300; private int height = width / 16 * 9; private int scale = 3; private static Graphics g; private static JFrame frame = new JFrame(); private static JPanel panel = new JPanel(); public Amazing(){ Dimension size = new Dimension(width*scale, height*scale); frame.setPreferredSize(size); frame.setResizable(false); frame.setTitle("Amazing!"); frame.add(panel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args){ Amazing amazing = new Amazing(); Character character = new Character(); character.paintComponent(g); } } </code></pre> <hr> <pre><code>package Game; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JPanel; public class Character extends JPanel{ private BufferedImage image; public Character(){ try { image = ImageIO.read(new File("E:\\Libraries\\Documents\\Java Stuff\\workspace\\Java_Final\\narwhal.png")); } catch (IOException ex) { // handle exception... } } protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 50, 50, null); // see javadoc for more info on the parameters } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload