// LineChart.java // Draw co-ordinates as a line graph ... can also take mouse click contributions // RJM Programming // November, 2014 import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.*; import java.util.ArrayList; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; class LineChart extends JPanel implements ActionListener, MouseListener { private final Timer timer = new Timer(20, this); // Create a timer that will go off every 20 ms int prevX = -1, prevY = -1; int xoffset = 0, yoffset = 0; int minx=0, midy, miny=0, maxx=0, midx, maxy=0, num = 0; double xscale = 1.0, yscale = 1.0; ArrayList coord = new ArrayList(); // You might want to try frame.setResizable(false) if you want your frame // and your panel to stay the same size. private final Dimension prefPanelSize = new Dimension(450, 450); public LineChart(String insis){ super(); // Call the constructor of JPanel, the class this subclasses. if (insis == "") insis = JOptionPane.showInputDialog("Please enter your comma separated co-ordinate list, or later you can click points on your line graph: "); coordize(insis); //JButton button =new JButton("press"); //this.add(button); this.setSize(prefPanelSize); addMouseListener(this); timer.start(); // Start the timer } public void coordize(String ins) { int inum = 0; if (ins.length() != 0) { String scoord[] = ins.split(","); for (String s:scoord) { if (s != "") { coord.add(Integer.parseInt(s)); if (inum == 0) { minx = coord.get(inum); maxx = coord.get(inum); } else if (inum == 1) { miny = coord.get(inum); maxy = coord.get(inum); } else if ((inum % 2) == 0) { if (minx > coord.get(inum)) minx = coord.get(inum); if (maxx < coord.get(inum)) maxx = coord.get(inum); } else { if (miny > coord.get(inum)) miny = coord.get(inum); if (maxy < coord.get(inum)) maxy = coord.get(inum); } inum = inum + 1; } } midx = (maxx - minx) / 2; midy = (maxy - miny) / 2; if (minx >= 0 && maxx <= 600 && miny >= 0 && maxy <= 600) { xscale = 1.0; yscale = 1.0; } else { if ((maxx - minx) > 600) { xoffset = (minx / 50) * 50; xscale = (maxx - xoffset) / 600.0; } else if (maxx > 600) { xoffset = (minx / 50) * 50; xscale = 1.0; } if ((maxy - miny) > 600) { yoffset = (miny / 50) * 50; yscale = (maxy - yoffset) / 600.0; } else if (maxy > 600) { yoffset = (miny / 50) * 50; yscale = 1.0; } } } } @Override public void paintComponent(Graphics g) { int x1, x2, y1, y2, isokay; long xx1, yy1; super.paintComponent(g); // fixes the immediate problem. Graphics2D g2 = (Graphics2D) g; // Axis annotation g2.setColor(Color.blue); g2.drawString("X", (650 / 2), 685); g2.drawString("Y", 2, (650 / 2)); // Numbering legends xx1 = xoffset; yy1 = yoffset; g2.setColor(Color.magenta); for (int x = 50; x <= 650; x = x + 50) { g2.drawString("" + xx1, x, 672); xx1 += 50 * Math.round(xscale); } for (int y = 650; y >= 50; y = y - 50) { g2.drawString("" + yy1, 5, y); yy1 += 50 * Math.round(yscale); } // Lines of 10 pixels g2.setColor(Color.lightGray); for (int x = 50; x <= 650; x = x + 10) { g2.drawLine(x, 50, x, 650); } for (int y = 50; y <= 650; y = y + 10) { g2.drawLine(50, y, 650, y); } // Lines of 50 pixels g2.setColor(Color.black); for (int x = 50; x <= 650; x = x + 50) { g2.drawLine(x, 50, x, 650); } for (int y = 50; y <= 650; y = y + 50) { g2.drawLine(50, y, 650, y); } g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); isokay = 1; g2.setColor(Color.red); for (int ij = 3; ij < coord.size(); ij = ij + 2) { if (coord.get(ij - 1) < 0 && coord.get(ij - 0) < 0) { x2 = Math.abs(coord.get(ij - 1)); // mouse y2 = Math.abs(coord.get(ij - 0)); isokay = 2; } else { // supplied x2 = 50 + Math.round(((xoffset * 0 + coord.get(ij - 1)) - xoffset) / Math.round(xscale)); y2 = 650 - Math.round(((yoffset * 0 + coord.get(ij)) - yoffset) / Math.round(yscale)); } if (coord.get(ij - 3) < 0 && coord.get(ij - 2) < 0) { g2.setColor(Color.orange); x1 = Math.abs(coord.get(ij - 3)); // mouse y1 = Math.abs(coord.get(ij - 2)); } else { // supplied if (isokay == 2) isokay = 0; x1 = 50 + Math.round(((xoffset * 0 + coord.get(ij - 3)) - xoffset) / Math.round(xscale)); y1 = 650 - Math.round(((yoffset * 0 + coord.get(ij - 2)) - yoffset) / Math.round(yscale)); } if (isokay != 0) { g2.drawLine(x1, y1, x2, y2); g2.drawLine(x1, y1, x2, y2); g2.drawLine(x1, y1 - 1, x2, y2 - 1); g2.drawLine(x1, y1 + 1, x2, y2 + 1); g2.drawLine(x1 - 1, y1, x2 - 1, y2); g2.drawLine(x1 + 1, y1, x2 + 1, y2); } } } @Override public Dimension getPreferredSize() { return prefPanelSize; } public static void main(String []args){ //cstring = args[0]; LineChart s; if (args.length >= 1) { s = new LineChart(args[0]); } else { s = new LineChart(""); } JFrame frame = new JFrame(); frame.setTitle("Line Graph"); frame.setVisible(true); frame.setSize(new Dimension(1050, 1050)); frame.add(s); frame.setResizable(false); } // This method is called ever 20 ms because of the timer. @Override public void actionPerformed(ActionEvent e) { } public void mousePressed(MouseEvent e) { int xx1, yy1; saySomething("Mouse pressed; # of clicks: " + e.getClickCount() + " " + e.getX() + "," + e.getY(), e); if (prevX == -1 && prevY == -1) { prevX = e.getX(); prevY = e.getY(); xx1 = (prevX - 50); xx1 *= Math.round(xscale); xx1 += xoffset; yy1 = (prevY + 650); yy1 *= Math.round(yscale); yy1 += yoffset; coord.add(-prevX); coord.add(-prevY); } else { xx1 = (e.getX() - 50); xx1 *= Math.round(xscale); xx1 += xoffset; yy1 = (e.getY() + 650); yy1 *= Math.round(yscale); yy1 += yoffset; coord.add(-e.getX()); coord.add(-e.getY()); prevX = e.getX(); prevY = e.getY(); repaint(); } } public void mouseReleased(MouseEvent e) { saySomething("Mouse released; # of clicks: " + e.getClickCount(), e); } public void mouseEntered(MouseEvent e) { saySomething("Mouse entered", e); } public void mouseExited(MouseEvent e) { saySomething("Mouse exited", e); } public void mouseClicked(MouseEvent e) { saySomething("Mouse clicked (# of clicks: " + e.getClickCount() + ")", e); } void saySomething(String eventDescription, MouseEvent e) { if (1 == 2) { String yourInfo = JOptionPane.showInputDialog(eventDescription + " detected on " + e.getComponent().getClass().getName() + "."); } } }