draw table on a graph

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass GrafTempextends JApplet{

staticdouble[][] t ={{37.5,37.9},{38.1,38.21},{36.2,36.8},{37.9,38.0},{35.4,35.6},{36.7,37.3},{37.1,36.7}};

publicvoid init(){}

publicvoid start(){}

publicvoid stop(){}

publicvoid destroy(){}

publicvoid paint(Graphics g){

g.drawString("Graficni prikaz temperature pacienta",10,15);

g.drawString("41",10,40);

g.drawString("40",10,70);

g.drawString("39",10,100);

g.drawString("38",10,130);

g.drawString("37",10,160);

g.drawString("36",10,190);

g.drawString("35",10,220);

g.drawString("1",55,235);

g.drawString("2",80,235);

g.drawString("3",105,235);

g.drawString("4",130,235);

g.drawString("5",155,235);

g.drawString("6",180,235);

g.drawString("7",205,235);

g.drawLine(40,35,240,35);

g.drawLine(40,50,240,50);

g.drawLine(40,65,240,65);

g.drawLine(40,80,240,80);

g.drawLine(40,95,240,95);

g.drawLine(40,110,240,110);

g.drawLine(40,125,240,125);

g.drawLine(40,140,240,140);

g.setColor(Color.red);

g.drawLine(40,155,240,155);

g.setColor(Color.black);

g.drawLine(40,170,240,170);

g.drawLine(40,185,240,185);

g.drawLine(40,200,240,200);

g.drawLine(40,215,240,215);

}

}

Hey. In the code you see a 2-dimensional table t and what I want todo know is to draw a line with that numbers on the graph that is already created. How can Ido that?

[3378 byte] By [eleanora] at [2007-11-26 13:32:09]
# 1

// <applet code="GT" width="300" height="250"></applet>

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

public class GT extends JApplet {

public void init() {

setLayout(new BorderLayout());

add(new GrafPanel());

}

}

// Using a separate graphics component will behave better in an applet.

class GrafPanel extends JPanel {

double[][] t = {

{37.5, 37.9}, {38.1, 38.21}, {36.2, 36.8}, {37.9, 38.0},

{35.4, 35.6}, {36.7, 37.3}, {37.1, 36.7}

};

AffineTransform xform;

boolean firstTime = true;

public GrafPanel() {

initializeTransform();

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

drawGrafStructs(g);

for(int j = 0; j < t.length; j++) {

g2.setPaint(Color.green.darker());

Point2D.Double p = modelToView(j, t[j][0]);

g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));

g2.setPaint(Color.blue);

p = modelToView(j, t[j][1]);

g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));

}

if(firstTime) firstTime = false;

}

/** Transform our model values to the view coordinate system. */

private Point2D.Double modelToView(double x, double y) {

Point2D.Double view = new Point2D.Double();

Point2D.Double model = new Point2D.Double(x, y);

xform.transform(model, view);

if(firstTime)

System.out.printf("model = [%.1f, %.1f] view = [%5.1f, %.1f]%n",

x, y, view.x, view.y);

return view;

}

private void initializeTransform() {

// Create scales for hard-coded graf.

double[] scales = getScaleFactors();

double xScale = scales[0];

double yScale = scales[1];

// Translate to the origin of the graf in view coordinate system.

double x0 = 55; // viewMinX

// For ordinate: move to bottom of graf at viewMaxY = 215 and

// continue down to the view equivalent of modelValue = 0 which

// is yScale * modelMinVal in view coordinate system. This point

// lies below/outside our gui, of course.

double y0 = 215 + yScale*35;

xform = AffineTransform.getTranslateInstance(x0, y0);

// Multiply the y scale by -1 so that we can count positive

// y upward from the origin along the graf ordinate.

// Java counts positive y downward from its origin which

// lies in the upper left corner of our applet. So we are

// repositioning our downward-looking view from the upper left

// corner to the model origin at modelY = 0, looking upward.

xform.scale(xScale, -yScale); // - sign flips y values

}

private double[] getScaleFactors() {

// Since graf structure values are hard-coded (vs. resizable)

// we only need to compute these scale values one time.

// The graf line y values form the viewY maximum/minimum.

// The abcissa values define the viewX maximum/minimum.

// The data values (t) define the model maxima/minima.

// view max/min values model max/min values

//minX = 55 minX = 1

//maxX = 205 maxX = 7

//minY = 35 minY = 35

//maxY = 215 maxY = 41

// _scale = view _domain / model _domain

int viewMinX = 55;

int viewMaxX = 205;

int viewMinY = 35;

int viewMaxY = 215;

int modelMinX = 1;

int modelMaxX = 7;

int modelMinY = 35;

int modelMaxY = 41;

double xScale = (double)(viewMaxX - viewMinX) / (modelMaxX - modelMinX);

double yScale = (double)(viewMaxY - viewMinY) / (modelMaxY - modelMinY);

return new double[] { xScale, yScale };

}

private void drawGrafStructs(Graphics g) {

g.drawString("Graficni prikaz temperature pacienta",10,15);

g.drawString("41",10,40);

g.drawString("40",10,70);

g.drawString("39",10,100);

g.drawString("38",10,130);

g.drawString("37",10,160);

g.drawString("36",10,190);

g.drawString("35",10,220);

g.drawString("1",55,235);// min x value

g.drawString("2",80,235);

g.drawString("3",105,235);

g.drawString("4",130,235);

g.drawString("5",155,235);

g.drawString("6",180,235);

g.drawString("7",205,235);// max x value

g.drawLine(40,35,240,35);// min y value

g.drawLine(40,50,240,50);

g.drawLine(40,65,240,65);

g.drawLine(40,80,240,80);

g.drawLine(40,95,240,95);

g.drawLine(40,110,240,110);

g.drawLine(40,125,240,125);

g.drawLine(40,140,240,140);

g.setColor(Color.red);

g.drawLine(40,155,240,155);

g.setColor(Color.black);

g.drawLine(40,170,240,170);

g.drawLine(40,185,240,185);

g.drawLine(40,200,240,200);

g.drawLine(40,215,240,215); // max y value

}

}

crwooda at 2007-7-7 20:40:31 > top of Java-index,Security,Cryptography...