0

It is a java program that is designed to look like a Windows 10 Blue Screen Of Death, but I can not figure out how to add an image to it.
I have tried many different methods, but they did not work out for me, maybe I was doing them wrong. The Image is the QR code that is going to be on the lower left hand corner.

import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;

public class BSODJava {
 public static void main(String[] args) {
  JFrame frame = new JFrame("Lab00");
  frame.setSize(1366, 768);
  frame.setLocation(0, 0);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setContentPane(new Panel1());
  frame.setVisible(true);
  frame.setBackground(Color.black);
 }
}
class Panel1 extends JPanel {
 public void paintComponent(Graphics g) {
  g.setColor(new Color(6, 117, 170));
  g.fillRect(1, 1, 1366, 768);

  g.setColor(new Color(255, 255, 255));
  g.setFont(new Font("Segoe UI", Font.PLAIN, 200));
  g.drawString(";)", 50, 165);

  g.setColor(new Color(255, 255, 255));
  g.setFont(new Font("Segoe UI", Font.PLAIN, 52));
  g.drawString("Your PC ran into a problem and needs to restart.  We'll", 50, 270);

  g.setColor(new Color(255, 255, 255));
  g.setFont(new Font("Segoe UI", Font.PLAIN, 52));
  g.drawString("restart for you.", 50, 330);
 }
}
  • 1) *"Ok, I think i did everything you said"* Everything who said? 2) Do not add new information as an answer. Instead [edit] the question. 3) The errors seem to be entered as though they were a runnable JavaScript. Instead use code formatting (as used in the original question). – Andrew Thompson Jun 02 '20 at 16:26
  • BTW - `g.fillRect(1,1,1366,768);` the upper left pixel is 0,0 and the width and height can be determined at run-time, so more like `g.fillRect(0,0,getWidth(),getHeight());`. But having said that, set the background color in a constructor and in the paint method, call `super.paintComponent(g)` to have the BG painted automatically. – Andrew Thompson Jun 02 '20 at 16:30
  • I would recommend optimising your imports. Importing x.x.* is not great practice and will lead to slower programs – Daniel H. Jun 02 '20 at 17:06
  • @Sqepia "Importing x.x.* is not great practice and will lead to slower programs" No it won't. All imports are resolved at compile time and only fully qualified class names are written in the class files. So it will slow down compilation a tiny bit (usually not noticeably) but make 0 difference to how a class runs. The reason for using fully qualified imports are for the benefit of other programmers. But for making a short code on SO, even shorter, they are perfectly acceptable. – Andrew Thompson Jun 03 '20 at 23:36

2 Answers2

1

There is no need to do all that custom painting. Swing has components to do the painting for you.

Start by using a JLabel. You can display a JLabel with text and/or Icon. Read the section from the Swing tutorial on How to Use Icons for examples to get you started.

Then also learn how to use Layout Managers to position the components on the panel.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • So a label would be on to of the frame? – Ephram Easley Jun 02 '20 at 15:46
  • and also where in my code would i put the: ImageIcon icon = createImageIcon("images/middle.gif", "a pretty but meaningless splat"); label1 = new JLabel("Image and Text", icon, JLabel.CENTER); – Ephram Easley Jun 02 '20 at 15:52
  • Glad it helped. Don't forget to "accept" the answer by clicking on the checkmark so people know the problem has been solved. – camickr Jun 03 '20 at 01:16
0

In your main (or if you are using the Swing solution from camickr somewhere when you initialise your frame) you load your image. Somewhat like this:

 BufferedImage image;

 // somewhere in your constructor for example
 this.image = ImageIO.read(new File("/Path/To/My/Picture/some-picture.jpg"));

if you want to have the picture within your application in the resources folder, you should copy it to your application (like I showed in this answer).

Now when you have loaded you image and your paintComponent method is called, you can supply it with the information how and where to paint it:

public void paintComponent(Graphics g) 
{ 
  // ... your code
  g.setColor(new Color(255, 255, 255)); 
  g.setFont(new Font("Segoe UI",Font.PLAIN, 52)); 
  g.drawString("restart for you.", 50, 330);     

  g.drawImage(this.image, 0,0,null);  // this will draw the image in the top left corner (keeping it's aspect ration, width and height)

  g.drawImage(                        // to draw the image in the bottom right corner
     this.image,                      // your image instance
     getWidth() - image.getWidth(),   // the frame's width minus the image's width
     getHeight() - image.getHeight(), // and the frame's height minus the image's height
     null                             // no need for an image observer
  );      
}

Of course you can do a lot more with images and manipulate them as you want. Maybe this tutorials from MKyong can help you in reading and writing images.

GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • 1
    `g.drawImage(this.image, 0,0,null);` should be `g.drawImage(this.image, 0,0,this);`, since every `JComponent` is an `ImageObserver`. – Andrew Thompson Jun 02 '20 at 16:23