Я пытаюсь добавить фоновое изображение в JFrame.
Я пробовал все возможные решения, но они не работают. Все, что я хочу сделать, - это добавить фоновое изображение в JFrame, а затем распечатать в нем фигуры, как вы можете видеть в моем коде.
Может кто поможет исправить?
public class Triangle {
Oval o;
public static void main(String[] args) {
new Triangle();
}
public Triangle() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.setSize(400, 400);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Polygon poly;
{
BufferedImage img = null;
try {
img = ImageIO.read(new File("apple.PNG"));
} catch (IOException e) {
e.printStackTrace();
}
Image dimg = img.getScaledInstance(800, 508, Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(dimg);
setContentPane(new JLabel(imageIcon));
poly = new Polygon(
new int[] { 110, 150, 50 },
new int[] { 200, 0, 0 },
3);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
private void setContentPane(JLabel jLabel) {
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.blue);
g2.fill(poly);
g2.setColor(Color.red);
g2.fillRect(0, 0, 25, 75);
g2.setColor(Color.green);
g2.fillOval(200, 100, 100, 50);
g2.setColor(Color.green);
g2.fillOval(300, 300, 250, 250);
}
}
}