0

FNAKLNFDKA.java

import java.awt.*;
import javax.swing.*;

public class GraphApplet extends JApplet{

private static final long serialVersionUID = 1L;
public static Graphics p;

public void init(){

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {


    });
  }
}

FANKJ.java

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

class FASKHF extends JPanel {


public static Graphics p;
private static final long serialVersionUID = 1L;

public Graph(){
    this.setBackground(Color.yellow);
}

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

public static void updateGraph() {


    repaint();
}



public void paint(Graphics F) {



    //line co-ordinates
    //the numbers represent the number of boxes on the graph
    //1st line on the l/h side
    int xstart1 = 2;
    int ystart1 = 3;

    int xfinish1 = 1;
    int yfinish1 = 9;

    //2nd line on the r/h side
    int xstart2 = 8;
    int ystart2 = 3;

    int xfinish2 = 9;
    int yfinish2 = 9;

  //drawing upper arc
    int arcX = 2;
    int arcY = 1;
    int imagRectWid = 6;
    int imagRectHei = 4;
    int startAngle = 0;
    int arcAngle = 180;

    //other



    f = xaxis22;
    g = xaxis11;

    //to be assigned later.
    //the values of xaxis and yaxis need to be separated
    //yaxis3 = 5;
    //yaxis4 = -5;

    //change -ve num to +ve
    int g3 = Math.abs(g);

    int a1 = g3 + f;
    int b1 = a1;

    int d = (appletWidth / a1);
    int e = (Height / b1);

    /**
     to determine the
     ammount of pixles there
     is in each box of the
     graph, both y-axis and 
     x-axis
     */
    int xbox = x1 / 10;
    int ybox = y1 / 10;

    //line variables
    //the xstart, ystart, etc represent the number of boxes

    //top point of the line on the graph
    px1 = xbox * xstart1;//start x
    py1 = ybox * ystart1;//start y

    //lowwer point of the line on the graph
    px = xbox * xfinish1;//finish x
    py = ybox * yfinish1;//finish y

    //other line on the right hand side
    p2x = xbox * xstart2;
    p2y = ybox * ystart2;

    p2x2 = xbox * xfinish2;
    p2y2 = ybox * yfinish2;

    //drawing arc
    int arcBoxX = arcX * xbox;
    int arcBoxY = arcY * ybox;
    int arcBoxWidth = imagRectWid * xbox;
    int arcBoxHeight = imagRectHei * ybox;


    //draw y-axis numbers 
    //(+ve)
    while(f != 0){
        String s = String.valueOf(f);
        p.drawString(s,(x + 5),m + 13);
        m = m + b;
        f = f - 1;
    }
    //(-ve)
    m2 = y;
    while(f2 != g-1){
        String u = String.valueOf(f2);
        p.drawString(u,(x + 5),m2 - 3);
        m2 = m2 + b;
        f2 = f2 - 1;
    }
    //draw x-axis numbers.
    //(-ve)
    while(g != 0){
        String t = String.valueOf(g);
        p.drawString(t,n,y - 5);
        n = n + a;
        g = g + 1;
    }
    //(+ve)
    n2 = x + a;
    while(g2 != g3+1){
        String vw = String.valueOf(g2);
        p.drawString(vw,n2 -10,y - 5);
        n2 = n2 + a;
        g2 = g2 + 1;
    }

    BasicStroke aLine2 = new BasicStroke(1.0F,
                                         BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
    graph.setStroke(aLine2);

    //notch on numbers and grid lines
    //left to right, top to bottom notches
    int v2 = -5;
    int v5 = 0;
    while(i <= a1-1){

        p.drawLine(Da,0,Ea,y1);
        a = a + d;
        b = b + e;
        i = i + 1;
    }
    //notches
    while(i2 <= a1){
        p.setColor(Color.blue);//notch color
        p.drawString("x",v2+2,y+3);//xaxis
        p.drawString("x",x-4,v5+4);//yaxis
        v5 = v5 + e;
        v2 = v2 + d;
        i2 = i2 + 1;
    }


    }
  }
}




import java.awt.*;
import javax.swing.*;


}
}



 /*
  * this code is to accept a user input in order for him to
  * change the ranges of the xaxis and yaxis to his needs
  */

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ControlsB extends JPanel{

    private static final long serialVersionUID = 1L;
    public static int xaxis1,xaxis2,yaxis3,yaxis4;

    //public int xaxis11,xaxis22,yaxis33,yaxis44;
    //public Object ControlsB;
    //private Graph graph;
    //public static Graphics p;
    public String s;

public ControlsB(Box box2) {

    JButton btn1 = new JButton("Resize");

    final Box b = Box.createHorizontalBox();
    b.add(new JLabel("Please enter range:  "));
    Box b0 = Box.createVerticalBox();//create a vertical box to stack the controls

    Box b1 = Box.createHorizontalBox(); // create a horizontal box for the x-axis

    //x-axis
    b1.add(new JLabel("x-axis "));
    b1.add(new JLabel("from"));

    final JTextField f1 = new JTextField("  0");

    f1.setMaximumSize(new Dimension(100,30));


 b.add(btn1);
}

protected Object Integer(int xaxis12) {
    return null;
}
}

