【问题标题】:How can I add a jpg saved on my computer to the code that i have written如何将保存在计算机上的 jpg 添加到我编写的代码中
【发布时间】:2020-09-21 03:08:48
【问题描述】:

它是一个 java 程序,旨在看起来像 Windows 10 蓝屏死机,但我不知道如何向它添加图像。
我尝试了许多不同的方法,但它们对我没有用,也许我做错了。图像是将位于左下角的 QR 码。

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) “好吧,我想我做了你说的一切” 谁说的一切? 2)不要添加新信息作为答案。而是edit 的问题。 3) 输入的错误似乎是可运行的 JavaScript。而是使用代码格式(在原始问题中使用)。
  • BTW - g.fillRect(1,1,1366,768); 左上角的像素是 0,0,宽度和高度可以在运行时确定,所以更像 g.fillRect(0,0,getWidth(),getHeight());。但话虽如此,在构造函数和paint方法中设置背景颜色,调用super.paintComponent(g)自动绘制BG。
  • 我建议优化您的导入。导入 x.x.* 不是很好的做法,会导致程序变慢
  • @Sqepia "导入 x.x.* 不是很好的做法,会导致程序变慢" 不,不会。所有导入都在编译时解析,只有完全限定的类名写入类文件。所以它会稍微减慢编译速度(通常不会明显),但会对类的运行方式产生 0 的影响。使用完全限定导入的原因是为了其他程序员的利益。但是对于在 SO 上制作短代码,甚至更短,它们是完全可以接受的。

标签: java swing graphics awt


【解决方案1】:

无需进行所有自定义绘画。 Swing 具有为您绘制的组件。

首先使用JLabel。您可以显示带有文本和/或图标的JLabel。阅读 How to Use Icons 上的 Swing 教程中的部分,以获取帮助您入门的示例。

接下来还要学习如何使用Layout Managers在面板上定位组件。

【讨论】:

  • 那么标签会在框架上吗?
  • 以及我将在我的代码中放置的位置:ImageIcon icon = createImageIcon("images/middle.gif", "a pretty but meaningless splat"); label1 = new JLabel("图像和文本", icon, JLabel.CENTER);
  • 很高兴它有帮助。不要忘记通过单击复选标记“接受”答案,以便人们知道问题已解决。
【解决方案2】:

在您的main 中(或者如果您在初始化框架时在某处使用来自 camickr 的 Swing 解决方案),您可以加载图像。有点像这样:

 BufferedImage image;

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

如果您想在资源文件夹中包含应用程序中的图片,则应将其复制到您的应用程序(like I showed in this answer)

现在,当您加载图像并调用 paintComponent 方法后,您可以向它提供如何以及在何处绘制它的信息:

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
  );      
}

当然,您可以对图像进行更多操作并根据需要进行操作。也许this tutorials from MKyong 可以帮助你读写图像。

【讨论】:

  • g.drawImage(this.image, 0,0,null); 应该是g.drawImage(this.image, 0,0,this);,因为每个JComponent 都是ImageObserver
猜你喜欢
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
相关资源
最近更新 更多