# Thread: Paint Grafic (Series of Points) on Demand

1. Member
Join Date
Jan 2010
Posts
9
Rep Power
0

## Paint Grafic (Series of Points) on Demand

Cordial Greetings:
I have a Class that inherit from Frame and defined with a set of Vectors for store information of variables for plotting like:
The pairs of values of functions or variables is stored in Two Vectors called xVector and yVector of Double[] type.
PHP Code:
```class GTrama extends Frame {
private Vector<Color> cVector = new Vector<Color>(); //Color of Variable
private Vector<Boolean> vVector = new Vector<Boolean>(); //Visible?
private Vector<String> nVector = new Vector<String>(); //Name of Variable
private Vector<Graphics2D> gVector = new Vector<Graphics2D>();
private Vector<Double []> xVector = new Vector<Double []>(); //Data for x Axis
private Vector<Double []> yVector = new Vector<Double []>(); //Data for y Axis
private Vector<Double> xMin = new Vector<Double>();
private Vector<Double> yMin = new Vector<Double>();
private Vector<Double> xMax = new Vector<Double>();
private Vector<Double> yMax = new Vector<Double>();
private Vector<Float> dVector = new Vector<Float>(); //Line's Grossor plot
private double MaxX, MinX, MaxY, MinY;

GTrama(){
}
...

Double [] double_Double (double[] ddata){
//Convert data from double to Double Object for operates with Vectors
...
}
double [] Double_double (Double[] Ddata){
//Restore data from Double to double for operates with Arrays
...
}

double getMax(double[] data){
//Obtain Max Value from Array double
...
}
double getMin(double[] data){
//Obtain Min Value from Array double
...
}
double getMax(Vector<Double> data){
//Obtain Max Value from Vector Double
...
}
double getMin(Vector<Double> data){
//Obtain Min Value from Vector Double
...
}

void ViewAll(){
//Set Visible all Variables
...
}
void HideAll(){
//Set Hide all Variables
...
}
void ViewVble(String nVble){
//Set Visible a Variable called nVble
...
}
void HideVble(String nVble){
//Set Hide a Variable called nVble
...
}

int addVble(Graphics g, String nVble, double[] aX, double[] aY, Color VbleColor, float zDepth, boolean bVisible){
//Add a new Variable called nVble, with Color VbleColor, Grossor zDepth, Visibilidad bVisible
...
}

int delVble(Graphics g, String nVble){
//Delete a Variable called nVble stored on Vectors..
...
}

void PlotAll(Graphics g){
//Plots all visible Variables stored in the vectors
...
}

void PlotVble(Graphics g, int Indice){
//reload the information of a variable to show
...
}
}```

For clarity put this code, that for graphic is needless
PHP Code:
```  double getMax(double[] data){
if (data.length <1) throw new RuntimeException("Arreglo de datos vacío");
double Max = data[0];
for (int i=0;i<data.length;i++){
if (Max < data[i])
Max = data[i];
}
return Max;
}
double getMin(double[] data){
if (data.length <1) throw new RuntimeException("Arreglo de datos vacío");
double Min = data[0];
for (int i=0;i<data.length;i++){
if (Min > data[i])
Min = data[i];
}
return Min;
}
double getMax(Vector<Double> data){
if (data.size() <1) throw new RuntimeException("Arreglo de datos vacío");
double Max = data.elementAt(0);
for (int i=0;i<data.size();i++){
if (Max < data.elementAt(i))
Max = data.elementAt(i);
}
return Max;
}
double getMin(Vector<Double> data){
if (data.size() <1) throw new RuntimeException("Arreglo de datos vacío");
double Min = data.elementAt(0);
for (int i=0;i<data.size();i++){
if (Min > data.elementAt(i))
Min = data.elementAt(i);
}
return Min;
}
Double [] double_Double (double[] ddata){
Double[] Ddata = new Double[ddata.length];
for (int i=0;i<ddata.length;i++){
Ddata[i] = ddata[i];
}
return Ddata;
}
double [] Double_double (Double[] Ddata){
double[] ddata = new double[Ddata.length];
for (int i=0;i<Ddata.length;i++){
ddata[i] = Ddata[i];
}
return ddata;
}```

A Code important is this ...:
PHP Code:
```  void ViewAll(){
//Set Visible all Variables
for (int Indice = 0; Indice<nVector.size();Indice++){
vVector.set(Indice, Boolean.TRUE);
}
}
void HideAll(){
//Set Hide all Variables
for (int Indice = 0; Indice<nVector.size();Indice++){
vVector.set(Indice, false);
}
}
void ViewVble(String nVble){
//Set Visible a Variable called nVble
if (!nVector.contains(nVble)) throw new RuntimeException("La Variable no existe");
int Indice = nVector.indexOf(nVble);
vVector.set(Indice, true);
}
void HideVble(String nVble){
//Set Hide a Variable called nVble
if (!nVector.contains(nVble)) throw new RuntimeException("La Variable no existe");
int Indice = nVector.indexOf(nVble);
vVector.set(Indice, false);
}```

El core for graphics is that code:

