Skip to content

Commit

Permalink
Merge branch 'master' into geila/update-weather-info
Browse files Browse the repository at this point in the history
  • Loading branch information
geilala authored May 29, 2024
2 parents f050e0a + 24c6f9d commit b175375
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "WeatherIsland",
"request": "launch",
"mainClass": "edu.project.components.WeatherIsland",
"projectName": "weatherforecast"
},
{
"type": "java",
"name": "Main",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/project/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private static boolean isInternetAvailable() {
socket.connect(new InetSocketAddress("8.8.8.8", 53), 1500);
return true;
} catch (Exception e) {
return false;
return true;
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/edu/project/components/WeatherInfo.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package edu.project.components;

import java.awt.Font;

import java.text.SimpleDateFormat;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import edu.project.Context;

Expand All @@ -16,6 +18,7 @@ public class WeatherInfo extends JPanel {
public WeatherInfo(Context context) {
setLayout(new BorderLayout());
setOpaque(false);
setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
Expand Down
35 changes: 30 additions & 5 deletions src/main/java/edu/project/components/WeatherIsland.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,58 @@
package edu.project.components;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import com.formdev.flatlaf.FlatClientProperties;

import edu.project.Context;
import java.awt.BorderLayout;

import edu.project.components.weathercards.WeatherGroup;

public class WeatherIsland extends JPanel {
public WeatherIsland(Context context) {
putClientProperty(FlatClientProperties.STYLE,
"background: tint(@background,50%); border: 16,16,16,16,shade(@background,10%),,16");
"background: tint(@background,50%); border: 8,8,8,8,shade(@background,10%),,16");
setOpaque(false);

setLayout(new BorderLayout());
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

WeatherInfo weatherInfo = new WeatherInfo(context);
add(weatherInfo, BorderLayout.CENTER);

// Maximize Width and Minimize Height
weatherInfo.setMaximumSize(
new Dimension(
weatherInfo.getMaximumSize().width,
weatherInfo.getPreferredSize().height));

add(weatherInfo);

add(Box.createVerticalStrut(8));

WeatherGroup weatherGroup = new WeatherGroup();

// Maximize Width and Minimize Height
weatherGroup.setMaximumSize(
new Dimension(
weatherGroup.getMaximumSize().width,
weatherGroup.getPreferredSize().height));

add(weatherGroup);

}

@Override
protected void paintComponent(Graphics graphics) {
Graphics2D graphics2d = (Graphics2D) graphics;
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2d.setColor(getBackground());
graphics2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16 + 12, 16 + 12);
graphics2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);
super.paintComponent(graphics2d);
}
}
60 changes: 60 additions & 0 deletions src/main/java/edu/project/components/weathercards/WeatherCard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package edu.project.components.weathercards;

import com.formdev.flatlaf.FlatClientProperties;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.RenderingHints;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public abstract class WeatherCard extends JPanel {
public WeatherCard(String title, String value, String unit) {
putClientProperty(FlatClientProperties.STYLE,
"border: 6,4,6,4,shade(@background,10%),,16");

setOpaque(false);

setLayout(new GridBagLayout());
// setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// setMaximumSize(new Dimension(100, 150));
// setPreferredSize(new Dimension(100, 150));

JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.PLAIN, 14));

JLabel valueLabel = new JLabel(value, SwingConstants.CENTER);
valueLabel.setFont(new Font("Arial", Font.BOLD, 32));

JLabel unitLabel = new JLabel(unit, SwingConstants.CENTER);
unitLabel.setFont(new Font("Arial", Font.PLAIN, 14));

GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(3, 3, 3, 3);
add(titleLabel, constraints);

constraints.gridy++;
add(valueLabel, constraints);

constraints.gridy++;
add(unitLabel, constraints);
}

@Override
protected void paintComponent(Graphics graphics) {
Graphics2D graphics2d = (Graphics2D) graphics;
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2d.setColor(Color.LIGHT_GRAY);
graphics2d.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);
super.paintComponent(graphics);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.project.components.weathercards;

import java.awt.GridLayout;

import javax.swing.JPanel;

public class WeatherGroup extends JPanel {
public WeatherGroup() {
setOpaque(false);
setLayout(new GridLayout(1, 4, 8, 0));
add(new WeatherWindCard("16"));
add(new WeatherHumidityCard("50"));
add(new WeatherRainRateCard("0"));
add(new WeatherUVCard("5"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.project.components.weathercards;

public class WeatherHumidityCard extends WeatherCard {
public WeatherHumidityCard(String value) {
super("Humidity", value, "%");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.project.components.weathercards;

public class WeatherRainRateCard extends WeatherCard {
public WeatherRainRateCard(String value) {
super("Rain Rate", value, "in/h");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.project.components.weathercards;

public class WeatherUVCard extends WeatherCard {
public WeatherUVCard(String value) {
super("UV Index", value, "Moderate");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.project.components.weathercards;

public class WeatherWindCard extends WeatherCard {
public WeatherWindCard(String value) {
super("Wind", value, "mph");
}
}

0 comments on commit b175375

Please sign in to comment.