Reputation: 73
I would like to draw something like this weather map:
On the top, there is a header which contains information (title, ...) and below there is the actual picture. I tried to model the program like the following but it does not show the image of the weather map.
Later I would like to be able to resize the whole weather map (title and picture).
public class Application {
public static void main(String[] args) {
Window meteoWindow = new Window();
meteoWindow.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
public class Window extends JFrame {
Container c;
WeatherMap wm1;
public Window() {
c = getContentPane();
// Loading weather maps
wm1 = new WeatherMap("http://www.link-to-image.com/weatherimage.png");
// ---
c.add(wm1);
setTitle("Meteoview Alpha");
setSize(new Dimension(1920, 1080));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
public class WeatherMap extends JPanel {
private DescriptionPanel descriptionPanel;
private ImagePanel imagePanel;
private WeatherImage weatherImage;
public WeatherMap(String urlPath) {
imagePanel = new ImagePanel();
descriptionPanel = new DescriptionPanel("Wetterkarte 1");
weatherImage = new WeatherImage("http://www.linktoweatherimage/image.png");
}
}
import javax.swing.*;
import java.awt.*;
public class DescriptionPanel extends JPanel {
private String name;
private JLabel nameLabel;
public DescriptionPanel(String name) {
this.name = name;
nameLabel = new JLabel(name, JLabel.LEFT);
setLayout(new FlowLayout());
add(nameLabel);
}
}
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
public class ImagePanel extends JPanel {
private BufferedImage image;
public ImagePanel() {
//setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
try {
image = ImageIO.read(new URL("http://www.linktoimage/image.png"));
System.out.println("Successfully read...");
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
As you can see, I tried it with a few different approaches, but nothing really works. Can you help me to model this? (JPanel on JPanel, or BufferedImage on JPanel, ...)
Upvotes: 1
Views: 51
Reputation: 324098
imagePanel = new ImagePanel();
descriptionPanel = new DescriptionPanel("Wetterkarte 1");
weatherImage = new WeatherImage("http://www.modellzentrale.de/WRF4km/12Z/15h/RR3h_eu.png");
You create 3 components, but you don't add the components to the panel.
Not really sure what you are trying to do since you attempt to read the same image twice, so I don't know why you have a "WeatherImage" and an "ImagePanel".
So I will just suggest you first try something like the following to understand how to use a panel with a layout manager.
imagePanel = new ImagePanel();
descriptionPanel = new DescriptionPanel("Wetterkarte 1");
setLayout( new BorderLayout() );
add(descriptionPanel, BorderLayout.PAGE_START);
add(imagePanel, BorderLayout.CENTER);
Also, then is no need to create a custom painting to simply paint an image at it actual size. You can just add the Image to a JLabel by using an ImageIcon:
image = ImageIO.read(…);
JLabel imageLabel = new JLabel( new ImageIcon(image) );
Now you add the label to any panel you want.
Upvotes: 2