2012-06-18 93 views
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文件I具有用于BTN1即是“调整”按钮和一旦底部被按下它改变了文本字段输入(f1和f2)成整数和动作侦听器赋值给x-AXIS1,x轴2等...

现在我正在从论文变量对类Graph.java与该线的基准(在动作侦听器发现方法):公共static int xaxis11 = ControlsB.xaxis1;

任何人都可以请让我知道什么是对这个问题我有??最好的解决办法..

注:所有这些文件进行编译。但图表没有出现。

回答

1

它是目前编码的方式,您只分配一次xaxis11,xaxis22,yaxis33yaxis44。变量在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方法的底部。

+0

可以请给我更多关于Graph.update()和repaint的信息,谢谢。 – Matthew

+0

早些时候说实话我尝试了重绘()但它没有发生,我做错了什么? – Matthew

+1

@Matthew我只是想在'Graph'类中添加'updateGraph()'方法,并在最后使用这四个语句和'repaint()'。然后,在你的'actionPerformed(...)'中,在更新值(xaxis1等)后调用'Graph.updateGraph()'。 – purtip31

1

当您在Graph设置静态变量,你可以将这个变量的副本ControlB,所以当你在ControlB chaning他们所做的更改不会在Graph反映。代替通过ControlB访问的变量(优选地通过在ControlB的公共方法)

更新

启用主叫重绘,从GraphControlsB除去所有静态修改;一个变量保存图表实例添加到ControlsB和实例传递给它的构造

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

然后更改GraphupdateGraph()方法

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

    repaint(); 
} 

和按钮的actionperformed()方法调用它是这样的:

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

注意:你似乎没有在ControlsB的构造函数中使用box2,因此您可以删除该参数。

+0

感谢您的回复,但是您可以通过一些代码来解释这一点,以便我能更好地了解您的意思。 – Matthew

+0

@Matthew - 快速和肮脏的方法是将所有'xaxis11'引用替换为'ControlB.xaxis1'引用(对其他类似),因为'ControlB'中的静态变量是公共的。这样任何时候'Graph'都需要坐标轴值,它会直接读取'ControlB'中的那个 – Attila

+0

(看更新后的问题),但是图形保持空白而不是重绘,更多的是通过我使用的软件(eclipse)指出第30行有一个错误:* repaint()*。我看不出为什么...... – Matthew