import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dialog; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JWindow; import javax.swing.filechooser.FileSystemView; /** * */ /** * Display program for viewing the input data to FindPoly * * @author zeil * */ public class ShowPoly { public class LineSegment { public Point p1; public Point p2; public LineSegment (Point p1, Point p2) { this.p1 = p1; this.p2 = p2; } } ArrayList lineSegments; public ShowPoly() { } final void solve (BufferedReader bin) throws IOException { lineSegments = new ArrayList<>(); String line = bin.readLine(); while (line != null) { String[] segmentParts = line.split(";"); for (String segmentStr: segmentParts) { if (segmentStr.length() > 0) { segmentStr = segmentStr.replaceAll("[(,)]", " "); Scanner in = new Scanner(segmentStr); int x1 = in.nextInt(); int y1 = in.nextInt(); int x2 = in.nextInt(); int y2 = in.nextInt(); lineSegments.add(new LineSegment(new Point(x1,y1), new Point(x2,y2))); in.close(); } } line = bin.readLine(); } displayPlot(); } static int zoom = 4; JScrollPane scrolledPlot; JPanel plot; private void displayPlot() { JWindow win = null; JDialog dialog = new JDialog(win,"data set", Dialog.ModalityType.DOCUMENT_MODAL); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); Container content = dialog.getContentPane(); plot = new JPanel() { @Override public void paint (Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(2)); for (LineSegment w: lineSegments) { Color c = Color.black; g2.setColor(c); g2.drawLine(5 + zoom*w.p1.x, 5 + zoom*(100-w.p1.y), 5 + zoom*w.p2.x, 5 + zoom*(100-w.p2.y)); } } }; plot.setBackground(Color.white); plot.setPreferredSize(new Dimension(10 + 1000*zoom/256, 10 + 1000*zoom/256)); content.setLayout(new BorderLayout()); scrolledPlot = new JScrollPane(plot, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); content.add (scrolledPlot, BorderLayout.CENTER); JPanel zoomPanel = new JPanel(); content.add (zoomPanel, BorderLayout.SOUTH); JButton zoomIn = new JButton ("Zoom in"); JButton zoomOut = new JButton ("Zoom out"); zoomIn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { zoom = 2 * zoom; plot.setPreferredSize(new Dimension(10 + 1000*zoom/256, 10 + 1000*zoom/256)); plot.revalidate(); plot.repaint(); } }); zoomOut.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { zoom = Math.max(1, zoom / 2); plot.setPreferredSize(new Dimension(10 + 1000*zoom/256, 10 + 1000*zoom/256)); plot.revalidate(); plot.repaint(); } }); zoomPanel.add (zoomIn); zoomPanel.add (zoomOut); dialog.setMinimumSize(new Dimension(500, 500)); dialog.setVisible(true); } /** * @param args * @throws IOException */ public static void main (String[] argv) throws IOException { BufferedReader in; if (argv.length > 0) { in = new BufferedReader(new FileReader(argv[0])); } else { JFileChooser jfc = new JFileChooser(new File("."), FileSystemView.getFileSystemView()); jfc.showOpenDialog(null); File selectedFile = jfc.getSelectedFile(); in = new BufferedReader (new FileReader(selectedFile)); } new ShowPoly().solve (in); } }