Skip to content

Instantly share code, notes, and snippets.

@hibiyasleep
Last active August 29, 2015 14:10
Show Gist options
  • Save hibiyasleep/8290c20c5d3897714f4e to your computer and use it in GitHub Desktop.
Save hibiyasleep/8290c20c5d3897714f4e to your computer and use it in GitHub Desktop.
MIT
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Hangang extends JFrame {
public static String uri = "http://shizuku.hibiya.moe/hangang";
Hangang(){
//JFrame
JFrame frame = new JFrame("한강가자 for Java");
frame.setSize(400,300);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setContentPane(new JLabel(new ImageIcon("./background.png")));
frame.setLayout(new BorderLayout());
//변수
JPanel button_pan = new JPanel();
JPanel text_pan = new JPanel();
JButton refresh_btn;
JButton exit_btn;
ImageIcon refresh_img;
ImageIcon exit_img;
JLabel txt = new JLabel("지금 한강은...", SwingConstants.CENTER);
JLabel wt = new JLabel("loading", SwingConstants.CENTER);
//버튼 출력
button_pan.setOpaque(false);
button_pan.setLayout(new FlowLayout(FlowLayout.RIGHT));
refresh_img = new ImageIcon("./btn_refresh.png");
refresh_btn = new JButton(refresh_img);
refresh_btn.setPreferredSize(new Dimension(16,16));
exit_img = new ImageIcon("./btn_exit.png");
exit_btn = new JButton(exit_img);
exit_btn.setOpaque(true);
exit_btn.setPreferredSize(new Dimension(16,16));
button_pan.add(refresh_btn);
button_pan.add(exit_btn);
//텍스트 창 출력
text_pan.setOpaque(false);
text_pan.setLayout(new GridLayout(3,1));
txt.setFont(new Font("Malgun Gothic", Font.PLAIN, 18));
wt.setFont(new Font("Segoe UI", Font.PLAIN, 60));
text_pan.add(txt);
text_pan.add(wt);
//버튼 리스너
class btnListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JButton btn = (JButton)e.getSource();
if(btn == refresh_btn){
wt.setText(fetchTemp());
} else if(btn == exit_btn){
System.exit(0);
}
}
}
//버튼 리스너 추가
refresh_btn.addActionListener(new btnListener());
exit_btn.addActionListener(new btnListener());
//JFrame 출력
frame.add(button_pan, BorderLayout.NORTH);
frame.add(text_pan, BorderLayout.CENTER);
frame.add(new JLabel(new ImageIcon("./empty.png")), BorderLayout.SOUTH);
frame.setVisible(true);
//새로고침 함수 호출
wt.setText(fetchTemp());
}
public String fetchTemp() {
// Pilyohan Objects Sangseong
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String r = "";
try {
url = new URL(uri);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(
new InputStreamReader(
conn.getInputStream()
)
);
while ((line = rd.readLine()) != null){
r += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return r;
}
public static void main(String[] args) {
new Hangang();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment