图形用户界面,为什么我的画布没有重新绘制?

时间:2016-02-08 19:06:32

标签: java swing repaint

我的图形用户界面中的画布在按下“显示”操作按钮时不会更新或重新绘制。当扩展下拉框被更改并且画布应在“显示”时产生不同的显示时,这是显而易见的。

如果有人能指出代码中的错误,我们将不胜感激。在画布上绘制图形线时也遇到问题,有些似乎是填充而不是绘制,我认为这可能是由于画布重绘错误造成的。

谢谢,下面的代码

import java.awt.*;
import javax.swing.*;
import java.awt.event.*; //Import event handler classes
import java.awt.geom.*; // Geom packages

public class Listric_gui extends JFrame 
                            implements ActionListener, ItemListener {

JPanel pane = new JPanel ();
JComboBox Extension;
MyCanvas can1;
//Class Variables 
static float[]x = new float[201]; 
static float[]y = new float[201]; 
static float[]y2 = new float[201];
static float[] hwd = new float [201];
//static float Dip = 0f;
static int end = 200;
static int i, e1;






static int x_ending=160, y_ending = 35, ticksize=5;
static float x_orig = 50f, y_orig=50f, x_scale=4f, y_scale=5f,hwd_orig=50f,hwd_scale=5f;  

    Listric_gui () {
     try{
            UIManager.setLookAndFeel (
            //"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.err.println("Can't set look and feel: " + e);
        }

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    pane.setLayout(gridbag);
    setBackground(Color.lightGray);

    //Title
    buildConstraints(constraints, 0,0,5,1,0,10);
    constraints.fill = GridBagConstraints.BOTH;
    Font f = new Font ("Helvetica", Font.BOLD, 22);
    JLabel label1 = new JLabel("Fault Modelling Program", JLabel.CENTER);
    label1.setFont (f);
    //label1.setBackground(Color.white);
    label1.setBackground(Color.red);
    gridbag.setConstraints(label1, constraints);
    pane.add(label1);


    //Initialise Button objects
    //File
        buildConstraints(constraints, 0, 1, 1, 1, 1, 10);
        constraints.fill = GridBagConstraints.BOTH;
        Font f1 = new Font("Helvetica", Font.BOLD, 14);
        JButton file = new JButton("File");
        gridbag.setConstraints(file, constraints);
        file.setFont(f1);
        pane.add(file);

    //Input
        buildConstraints(constraints, 1, 1, 1, 1, 1, 0);
        constraints.fill = GridBagConstraints.BOTH;
        //constraints.ipadx = 10;
        JButton input = new JButton ("Input");
        gridbag.setConstraints(input,constraints);
        input.setFont(f1);
        pane.add(input);

    //Run
        buildConstraints(constraints, 2, 1, 1, 1, 1, 0);
        constraints.fill = GridBagConstraints.BOTH;
        JButton run = new JButton ("Run");
        gridbag.setConstraints(run, constraints);
        run.setFont(f1);
        pane.add(run);

    //Display Button
        buildConstraints(constraints, 3, 1, 1, 1, 1, 0);
        constraints.fill = GridBagConstraints.BOTH;
        JButton display = new JButton ("Display"); 
        gridbag.setConstraints(display, constraints);
        display.setFont(f1);
        pane.add(display);

    //Quit button
        buildConstraints(constraints, 4, 1, 1, 1, 1, 0);
        constraints.fill = GridBagConstraints.BOTH;
        JButton quit = new JButton ("Quit");
        gridbag.setConstraints(quit, constraints);
        quit.setFont(f1);
        pane.add(quit);

    //Fault Geometry label 

        buildConstraints(constraints, 0, 2, 1, 1, 1, 10);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.SOUTH;
        JLabel label2 = new JLabel ("Fault Geometry", JLabel.CENTER);
        label2.setFont(f1);
        gridbag.setConstraints(label2, constraints);
        pane.add(label2);

    //Geometry - Listric

        buildConstraints(constraints, 0, 3, 1, 1, 1, 10);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.NORTH;
        JCheckBox C1 = new JCheckBox ("Listric");
        gridbag.setConstraints(C1, constraints);
        pane.add(C1);

    //Checkbox group define so that only one selected at time
        ButtonGroup bg = new ButtonGroup ();
        bg.add(C1);

    //Extension Label
        buildConstraints(constraints, 0, 4, 1, 1, 1, 10);
        constraints.anchor = GridBagConstraints.SOUTH;
        JLabel label3 = new JLabel ("Extension", JLabel.CENTER);
        label3.setFont(f1);
        gridbag.setConstraints(label3, constraints);
        pane.add(label3);

    //Extension Choice
        buildConstraints(constraints, 0, 5, 1, 1, 1, 10);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.NORTH;
        JComboBox Extension = new JComboBox ();
        Extension.addItem ("-5 km");
        Extension.addItem ("-2 km");
        Extension.addItem ("0 km");
        Extension.addItem ("5 km");
        Extension.addItem ("10 km");
        Extension.addItem ("15 km");
        Extension.addItem ("20 km");
        Extension.addItem ("25 km");
        Extension.addItem ("30 km");
        gridbag.setConstraints(Extension, constraints);
        pane.add(Extension);

    //Draw Canvas
        buildConstraints(constraints, 1, 2, 4, 5, 0, 0);
        constraints.fill = GridBagConstraints.BOTH;
        MyCanvas can2 = new MyCanvas (); // paint method in seperate class
        gridbag.setConstraints(can2, constraints);
        can2.setBackground(Color.white);
        pane.add(can2);

        //Add all event listeners to components
        display.addActionListener(this);
        quit.addActionListener(this);
        Extension.addActionListener(this);

        setContentPane(pane);
    }
//Define main block
    public static void main (String args[]){
        JFrame frame = new Listric_gui();
        //Method to exit
    WindowListener l = new WindowAdapter (){
                    public void windowClosing (WindowEvent e) {
                        System.exit (0);
                    }
    };
                    frame.addWindowListener (l);
    frame.setSize(1600,600);
    //frame.pack();
    //frame.revalidate();
    //frame.repaint();
    frame.setVisible (true);
}

    static void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy){
        gbc.gridx = gx;
        gbc.gridy = gy;
        gbc.gridwidth = gw;
        gbc.gridheight = gh;
        gbc.weightx = wx;
        gbc.weighty = wy;
    }

    public Insets getInsets() {
        return new Insets (10, 10, 10, 10);
    }

//declare actionlistener method
public void actionPerformed (ActionEvent ae) {
        String label = ae.getActionCommand();
        if (label == "Display") {
            Modelling m1 = new Modelling ();
            can1= new MyCanvas();
            pane.add(can1);
            setContentPane(pane);
            }else if (label == "Quit")
            System.exit(0);
    }
   public void itemStateChanged (ItemEvent evt) {
        if(evt.getSource () == Extension) {
            String s = (String) evt.getItem ();
            if (s == "-5 km")
               e1 = (int) -5f;
            if (s == "-2 km")
               e1 = (int) -2f;
           if (s == "0 km")
               e1 = (int) 0f;
           else if (s == "5 km")
               e1 = (int) 5f;
           else if (s == "10 km")
               e1 = (int) 10f;
           else if (s == "15 km")
               e1 = (int) 15f;
           else if (s == "20 km")
               e1 = (int) 20f;
           else if (s == "25 km")
               e1 = (int) 25f;
           else if (s == "30 km")
               e1 = (int) 30f;
        }
    }

//End of controlling class
//Class for modelling fault
class Modelling {
Modelling () {
    calc ();
}
private void calc () {
    float Sum1, Sum2, Sum3, Sum4, Sum5, Sum6, Sum7, Sum8;//Initialise variables
    float A = 30f;
float B = 50f;
float C = -1f;
float Zd = 20f;
//float x0 = 60f;
float D = 1f;
    int k;
        //Loop for calculating fault 1 coordinates
        for(k=0;k<=Listric_gui.x_ending;k++){   
        Listric_gui.x[k] = (float) k;
        Sum1 = (Listric_gui.x[k] - A);
        Sum2 = ((Sum1/Zd)*C);
        Sum3 = D - ((float)Math.exp(Sum2));
        Sum4 = (A * Sum3);
                    Listric_gui.y[k] = Sum4; 
        if(Listric_gui.x[k]<=30) 
           Listric_gui.y[k]=0;

        Sum1 = (Listric_gui.x[k] - B);
        Sum2 = ((Sum1/Zd)*C);
        Sum3 = D - ((float)Math.exp(Sum2));
        Sum4 = (A * Sum3);
                    Listric_gui.y2[k] = Sum4; 
        if(Listric_gui.x[k]<=50) 
           Listric_gui.y2[k]=0;


        System.out.println("x = " + Listric_gui.x[k] + " y = "+ Listric_gui.y[k] + "y2 = " + Listric_gui.y2[k]);
        }
        //Loop for calculating hangingwall deformation

        //Initialise Variables

        Listric_gui.hwd [k] = (float) k;
        Listric_gui.e1 = 30;
        for(k=0;k<=Listric_gui.x_ending;k++){
        Listric_gui.hwd[k] = 0f;
        if(Listric_gui.x[k]>30) 
            Listric_gui.hwd[k] = Listric_gui.y[k]-Listric_gui.y[k-Listric_gui.e1];
//System.out.println("x = " + x[k] + " hwd = "+ hwd [k]);
}
        //Fault 2 loop

        /*for(k=0;k<=Listric_gui.x_ending;k++){   
        //Listric_gui.x[k] = (float) k;
        Listric_gui.y2[k] = (B * (D - ((float)Math.exp((((Listric_gui.x[k] - B)/Zd)*C))))); 
        if(Listric_gui.x[k]<=50) {
           Listric_gui.y2[k]=0;
        }
        System.out.println("f2x = " + Listric_gui.y2[k]);*/

}
}

//Listener Method
//draw Class
class MyCanvas extends JPanel {
int k, i;
    public void paintComponent (Graphics screen) {
        super.paintComponent(screen);
        Graphics2D screen2D = (Graphics2D) screen;

    screen2D.setColor(Color.red);
    GeneralPath ln = new GeneralPath();
    ln.moveTo(Listric_gui.x_orig,Listric_gui.y_orig);
    for(k=0;k<=Listric_gui.x_ending;k++) {
    ln.lineTo((Listric_gui.x[k]*Listric_gui.x_scale)+Listric_gui.x_orig,(Listric_gui.y_orig+(Listric_gui.y[k]*Listric_gui.y_scale)));
    ln.closePath();
    screen2D.fill(ln);
    }
    GeneralPath ln2 = new GeneralPath();
    ln2.moveTo(Listric_gui.x_orig,Listric_gui.hwd_orig);
    for(k=0;k<=Listric_gui.x_ending;k++) {
    ln2.lineTo((Listric_gui.x[k]*Listric_gui.x_scale)+Listric_gui.x_orig,(Listric_gui.y_orig+(Listric_gui.hwd[k]*Listric_gui.hwd_scale)));
    ln2.closePath();
    screen2D.fill(ln2);

    }

    /*GeneralPath ln5 = new GeneralPath();
    ln5.moveTo(Listric_gui.x_orig,Listric_gui.hwd_orig);
     for(k=0;k<=Listric_gui.x_ending;k++) {
    ln5.lineTo((Listric_gui.x[k]*Listric_gui.x_scale)+Listric_gui.x_orig,(Listric_gui.y_orig+(Listric_gui.y2[k]*Listric_gui.hwd_scale)));
    ln5.closePath();
    //screen2D.fill(ln5);
    screen2D.draw(ln5);
    }*/
    //Fault profile graphically


//x axis
BasicStroke xpen = new BasicStroke(2f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND);
screen2D.setStroke(xpen);
screen2D.setFont(new Font("Serif", Font.PLAIN, 11));
screen2D.setColor(Color.black);
GeneralPath xaxis = new GeneralPath();
xaxis.moveTo(Listric_gui.x_orig,(Listric_gui.y_orig+(Listric_gui.y_ending*Listric_gui.y_scale)));
xaxis.lineTo((Listric_gui.x_orig+(Listric_gui.x_ending*Listric_gui.x_scale)),(Listric_gui.y_orig+(Listric_gui.y_ending*Listric_gui.y_scale)));
for (i=0; i <=(Listric_gui.x_ending); i=i+20){    
    Line2D.Float ticks = new Line2D.Float(((i*Listric_gui.x_scale)+Listric_gui.x_orig), ((Listric_gui.y_orig)+(Listric_gui.y_ending*Listric_gui.y_scale)), ((i*Listric_gui.x_scale)+Listric_gui.x_orig), ((Listric_gui.y_orig+Listric_gui.ticksize)+(Listric_gui.y_ending*Listric_gui.y_scale)));
    screen2D.draw(ticks);
    screen2D.drawString(""+i, ((i*Listric_gui.x_scale)+Listric_gui.x_orig), ((Listric_gui.y_orig)+(Listric_gui.y_ending*Listric_gui.y_scale))+20);
}

//y axis
BasicStroke ypen = new BasicStroke(2f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND);
screen2D.setStroke(ypen);
screen2D.setFont(new Font("Serif", Font.PLAIN, 11));
screen2D.setColor(Color.black);
GeneralPath yaxis = new GeneralPath();
yaxis.moveTo(Listric_gui.x_orig,Listric_gui.y_orig);
yaxis.lineTo(Listric_gui.x_orig,(Listric_gui.y_orig+(Listric_gui.y_ending*Listric_gui.y_scale)));
screen2D.draw(yaxis);
for (i=0; i <=Listric_gui.y_ending; i=i+5){  
    screen2D.drawString(""+i, ((Listric_gui.x_orig))-15, (Listric_gui.y_orig+(i*Listric_gui.y_scale)));
    Line2D.Float yticks = new Line2D.Float(((Listric_gui.x_orig)), (Listric_gui.y_orig+(i*Listric_gui.y_scale)), (((Listric_gui.x_orig+Listric_gui.ticksize))), (Listric_gui.y_orig+(i*Listric_gui.y_scale)));
    screen2D.draw(yticks);    
}

//Y axis label
screen2D.setFont(new Font("Serif", Font.PLAIN, 14));
screen2D.draw(xaxis);
AffineTransform orig = screen2D.getTransform();

screen2D.drawString("Depth(km)", Listric_gui.x_orig-20, Listric_gui.y_orig-20);
screen2D.rotate(-Math.PI/2);
screen2D.setTransform(orig);

//X axis label
screen2D.setFont(new Font("Serif", Font.PLAIN, 14));
screen2D.drawString("Distance (km)", Listric_gui.x_orig+(Listric_gui.x_ending*Listric_gui.x_scale/2), Listric_gui.y_orig+(Listric_gui.y_ending*Listric_gui.y_scale)+40);

//Values
screen2D.setFont(new Font("Serif", Font.PLAIN, 10));
screen2D.setColor(Color.red);
int y_val = 25;
for (k = 0; k <=160; k=k+5){
    screen2D.drawString(" X = " + String.valueOf(Listric_gui.x[k]) + " Y = " + String.valueOf(Listric_gui.y[k]), (Listric_gui.x_orig+(Listric_gui.x_ending*Listric_gui.x_scale)+35), y_val);
    y_val = y_val+10;
}
//print screen for hwd values
int y_val2 = 25;
for (k = 0; k <=160; k=k+5){
    screen2D.drawString("X = " + String.valueOf(Listric_gui.x [k])+ " Hwd = " + String.valueOf(Listric_gui.hwd[k]),(Listric_gui.x_orig+(Listric_gui.x_ending*Listric_gui.x_scale)+155),y_val2);
    y_val2 = y_val2+10;
}
//Title
screen2D.setColor(Color.black);
Font f1 = new Font("Helvetica", Font.BOLD, 24);
screen2D.setFont(f1);
screen2D.drawString("LISTRIC FAULT", (Listric_gui.x_ending*Listric_gui.x_scale/2)+Listric_gui.x_orig,(Listric_gui.y_orig-10));
}     
}
}

0 个答案:

没有答案