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;
}
}

現在、このプログラムの問題は次のとおりです。ControlsBでは、コントロールを下側(黄色のボックス/グラフの下)に配置する方法があります。これらのコントロールは、x軸線上のグラフの範囲を受け入れることになっています(現時点ではx軸のみy軸のコードはまだ準備ができていません)。ユーザーが「サイズ変更」ボタンを押すと、グラフに変更が表示されます。

しかし、これは起こっておらず、ボタンは何も押されていません...そして私は何が間違っているのか理解できません。

ControlsBファイルには、「サイズ変更」ボタンであるbtn1のアクションリスナーがあり、下部押すとすぐに、テキストフィールド入力(f1およびf2)が整数に変更され、x軸1、x軸2に割り当てられます。 、等...

ここで、これらの変数(アクションリスナーメソッドにあります)からクラスGraph.javaへの参照を次の行で作成しています。publicstatic int xaxis11 = ControlsB.xaxis1;

誰かが私が持っているこの問題の最善の解決策を教えてもらえますか..??。。

注:これらのファイルはすべてコンパイルされます。しかし、グラフは表示されません。

4

2 に答える 2

1

で静的変数を設定するGraphと、 で変数のコピーが作成されるControlBため、 で変数をControlB変更しても、変更は に反映されませんGraph。代わりにControlB(できれば のパブリック メソッドを介してControlB)を通じて変数にアクセスします。

アップデート

再描画の呼び出しを有効にするには、 and からすべての静的変更を削除しGraphますControlsB。グラフ インスタンスを保持する変数を追加し、ControlsBそのインスタンスをそのコンストラクタに渡します

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

次に、updateGraph()メソッドをに変更Graphします

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

  repaint();
}

ボタンのactionperformed()メソッドで次のように呼び出します。

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

box2:コンストラクターで使用していないようですControlsBので、そのパラメーターを削除できます。

于 2012-06-18T20:09:50.143 に答える
1

現在コーディングされている方法ではxaxis11、 、xaxis22yaxis33、および をyaxis441 回だけ割り当てています。で変数が変更されても、変数は動的に更新されませんControlsB。これは、値が変わるたびに更新するだけでよいことを意味します。これを行うには、Graphクラスにメソッドを追加します。

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

次に、ユーザーが の値を変更したら、ControlsBを呼び出しますGraph.update()。これが発生した場合は、repaint も呼び出していることを確認してください。

また、これらすべてを静的にすることが唯一の選択肢ではないことに注意してください。オブジェクトへの参照を別のクラスのフィールドとして保持し、それをコンストラクターに設定し、必要に応じてメソッドを呼び出すこともできます。この問題により、実装でいくつかの問題が発生する可能性があります。すべてを静的にすることは、オブジェクトを更新するために必要な repaint() をオブジェクトで呼び出す機能に影響を与える可能性があるためです。

repaint()この問題の回避策は、上記で概説した updateGraph メソッドの末尾に追加することです。

于 2012-06-18T20:06:29.487 に答える