Skip to content

Commit

Permalink
Scaling images on small screens, small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
LabunskyA committed Jan 17, 2015
1 parent 7853246 commit 28dc5dc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 55 deletions.
48 changes: 27 additions & 21 deletions Sources/Listeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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) {
Expand All @@ -118,19 +119,20 @@ 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: ");
JTextField height = new JTextField(" Height: ");

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");

Expand Down Expand Up @@ -159,7 +161,7 @@ public void warn() {
Window.useTag = false;
} else {
Window.useTag = true;
Window.tag = tag.getText();
Window.tag = tag.getText().toLowerCase();
}
}
};
Expand All @@ -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);
}
}
};
Expand All @@ -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()));
}
}
};
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions Sources/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
43 changes: 23 additions & 20 deletions Sources/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand Down
32 changes: 20 additions & 12 deletions Sources/Zerochan.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@
* 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) {
e.printStackTrace();
}
}

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");
Expand All @@ -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();

Expand All @@ -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("<ul id=\"tags\">"));
return result.substring(0, result.indexOf("/ul")).toLowerCase();
}

return stringBuffer.toString().toLowerCase();
}
}
Binary file modified Sources/resources/Nya.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 28dc5dc

Please sign in to comment.