在java中绘制一个简单的2D图形

时间:2013-12-07 21:43:23

标签: java swing graphics jpanel java-2d

我得到了一个下面的java代码,它绘制了一个2D图形,其中x轴的值为1,2,3,...... 20 和y轴的值为21,14,18 .......... 18 代码完美地运行但唯一的问题是图形没有分别在x轴和y轴上显示相应的x和y值。我知道它可能是代码的一小部分。但我是java图形的新手,我无法在考虑时间限制的情况下找出添加相关代码的位置。

/*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);
    }
}

1 个答案:

答案 0 :(得分: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);
}