Java 如何在屏幕上绘制图像?
我想使用绘画方法将图像添加到屏幕上?我不想使用JLabel,因为我想自己设置位置 板级:Java 如何在屏幕上绘制图像?,java,image,Java,Image,我想使用绘画方法将图像添加到屏幕上?我不想使用JLabel,因为我想自己设置位置 板级: public class Board extends JPanel{ BufferedImage image; public Board() { try { image = ImageIO.read(new File("C:\\Users\\alexa_000\\Pictures\\RocketShip.png")); }catch(IOException e) {
public class Board extends JPanel{
BufferedImage image;
public Board() {
try {
image = ImageIO.read(new File("C:\\Users\\alexa_000\\Pictures\\RocketShip.png"));
}catch(IOException e) {
e.printStackTrace();
}
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.drawImage(image, 0, 0, this);
}
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(),image.getHeight());
}
}
RType.class:
public class RType extends JFrame{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
Panel panel = new Panel();
frame.setContentPane(panel);
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
你有这么多的错误,我甚至不知道从哪里开始。。。您可以在某些容器上显示图像,而不是在JLabel上显示图像。像杰帕内尔。 下面是一个演示如何在JPanel上显示图像的小演示。代码解释
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
//No need to extend your class with JFrame
public class Board {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Instantiate Panel class.
Panel panel = new Panel();
//Set instance of Panel class as a content pane for your JFrame
frame.setContentPane(panel);
//Avoid calling setSize. Call pack instead
frame.pack();
//If you want to position your frame on center of screen, no need
//for fancy calculations. This is how you can do it.
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
/*Image will be displayed on JPanel*/
private static class Panel extends JPanel {
BufferedImage image;
public Panel() {
try {
//String you are passing in "new File()" is a path to your image
image = ImageIO.read(new File("path-to-your-image"));
} catch (IOException e) {
e.printStackTrace();
}
}
/*Override this method to display graphics on JPanel.
* Do not override paint method!*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,this);
}
/*Override getPreferredSize method so it returns dimensions of your image.
* Size of your container (Panel) will be equal to size of that image*/
@Override
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(),image.getHeight());
}
}
}
在哪里覆盖此绘制方法?在扩展JPanel的课程中,请发布一个更完整的程序,一个我们可以运行的程序,它向我们展示了什么不起作用,一个。这里有一个很好的教程给你:也请搜索这个网站,因为有大量与这个问题相关的Java绘图示例,我甚至还写了一些例子。我添加了程序的其余部分。我喜欢你开始提问的方式:p我对java比较陌生,所以有很多错误。不管怎样,我试过了你的答案,但还是没能成功。如果您想再次查看,我更新了我尝试的代码:D@alexdr3437:这意味着图像文件的路径可能错误。虽然最好使用InputStream,但是这个答案需要1+。另外,你真的应该阅读你评论中链接到的教程。你不能猜测这些东西,并希望它可能会起作用。完全复制代码。将路径设置为:C:/Users/alexa_000/Pictures/RocketShip.png。它应该会起作用。我刚刚测试了我的代码。好吧,它成功了。这通常是有效的,有什么原因它不在这里吗?顺便说一句,谢谢。很奇怪……我也用过它,当我像你们一样经过那个条小路时,它就起作用了。