Java输入坐标和绘制线

时间:2014-10-29 20:06:58

标签: java

如果我传递像(1,1)(2,2)(3,3)这样的输入,如何在java中绘制一条线。并且点数可以变化,如(1,1)(2,2)。真的无法让它正常工作。请参阅下面的代码

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;

@SuppressWarnings("serial")
public class DrawGraph extends JPanel {
 private static final int MAX_SCORE = 20;
 private static final int PREF_W = 800;
 private static final int PREF_H = 650;
 private static final int BORDER_GAP = 30;
 private static final Color GRAPH_COLOR = Color.green;
 private static final Color GRAPH_POINT_COLOR = new Color(150, 50, 50, 180);
 private static final Stroke GRAPH_STROKE = new BasicStroke(3f);
 private static final int GRAPH_POINT_WIDTH = 12;
 private static final int Y_HATCH_CNT = 10;
 private List<Integer> scores;

 public DrawGraph(List<Integer> scores) {
  this.scores = scores;
 }

 @Override
 protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D)g;
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

  double xScale = ((double) getWidth() - 2 * BORDER_GAP) / (scores.size() - 1);
  double yScale = ((double) getHeight() - 2 * BORDER_GAP) / (MAX_SCORE - 1);

 String s ="(220 616) (220 666) (251 670) (272 647)";
 String[] arr = s.split("\\)\\s*");
 List<Point> graphPoints = new ArrayList<Point>();
 for (String anArr : arr){
    int x = Integer.parseInt(anArr.substring(1,anArr.indexOf(" ")));
    int y = Integer.parseInt(anArr.substring(anArr.indexOf(" ") + 1, anArr.length()));
    Point p = new Point(x,y);
    graphPoints.add(p);
 }
 for (int i = 0; i < graphPoints.size() - 1; i++) {
     int x1 = graphPoints.get(i).x;
     int y1 = graphPoints.get(i).y;
     int x2 = graphPoints.get(i + 1).x;
     int y2 = graphPoints.get(i + 1).y;
     g2.drawLine(x1, y1, x2, y2);         
  }
}

 @Override
public Dimension getPreferredSize() {
   return new Dimension(PREF_W, PREF_H);
}

 private static void createAndShowGui() {
  List<Integer> scores = new ArrayList<Integer>();
  Random random = new Random();
  int maxDataPoints = 16;
  int maxScore = 20;
  for (int i = 0; i < maxDataPoints ; i++) {
     scores.add(random.nextInt(maxScore));
  }
  DrawGraph mainPanel = new DrawGraph(scores);

  JFrame frame = new JFrame("DrawGraph");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(mainPanel);

  frame.pack();
  frame.setLocationByPlatform(true);
  frame.setVisible(true);
 }

public static void main(String[] args) {


  SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        createAndShowGui();
     }
  });
 }
}

1 个答案:

答案 0 :(得分:0)

这应该让你开始

请参阅下面的示例。

public void paint(Graphics g){
       g.drawLine(x1, y1, x2, y2);
       //then add the other point
       g.drawLine(x2,y2,x3,y3)
    }

现在你需要分解你的积分,并用绘制线继续将下一个点连接到前一个点。使用for循环并发布您的代码,我们将帮助您

code seems somewhat useful