diff --git a/Sources/Listeners.java b/Sources/Listeners.java index edd9bc4..7d4cdcf 100644 --- a/Sources/Listeners.java +++ b/Sources/Listeners.java @@ -23,6 +23,7 @@ public void actionPerformed(ActionEvent e) { ObjectInputStream objectIn = new ObjectInputStream(fileIn); lastSave = (File) objectIn.readObject(); fileIn.close(); + firstTime = false; } } catch (IOException ignored) { } catch (ClassNotFoundException e2){ @@ -108,8 +109,8 @@ public void actionPerformed(ActionEvent e) { } class Settings implements ActionListener{ - Integer positionX = 0; - Integer positionY = 0; + private Integer positionX = 0; + private Integer positionY = 0; @Override public void actionPerformed(ActionEvent e) { @@ -118,8 +119,8 @@ public void actionPerformed(ActionEvent e) { final JFrame settingsDialog = new JFrame("Settings"); final JTextField tag = new JTextField(Window.tag); - final JTextField widthScan = new JTextField(Integer.toString((int) Window.customResolution.getWidth())); - final JTextField heightScan = new JTextField(Integer.toString((int) Window.customResolution.getHeight())); + final JTextField widthScan = new JTextField(Integer.toString(Window.customResolution.width)); + final JTextField heightScan = new JTextField(Integer.toString(Window.customResolution.height)); JTextField tagPointer = new JTextField(" Tag (only one, could works slowly): "); JTextField width = new JTextField(" Width: "); @@ -127,10 +128,11 @@ public void actionPerformed(ActionEvent e) { JPanel mainPanel = new JPanel(); JPanel hdCheckBoxPanel = new JPanel(); - JPanel smallButtonsPanel = new JPanel(); + JPanel buttonsPanel = new JPanel(); JPanel tagPanel = new JPanel(); JPanel sizePanel = new JPanel(); - JPanel buttonPanel = new JPanel(); + JPanel prevNyaPanel = new JPanel(); + JPanel closePanel = new JPanel(); Checkbox hdCheckBox = new Checkbox("HD nya only"); @@ -159,7 +161,7 @@ public void warn() { Window.useTag = false; } else { Window.useTag = true; - Window.tag = tag.getText(); + Window.tag = tag.getText().toLowerCase(); } } }; @@ -174,10 +176,10 @@ public void warn() { public void warn() { if (widthScan.getText().length() == 0) { Window.currentResolution = false; - Window.customResolution = new Dimension(0, (int) Window.customResolution.getHeight()); + Window.customResolution = new Dimension(0, Window.customResolution.height); } else { Window.currentResolution = true; - Window.customResolution = new Dimension(Integer.valueOf(widthScan.getText()), (int) Window.customResolution.getHeight()); + Window.customResolution = new Dimension(Integer.valueOf(widthScan.getText()), Window.customResolution.height); } } }; @@ -192,10 +194,10 @@ public void warn() { public void warn() { if (heightScan.getText().length() == 0) { Window.currentResolution = false; - Window.customResolution = new Dimension((int) Window.customResolution.getWidth(), 0); + Window.customResolution = new Dimension(Window.customResolution.width, 0); } else { Window.currentResolution = true; - Window.customResolution = new Dimension((int) Window.customResolution.getWidth(), Integer.valueOf(heightScan.getText())); + Window.customResolution = new Dimension(Window.customResolution.width, Integer.valueOf(heightScan.getText())); } } }; @@ -252,19 +254,24 @@ public void itemStateChanged(ItemEvent e) { sizePanel.add(height); sizePanel.add(heightScan); - smallButtonsPanel.setLayout(new BoxLayout(smallButtonsPanel, BoxLayout.X_AXIS)); - smallButtonsPanel.add(Box.createRigidArea(new Dimension(277, 0))); - smallButtonsPanel.add(closeButton); - smallButtonsPanel.setBackground(Color.WHITE); - tagPanel.setLayout(new FlowLayout()); tagPanel.setBackground(Color.WHITE); tagPanel.add(tagPointer); tagPanel.add(tag); - buttonPanel.setLayout(new FlowLayout()); - buttonPanel.setBackground(Color.WHITE); - buttonPanel.add(prevNya); + prevNyaPanel.setLayout(new FlowLayout()); + prevNyaPanel.setBackground(Color.WHITE); + prevNyaPanel.add(prevNya); + + closePanel.setLayout(new BoxLayout(closePanel, BoxLayout.Y_AXIS)); + closePanel.setBackground(Color.WHITE); + closePanel.add(Box.createRigidArea(new Dimension(0, 10))); + closePanel.add(closeButton); + + buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); + buttonsPanel.add(prevNyaPanel); + buttonsPanel.add(closePanel); + buttonsPanel.setBackground(Color.WHITE); tagPointer.setBackground(Color.WHITE); tagPointer.setDisabledTextColor(Color.BLACK); @@ -283,8 +290,7 @@ public void itemStateChanged(ItemEvent e) { mainPanel.add(hdCheckBoxPanel); mainPanel.add(tagPanel); mainPanel.add(sizePanel); - mainPanel.add(buttonPanel); - mainPanel.add(smallButtonsPanel); + mainPanel.add(buttonsPanel); settingsDialog.add(mainPanel); settingsDialog.setUndecorated(true); diff --git a/Sources/Solution.java b/Sources/Solution.java index 7836f9e..40f3367 100644 --- a/Sources/Solution.java +++ b/Sources/Solution.java @@ -4,8 +4,8 @@ /** * Created by Lina on 21.11.2014. */ -public class Solution { - static protected Window getNya = new Window(); +class Solution { + static final Window getNya = new Window(); public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); diff --git a/Sources/Window.java b/Sources/Window.java index 7c99adb..29cc467 100644 --- a/Sources/Window.java +++ b/Sources/Window.java @@ -4,6 +4,7 @@ import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.image.BufferedImage; +import java.awt.image.ImageObserver; import java.io.IOException; import java.net.MalformedURLException; @@ -19,24 +20,24 @@ public class Window extends JFrame { static String tag = ""; static Integer positionX; static Integer positionY; - static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); static Dimension customResolution = new Dimension(0, 0); + static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); private BufferedImage bufferedNyaImage; private BufferedImage bufferedFullImage; - private JTextField dataField; private JLabel nyaLabel = new JLabel(); - private JPanel buttonsPanel = new JPanel(); - private SimpleButton getNya; - private Component northRigidArea = Box.createRigidArea(new Dimension(0, 0)); - private Component buttonsPanelFirstRigidArea = Box.createRigidArea(new Dimension(0, 0)); - private Component buttonsPaneSecondRigidArea = Box.createRigidArea(new Dimension(0, 0)); + private final JTextField dataField; + private final JPanel buttonsPanel = new JPanel(); + private final SimpleButton getNya; + private final Component northRigidArea = Box.createRigidArea(new Dimension(0, 0)); + private final Component buttonsPanelFirstRigidArea = Box.createRigidArea(new Dimension(0, 0)); + private final Component buttonsPaneSecondRigidArea = Box.createRigidArea(new Dimension(0, 0)); BufferedImage getBufferedFullImage() { return bufferedFullImage; } - protected Window() { + Window() { super("getNya"); try { @@ -131,20 +132,20 @@ protected Window() { pack(); } - protected void drawNya() throws MalformedURLException { + void drawNya() throws MalformedURLException { setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); Zerochan.generateNumberNya(); Zerochan.generateURLs(); LittleParser littleParser = new LittleParser(); if (useTag) - if (littleParser.parse("http://www.zerochan.net/" + Zerochan.numberNya).contains(tag)) + if (littleParser.parse("http://www.zerochan.net/" + Zerochan.numberNya, LittleParser.TAG).contains(tag)) draw(); else drawNya(); else draw(); } - protected void draw() throws MalformedURLException { + void draw() throws MalformedURLException { try { //sometimes Zerochan.net blocks some pictures, so I need to handle this occasion and get another url with another picture bufferedFullImage = ImageIO.read(Zerochan.fullURL); Integer nyaFullHeight = bufferedFullImage.getHeight(); @@ -162,17 +163,20 @@ protected void draw() throws MalformedURLException { Dimension maximumSizeForTheFistArea = new Dimension((nyaImageWidth - 204) / 2 - 16, 48); Dimension maximumSizeForTheSecondArea = new Dimension((nyaImageWidth - 204) / 2 - 48, 48); Dimension minimumWindowSize = new Dimension(274, 0); //just buttons size - Integer maxContentPaneHeight = nyaImageHeight + 48 + dataField.getHeight() + Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration()).bottom; + Integer maxContentPaneHeight = nyaImageHeight + 48 + dataField.getHeight(); //this on is for small screens, less then 720p - if (screenSize.getHeight() < maxContentPaneHeight || screenSize.getWidth() < nyaImageWidth) - drawNya(); - else + if (screenSize.height < maxContentPaneHeight || screenSize.width < nyaImageWidth) { + Image scaledImage = bufferedNyaImage.getScaledInstance(screenSize.height - 48 - Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration()).bottom, -1, Image.SCALE_FAST); + nyaLabel.setIcon(new ImageIcon(scaledImage)); + nyaImageHeight = ((BufferedImage) scaledImage).getHeight(); + nyaImageWidth = ((BufferedImage) scaledImage).getWidth(); + } else nyaLabel.setIcon(new ImageIcon(bufferedNyaImage)); dataField.setText("Width: " + nyaFullWidth + " Height: " + nyaFullHeight); dataField.setMaximumSize(new Dimension(nyaImageWidth, dataField.getHeight())); - northRigidArea.setMaximumSize(new Dimension(nyaImageWidth, ((int) screenSize.getHeight() - maxContentPaneHeight) / 2)); + northRigidArea.setMaximumSize(new Dimension(nyaImageWidth, (screenSize.height - maxContentPaneHeight) / 2)); buttonsPanelFirstRigidArea.setMaximumSize(maximumSizeForTheFistArea); buttonsPaneSecondRigidArea.setMaximumSize(maximumSizeForTheSecondArea); setMinimumSize(minimumWindowSize); @@ -191,15 +195,14 @@ protected void draw() throws MalformedURLException { } } catch (IOException e) { drawNya(); //get new image, if there is something wrong with this one - } catch (AWTException ignored) { - } //never uses + } catch (AWTException ignored) {} } - protected void setWindowSizeNormal(Boolean normal) { //I need it to resize window every time when it changes state from maximized to normal + void setWindowSizeNormal(Boolean normal) { //I need it to resize window every time when it changes state from maximized to normal if (normal) setSize(bufferedNyaImage.getWidth(), bufferedNyaImage.getHeight() + buttonsPanel.getHeight() + dataField.getHeight()); else{//if maximized - Dimension maximumSizeForTheFistArea = new Dimension(((int) screenSize.getWidth() - 223)/2, 48); + Dimension maximumSizeForTheFistArea = new Dimension((screenSize.width - 223)/2, 48); Dimension maximumSizeForTheSecondArea = new Dimension((int) maximumSizeForTheFistArea.getWidth() - 48, 48); buttonsPanelFirstRigidArea.setMaximumSize(maximumSizeForTheFistArea); diff --git a/Sources/Zerochan.java b/Sources/Zerochan.java index 1451349..d29c420 100644 --- a/Sources/Zerochan.java +++ b/Sources/Zerochan.java @@ -9,20 +9,20 @@ * Created by Lina on 21.11.2014. */ -public class Zerochan { - protected static Integer numberOfTheNyas; - protected static Integer numberNya; - protected static URL prevURL; - protected static URL nyaURL; - protected static URL fullURL; +class Zerochan { + private static Integer numberOfTheNyas; + static Integer numberNya; + static URL prevURL; + static URL nyaURL; + static URL fullURL; public static void getNumberOfTheNyas() { try { LittleParser littleParser = new LittleParser(); - String result = littleParser.parse("http://Zerochan.net"); + String result = littleParser.parse("http://Zerochan.net", LittleParser.MAIN_PAGE); int index = result.indexOf("240."); - String stringNumber = result.substring(index + 4, index + 10); + String stringNumber = result.substring(index + 4, index + 11); //4 is length of "240." and there is only ~1.8 million of images on zerochan, so we need only 6 digits numberOfTheNyas = Integer.parseInt(stringNumber); } catch (MalformedURLException e) { @@ -30,12 +30,12 @@ public static void getNumberOfTheNyas() { } } - protected static void generateNumberNya() { + static void generateNumberNya() { Random randomNumberNya = new Random(); numberNya = randomNumberNya.nextInt(numberOfTheNyas + 1); } - protected static void generateURLs() { + static void generateURLs() { try { prevURL = nyaURL; nyaURL = new URL("http://s1.zerochan.net/.600." + Integer.toString(numberNya) + ".jpg"); @@ -45,7 +45,10 @@ protected static void generateURLs() { } class LittleParser { - public String parse(String urlString) throws MalformedURLException { + static final char MAIN_PAGE = 0; + static final char TAG = 1; + + public String parse(String urlString, char type) throws MalformedURLException { URL url = new URL(urlString); StringBuilder stringBuffer = new StringBuilder(); @@ -65,6 +68,11 @@ public String parse(String urlString) throws MalformedURLException { return ""; } - return stringBuffer.toString(); + if (type == TAG) { + String result = stringBuffer.toString().substring(stringBuffer.toString().indexOf("