我无法在JFrame上显示图片。 跑步时车架完全变黑。这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class JFrameTesting extends JFrame {
BufferedImage test = null;
public static void main(String[] args) throws URISyntaxException {
new JFrameTesting();
}
public JFrameTesting() throws URISyntaxException {
JFrame frame = new JFrame("My first JFrame!");
frame.setSize(400, 400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
test = ImageIO.read(new File(getClass().getResource("test.png").toURI()));
} catch (IOException ex) {
Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(test, 200, 200, null);
}
}
我不确定自己是否做错了什么。跑步时我没有任何错误。
提前致谢!
答案 0 :(得分:0)
您尚未将图像实际添加到JFrame中。要显示图像,您需要将BufferedImage添加到组件上然后绘制它。你可以使用JLabel和ImageIcon来做到这一点。
function line_detect(wall,point){ // based on line formula of analytic geometry
var a = (wall.p1[0] - wall.p2[0]) / (wall.p1[1] - wall.p2[1]);
var b = wall.p1[1];
var y = a*point[0]+b;
if(y == point[1])
return 0;
else if(y > point[1])
return 1;
else return -1;
}
function range_detect(wall,point){
if(point[0] > wall.p1[0]
&& point[0] > wall.p2[0])
return 0;
if(point[0] < wall.p1[0]
&& point[0] < wall.p2[0])
return 0;
if(point[1] > wall.p1[1]
&& point[1] > wall.p2[1])
return 0;
if(point[1] < wall.p1[1]
&& point[1] > wall.p2[1])
return 0;
return 1;
}
}
或者,如果需要,您可以跳过标签并绘制到组件上。在这种情况下,您将必须覆盖JPanel的draw方法。
public class JFrameTesting extends JFrame {
BufferedImage test = null;
ImageIcon image = new ImageIcon();
public static void main(String[] args) throws URISyntaxException {
new JFrameTesting();
}
public JFrameTesting() throws URISyntaxException {
JFrame frame = new JFrame("My first JFrame!");
try {
test = ImageIO.read(new File(getClass().getResource("test.png").toURI()));
image.setImage(test);
} catch (IOException ex) {
Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
}
JLabel label = new JLabel();
label.setIcon(image);
frame.add(label);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
另一个注意事项是,您正在扩展JFrame,但也在类中创建一个新的JFrame。您可以删除额外的JFrame和所有“框架”。该类本身是一个JFrame,因此您不需要额外的一个。
JPanel pane = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 200, 200, null);
}
};
frame.add(pane);
另外,我相信ImageIO.read(...)方法可以将URI作为参数,因此您不必从中创建文件。
答案 1 :(得分:0)
我的代码绘制图像,但需要重新绘制。为此,您需要使用鼠标更改框架的大小。
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class JFrameTesting extends JFrame {
BufferedImage test = null;
public static void main(String[] args) throws URISyntaxException {
new JFrameTesting();
}
public JFrameTesting() throws URISyntaxException {
JFrame frame = new JFrame("My first JFrame!");
frame.setSize(400, 400);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
System.out.println("init");
test = ImageIO.read(new File(getClass().getResource("test.png").toURI()));
System.out.println(test);
} catch (IOException ex) {
Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
}
final JPanel pane = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
System.out.println("paint");
super.paintComponent(g);
g.drawImage(test, 0, 0, null);
}
};
frame.add(pane);
frame.repaint();
}
@Override
public void paint(Graphics g) {
super.paint(g);
System.out.println("paint");
g.drawImage(test, 200, 200, null);
}
}
答案 2 :(得分:0)
您可以尝试使用此代码。 您需要在添加图像时在Jframe上加载JLabel。
BufferedImage test = null;
public static void main(String[] args) throws URISyntaxException {
new JFrameTesting();
}
public JFrameTesting() throws URISyntaxException {
JFrame frame = new JFrame("My first JFrame!");
JLabel label = new JLabel();
frame.setSize(800, 800);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
test = ImageIO.read(new File(getClass().getResource("test.png").toURI()));
frame.add( new JLabel(new ImageIcon(test)),BorderLayout.CENTER);
frame.setIconImage(test);
frame.setVisible(true);
label.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(JFrameTesting.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(test, 200, 200, null);
}
}