2013-12-07 107 views
1

我得到了一個下面的java代碼,它繪製了一個2D圖,其中x軸的值爲1,2,3 ...... 20 ,y軸的值爲21,14,18 .......... 18 該代碼完美地工作,但唯一的問題是該圖不會分別在x軸和y軸上顯示相應的x和y值。我知道這可能是代碼中的一小部分。但我是新來的Java圖形,我無法找出考慮到時間限制,在哪裏添加相關的代碼。在java中繪製一個簡單的2D圖形

/*Sample code */ 

public class GraphingData extends JPanel { 

    int[] data = { 
     21, 14, 18, 03, 86, 88, 74, 87, 54, 77, 
     61, 55, 48, 60, 49, 36, 38, 27, 20, 18 
    }; 
    final int PAD = 20; 

    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D)g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
          RenderingHints.VALUE_ANTIALIAS_ON); 
     int w = getWidth(); 
     int h = getHeight(); 
     // Draw ordinate. 
     g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD)); 
     // Draw abcissa. 
     g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD)); 
     // Draw labels. 
     Font font = g2.getFont(); 
     FontRenderContext frc = g2.getFontRenderContext(); 
     LineMetrics lm = font.getLineMetrics("0", frc); 
     float sh = lm.getAscent() + lm.getDescent(); 
     // Ordinate label. 
     String s = "Average Byte Value"; 
     float sy = PAD + ((h - 2*PAD) - s.length()*sh)/2 + lm.getAscent(); 
     for(int i = 0; i < s.length(); i++) { 
      String letter = String.valueOf(s.charAt(i)); 
      float sw = (float)font.getStringBounds(letter, frc).getWidth(); 
      float sx = (PAD - sw)/2; 
      g2.drawString(letter, sx, sy); 
      sy += sh; 
     } 
     // Abcissa label. 
     s = "file blocks"; 
     sy = h - PAD + (PAD - sh)/2 + lm.getAscent(); 
     float sw = (float)font.getStringBounds(s, frc).getWidth(); 
     float sx = (w - sw)/2; 
     g2.drawString(s, sx, sy); 
     // Draw lines. 
     double xInc = (double)(w - 2*PAD)/(data.length-1); 
     double scale = (double)(h - 2*PAD)/getMax(); 
     g2.setPaint(Color.green.darker()); 
     for(int i = 0; i < data.length-1; i++) { 
      double x1 = PAD + i*xInc; 
      double y1 = h - PAD - scale*data[i]; 
      double x2 = PAD + (i+1)*xInc; 
      double y2 = h - PAD - scale*data[i+1]; 
      g2.draw(new Line2D.Double(x1, y1, x2, y2)); 
     } 
     // Mark data points. 
     g2.setPaint(Color.red); 
     for(int i = 0; i < data.length; i++) { 
      double x = PAD + i*xInc; 
      double y = h - PAD - scale*data[i]; 
      g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4)); 
     } 

    } 

    private int getMax() { 
     int max = -Integer.MAX_VALUE; 
     for(int i = 0; i < data.length; i++) { 
      if(data[i] > max) 
       max = data[i]; 
     } 
     return max; 
    } 

    public static void main(String[] args) { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(new GraphingData()); 
     f.setSize(400,400); 
     f.setLocation(200,200); 
     f.setVisible(true); 
    } 
} 
+2

*「..我沒能找出其中添加相關代碼,考慮到時間限制。」 *有什麼時間限制「? –

+0

我的意思是我有一些時間限制去通過java教程,並完全理解圖形代碼,從而找出在哪裏添加代碼。 – dsingh

+5

也許你應該離開這個任務,因爲你*有*時間。提供免費幫助的人通常希望您有時間參考他們鏈接的Java文檔和教程。 –

回答