Now the problem with this program is; in ControlsB I have a method that places the controls at the bottom side (Under the yellow box / graph), now these controls are supposed to accept the range of the graph on the x-axis line (x-axis only at the moment because the code for the y-axis is not ready yet). When the user presses the "resize" button the graph should come up with the changes.

However this is not happening and the button is being pressed for nothing... and I cannot figure out what I have wrong.

In ControlsB file I have an action listener for the btn1 that is the "resize" button and a soon as the bottom is pressed it changes the text fields inputs (f1 and f2) into integers and assigned to x-axis1,x-axis 2,etc...

Now I am making a reference from theses variables (found in the action listener method) to the class Graph.java with this line: public static int xaxis11 = ControlsB.xaxis1;

Can anyone please let me know what is the best solution for this problem I have..??..

NOTE: all these files compile. but the graph does not come up.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
MBC870
  • 373
  • 3
  • 8
  • 16

2 Answers2

1

The way that it is currently coded, you are assigning the xaxis11, xaxis22, yaxis33, and yaxis44 only one time. The variables are not updated dynamically when they change in ControlsB. This means that you simply have to update them each time the values change. To do this, add a method to your Graph class:

public static void updateGraph() {
    xaxis11 = ControlsB.xaxis1;
    xaxis22 = ControlsB.xaxis2;
    yaxis33 = ControlsB.yaxis3;
    yaxis44 = ControlsB.yaxis4;
}

Then, when the user changes the values in ControlsB, call Graph.update(). Make sure you are also calling repaint when this happens.

Also note that making all of these things static is not your only option. You can also simply have a reference to an object as a field of another class, set it in the constructor, and call methods on it when necessary. You may run into some problems with your implementation due to this problem, as making everything static may affect your ability to call repaint() on an object, which you need to do in order to update it.

A workaround to that problem would be to add repaint() to the bottom of the updateGraph method I outlined above.

Michael
  • 1,239
  • 9
  • 14
  • can you please give me more info on the Graph.update() and the repaint please thanks. – MBC870 Jun 18 '12 at 20:22
  • Earlier to be honest I tried with the repaint() but it didn't happen, am I doing something wrong?? – MBC870 Jun 18 '12 at 20:30
  • 1
    @Matthew I simply meant to add the `updateGraph()` method to your `Graph` class with those four statements and `repaint()` at the end. Then, in your `actionPerformed(...)`, you call `Graph.updateGraph()` after updating the values (xaxis1, etc). – Michael Jun 18 '12 at 20:34
  • is this what you are talking about: public static void update() { xaxis11 = ControlsB.xaxis1; xaxis22 = ControlsB.xaxis2; yaxis33 = ControlsB.yaxis3; yaxis44 = ControlsB.yaxis4; repaint(); } – MBC870 Jun 18 '12 at 20:42
  • I am asking you again because it is not working the repaint(); :s – MBC870 Jun 18 '12 at 20:46
  • @Matthew Yes, that's what I am talking about. I don't know why it wouldn't work, not in a position to test it at the moment unfortunately. – Michael Jun 18 '12 at 20:49
  • ok. thanks anyways, I will update my question. when you have time please take a quick look. thanks for your help :) – MBC870 Jun 18 '12 at 21:18
1

When you set the static variables in Graph, you are making a copy of the variables in ControlB, so when you are chaning them in ControlB, the changes do not reflect in Graph. Instead access the variables through ControlB (preferably via a public method in ControlB)

Update

to enable calling repaint, remove all static modifies from Graph and ControlsB; add a variable holding the graph instance to ControlsB and pass that instance to its constructor

public class ControlsB extends JPanel
{
  Graph my_graph;
  //...
  public ControlsB(Box box2, Graph graph)
  { 
    my_graph = graph;
    // ...
  }
}

then change the updateGraph() method in Graph to

public void updateGraph(int xaxis1, xaxis2, yaxis3, yaxis4)
{
  xaxis11 = xaxis1;
  xaxis22 = xaxis2;
  yaxis33 = yaxis3;
  yaxis44 = yaxis4;

  repaint();
}

and call it like this in the button's actionperformed() method:

graph.updateGraph(xaxis1, xaxis2, yaxis3, yaxis4);

Note: you don't seem to be using box2 in the ControlsB constructor, so you can remove that parameter.

Attila
  • 28,265
  • 3
  • 46
  • 55
  • Thanks for your reply, however can you please explain this via some code so that I can have a better picture on what you mean. – MBC870 Jun 18 '12 at 20:18
  • @Matthew - the quick and dirty way is to replace all `xaxis11` references to `ControlB.xaxis1` references (similarly for the others) as those static variables in `ControlB` are public. That way any time `Graph` needs the axes values, it will read the ones in `ControlB` directly – Attila Jun 18 '12 at 20:32
  • done that (see updated question) however the graph is staying blank and not repainting, more over the software I am using (eclipse) is pointing out there is an error on line 30: *repaint()*. I cannot see why... – MBC870 Jun 18 '12 at 21:31
  • @Matthew - I think the problem with the `repaint()` is that you are calling it from a static method. If you remove the `static` modifier, you will have to instantaite the class and pass that instance around for other objects to be able to call methods on it – Attila Jun 19 '12 at 00:22
  • Can you give me a small example so i can understand better what you mean please. Thanks :) – MBC870 Jun 19 '12 at 12:24