AddVble Method stores all Variable's Information...
The name is the main identifier of a variable, in other words, nVble is unique...
PHP Code:
```int addVble(Graphics g, String nVble, double[] aX, double[] aY, Color VbleColor, float zDepth, boolean bVisible){
if (nVector.contains(nVble)) throw new RuntimeException("La Variable ya existe");
if(aX.length != aY.length) throw new RuntimeException("Tallas diferentes");
if(aX.length == 0||aY.length == 0) throw new RuntimeException("Arreglos de datos vacíos");
Graphics2D g2 = (Graphics2D)g;
Double[] vX = double_Double(aX);
Double[] vY = double_Double(aY);

MinX = getMin(xMin);
MaxX = getMax(xMax);
MinY = getMin(yMin);
MaxY = getMax(yMax);
return nVector.size();
}```
Remove all information of a variable from Vectors
PHP Code:
```  int delVble(Graphics g, String nVble){
if (!nVector.contains(nVble)) throw new RuntimeException("La Variable no existe");
int Indice = nVector.indexOf(nVble);
xMin.remove(Indice);
xMax.remove(Indice);
yMin.remove(Indice);
yMax.remove(Indice);
gVector.remove(Indice);
nVector.remove(Indice);
xVector.remove(Indice);
yVector.remove(Indice);
cVector.remove(Indice);
dVector.remove(Indice);
vVector.remove(Indice);

MinX = getMin(xMin);
MaxX = getMax(xMax);
MinY = getMin(yMin);
MaxY = getMax(yMax);
return nVector.size();
}```
runs through the vector called nvector and graph all the variables that are visible as vVector
PHP Code:
```  void PlotAll(Graphics g){
if (!nVector.isEmpty()){
for (int Indice = 0; Indice<nVector.size(); Indice++) {
if (vVector.elementAt(Indice)){
PlotVble(g, Indice);
}
}
}
}```
Reload the information of a variable to show
PHP Code:
```  void PlotVble(Graphics g, int Indice){
Graphics2D g2 = gVector.elementAt(Indice);
//    Graphics2D g2 = (Graphics2D)g;
float zDepth = dVector.elementAt(Indice);
Color color = cVector.elementAt(Indice);
double[] aX = Double_double(xVector.elementAt(Indice));
double[] aY = Double_double(yVector.elementAt(Indice));
g2.setColor(color);
g2.setStroke(new BasicStroke(zDepth));
g2.setPaintMode();
if (aX.length == 1){
g2.drawLine ((int)aX[0], (int)aX[0], (int)aY[0], (int)aY[0]);
} else {
System.out.print(nVector.elementAt(Indice)+"\n");
for (int j = 0;j<aY.length-1;j++){
//g2.draw( new Line2D.Double((int)vX[i], (int)vX[i+1], (int)vY[i], (int)vY[i+1]));
g2.drawLine ((int)aX[j], (int)aY[j], (int)aX[j+1], (int)aY[j+1]);
}
}
}```

How Use it?
A declaration in other Class.. Example MyClass;
PHP Code:
```public class Graficador extends javax.swing.JFrame {
/** Creates new form Graficador */
Image Imagen;
Graphics Grafica;
Graphics2D Grafica2D;
//  BufferedImage bImagen;
GTrama MisGraficos;

public void dibujar(Graphics g){
// super.paint( Grafica );
double misT[] = {100,120,140,160,180,200,220,240};
double misX[] = {120,120,160,160,160,140,140,140};
double misY[] = {140,140,120,120,120,160,160,160};
double misZ[] = {160,160,140,140,140,120,120,120};
double misP[] = {200,200,220,220,240,240,260,260};
double misQ[] = {260,260,240,240,220,220,200,200};
MisGraficos = new GTrama();
MisGraficos.addVble(g,"a", misT, misX, Color.RED, (float)1, false);
MisGraficos.addVble(g,"b", misT, misY, Color.GREEN, (float)1,false);
MisGraficos.addVble(g,"c", misT, misZ, Color.BLUE, (float)1, false);
MisGraficos.addVble(g,"d", misT, misP, Color.LIGHT_GRAY, (float)2, false);
MisGraficos.addVble(g,"e", misT, misQ, Color.DARK_GRAY, (float)2,false);
MisGraficos.ViewAll();
MisGraficos.ViewVble("e");
MisGraficos.ViewVble("d");
}
public void paint(Graphics g) {
Grafica = g;
super.paint(Grafica);
dibujar(Grafica);
if (MisGraficos!=null){
MisGraficos.PlotAll(Grafica);
}
}

private void jbDibujarActionPerformed(java.awt.event.ActionEvent evt) {

System.out.print("\n"+"\n"+"\n");
dibujar(Grafica); //vectors are initialized
paint(Grafica);
}```
The jbDibujarActionPerformed Method is action performed for a button called jbDibujar

When start my application... I can see the graphics for variables...
But, I change the paint method like:
PHP Code:
```  public void paint(Graphics g) {
Grafica = g;
super.paint(Grafica);
//dibujar(Grafica); Not Draw!!!!
if (MisGraficos!=null){
MisGraficos.PlotAll(Grafica);
}```
with the intention of drawing all the graphics, but have not been initialized data vectors, then you should not draw anything ..
Pressing the button "jbDibujar" ... vectors are initialized data and calls the paint method to display the graphics but that does not happen ....

What I can do?
Last edited by joseluisbz; 05-19-2010 at 04:16 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•