0
public void drawGrid(Graphics g) 
{ 
    g.setColor(Color.black); 
    int rows = model.maxy-model.miny+1; 
    int cols = model.maxx-model.minx+1; 

    int colWidth = (getWidth()-paddingFromRight - ylabelWidth)/cols; 
    int rowHeight = (getHeight()-xlabelHeight-headingHeight)/rows; 

    int endX = ylabelWidth + cols * colWidth; 
    int endY = headingHeight + rows*rowHeight; 

    for (int k = 0; k <= cols; k++) 
    { 
     g.drawLine(ylabelWidth+colWidth*k, headingHeight, ylabelWidth+colWidth*k, endY); //getHeight()-xlabelHeight 
    } 

    for (int k = 0; k <= rows;k++) 
    { 
     g.drawLine(ylabelWidth, headingHeight+rowHeight*k,endX, headingHeight+rowHeight*k); 
    } 
} 

public void drawLines(Graphics g) 
{ 
    ArrayList<Line> lines = model.getLines(); 
    Random random = new Random(50); 
    for (int k = 0; k < lines.size(); k++) 
    { 
     g.setColor(new Color(255-50*k, 50*k, 2*50*k)); 
     //g.setColor(Color.red); 
     drawLine(g, lines.get(k)); 
    } 

} 

public void drawLine(Graphics g, Line line) 
{ 
    ArrayList<Point> points = line.points; 
    for (int k = 0; k<points.size()-1;k++) 
    { 
     //scale up points 
     Point p1 = points.get(k); 
     Point p2 = points.get(k+1); 
     g.drawLine(scaleUpX(p1.x), scaleUpY(p1.y), scaleUpX(p2.x), scaleUpY(p2.y)); 
     g.fillOval(scaleUpX(p1.x)-5, scaleUpY(p1.y)-5, 10, 10); 
    } 
    Point p1 = points.get(points.size()-1); 
    g.fillOval(scaleUpX(p1.x)-5, scaleUpY(p1.y)-5, 10, 10); 
} 

public int scaleUpX(int x) 
{ 
    int cols = model.maxx-model.minx+1; 
    int colWidth = (getWidth()-paddingFromRight - ylabelWidth)/cols; 

    return ylabelWidth+colWidth*x; 
} 
public int scaleUpY(int y) 
{ 
    int rows = model.maxy-model.miny+1; 
    int rowHeight = (getHeight()-xlabelHeight-headingHeight)/(rows); 
    //int endY = getHeight()-xlabelHeight; 
    int endY = headingHeight+rows*rowHeight; 

    return endY - rowHeight*y; 
} 
public void drawLabels(Graphics g) 
{ 
    Dimension d = getSize(); 
    //xlabel 
    g.drawString(model.xlabel, d.width/2, d.height-xlabelHeight/4); 
    //ylabel 
    g.drawString(model.ylabel, 0+5, d.height/2); 
    //xaxis 
    int rows = model.maxy-model.miny+1; 
    int cols = model.maxx-model.minx+1; 

    int colWidth = (getWidth()-paddingFromRight - ylabelWidth)/cols; 
    int rowHeight = (getHeight()-xlabelHeight-headingHeight)/rows; 

    for (int k = 0;k<=cols;k++) 
    { 
     g.drawString(String.valueOf(model.minx+k), ylabelWidth+k*colWidth-5, rowHeight*rows+headingHeight+15); 
    } 
    //yaxis 
    for (int k = 0;k<=rows;k++) 
    { 
     g.drawString(String.valueOf(model.maxy-k+1),ylabelWidth-15,headingHeight+k*rowHeight); 
    } 
    //heading 
    FontMetrics fm = g.getFontMetrics(); 
    g.setFont(g.getFont().deriveFont((float)25.0)); 
    g.drawString(model.title, d.width/2-fm.stringWidth(model.title), headingHeight/2+headingHeight/4); 
} 
+0

你應該擴大這個答案,而不是隻發佈代碼;它如何回答這個問題?你的方法有什麼好處/壞處? – Tetrinity