Witam,
Mam pewien problem, napisałem prosty program, który ma wyświetlić obraz, lecz program się kompiluje a obrazu nie wyświetla. tu jest kod oraz komunikat, który wyświetla mi się w konsoli.
import javax.swing.*;
public class Main {
public static void main(String[] args){
Frame frame = new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import javax.swing.*;
public class Frame extends JFrame {
public Frame()
{
setSize(800, 600);
setTitle("Obraz");
MyComponent panel = new MyComponent();
add(panel);
}
}
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
public class MyComponent extends JPanel {
public MyComponent()
{
setPreferredSize(new Dimension(800,600));
setBackground(Color.BLUE);
try{
img = ImageIO.read(new File("Koala.jpg"));
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img, 205, 105, 100, 50, null);
Graphics2D g2 = (Graphics2D )g;
g2.setPaint(Color.RED);
Font font = new Font("Serif", Font.ITALIC, 60);
g2.setFont(font);
String message = "Obrazy";
g2.drawString(message, 300, 100);
g2.setPaint(Color.BLACK);
Rectangle2D rec = new Rectangle2D.Double(200,150, 400,400);
g2.fill(rec);
}
private Image img;
}
Z góry dzięki :)