package me.zacharias.star.map; import org.json.JSONObject; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; public class Main extends JPanel { public static void main(String[] args) throws IOException { new Main(); } String word; JSONObject starMap; public Main() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Input word:"); //word = in.readLine(); word = "This is a long test message that might break. let's hope it don't!"; BufferedReader starMapIn = new BufferedReader(new FileReader("./map.json")); String tmp = ""; StringBuilder builder = new StringBuilder(); while((tmp = starMapIn.readLine()) != null){ builder.append(tmp); } starMap = new JSONObject(builder.toString()); word = word.toUpperCase().replaceAll("[^A-Z\\ ]", "").trim(); JFrame frame = new JFrame(); frame.add(this); frame.setSize(600,400); frame.setVisible(true); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } Stroke big = new BasicStroke(2); Stroke normal = new BasicStroke(1); int starSize = 6; int starOffset = starSize/2; int size = 20; int endX; int endY; @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; int x = 40, y = 40; for(char c : word.toCharArray()){ JSONObject constellation = starMap.getJSONObject(String.valueOf(Character.toUpperCase(c))); for(Object part : constellation.getJSONArray("parts")){ if(part instanceof JSONObject jsonObject){ int x1 = x + size * jsonObject.getInt("x"); int y1 = y + size * jsonObject.getInt("y"); g2d.fillOval(x1 -starOffset, y1 -starOffset,starSize,starSize); for(Object lineObj : jsonObject.getJSONArray("links")){ if(lineObj instanceof JSONObject line){ g2d.setStroke(big); int x2 = x + size * line.getInt("x"); int y2 = y + size * line.getInt("y"); g2d.drawLine(x1, y1, x2, y2); g2d.setStroke(normal); } } } } g2d.setColor(new Color(178, 164, 0)); int startX = x + size * constellation.getJSONObject("start").getInt("x"); int startY = y + size * constellation.getJSONObject("start").getInt("y"); g2d.fillOval(startX-starOffset, startY-starOffset,starSize,starSize); int endX = x + size * constellation.getJSONObject("end").getInt("x"); int endY = y + size * constellation.getJSONObject("end").getInt("y"); g2d.fillOval(endX-starOffset, endY-starOffset,starSize,starSize); g2d.setColor(new Color(0,0,0)); boolean b = (int) (Math.random() * 100) % 2 == 0; x = (b ?endX:startX); y = (b ?endY:startY); } endX = x; endY = y; createImage(); } private void createImage(){ BufferedImage image = new BufferedImage(endX, endY, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = image.createGraphics(); g2d.setColor(new Color(0,0,0)); int x = 40, y = 40; for(char c : word.toCharArray()){ JSONObject constellation = starMap.getJSONObject(String.valueOf(Character.toUpperCase(c))); for(Object part : constellation.getJSONArray("parts")){ if(part instanceof JSONObject jsonObject){ int x1 = x + size * jsonObject.getInt("x"); int y1 = y + size * jsonObject.getInt("y"); g2d.fillOval(x1 -starOffset, y1 -starOffset,starSize,starSize); for(Object lineObj : jsonObject.getJSONArray("links")){ if(lineObj instanceof JSONObject line){ g2d.setStroke(big); int x2 = x + size * line.getInt("x"); int y2 = y + size * line.getInt("y"); g2d.drawLine(x1, y1, x2, y2); g2d.setStroke(normal); } } } } g2d.setColor(new Color(178, 164, 0)); int startX = x + size * constellation.getJSONObject("start").getInt("x"); int startY = y + size * constellation.getJSONObject("start").getInt("y"); g2d.fillOval(startX-starOffset, startY-starOffset,starSize,starSize); int endX = x + size * constellation.getJSONObject("end").getInt("x"); int endY = y + size * constellation.getJSONObject("end").getInt("y"); g2d.fillOval(endX-starOffset, endY-starOffset,starSize,starSize); g2d.setColor(new Color(0,0,0)); boolean b = (int) (Math.random() * 0) % 2 == 0; x = (b ?endX:startX); y = (b ?endY:startY); } try { ImageIO.write(image, "png", new File("./starMap.png")); } catch (IOException e) { throw new RuntimeException(e); } } }