mit repaint() Methode actionPerformed

Wie die repaint () - Methode auf, wenn eine Taste gedrückt wird, und Grafiken p hat zu re-paint alles von Grund auf neu?

Dank.

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

  class fares extends JPanel{

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

public ControlsB(square) {

    final 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("mark "));
    b1.add(new JLabel("for"));

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

    b1.add(f1);
    b1.add(new JLabel("till"));

    f2.setMaximumSize(new Dimension(100,30));
    b1.add(f2);

    //y-axis
    //this code is not in use at the moment

    f4.setMaximumSize(new Dimension(100,30));
    b2.add(f4);

    b0.add(b1);

    add(b);

    btn1.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent event){

            f = Integer.parseInt(f1.getText());

            invalidate();
            validate();
            paint(p);//this is not working...
        }
    });
 b.add(btn1);
  }
}

Dies ist der code, der aufgerufen werden und neu gestrichen:

import java.awt.*;

import javax.swing.*;

class Graph extends JPanel {

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

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

public void paintComponent(Graphics p) {

    super.paintComponent(p);

    Graphics2D graph = (Graphics2D)p;

    Dimension appletSize = this.getSize();
    int appletHeight = (int)(appletSize.height);
    int appletWidth = appletSize.width;

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

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


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

    //draw y-axis numbers 
    //(+ve)
    while(f != 0){
        String s = String.valueOf(f);

        m = m + b;
        f = f - 1;
    }
    //(-ve)
    m2 = y;
    while(f2 != g-1)
        m2 = m2 + b;
        f2 = f2 - 1;
    }
    //draw x-axis numbers.
    //(-ve)
    while(g != 0){
        String hy = String.valueOf(g);

        n = n + a;
        g = g + 1;
    }
    //(+ve)
    n2 = x + a;
    while(g2 != g3+1){
        String w = String.valueOf(g2);
        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.setColor(Color.lightGray);//lightgray line
        a = a + d;
        b = b + e;
        i = i + 1;
    }

  }
}

Im moment die resize-Taste funktioniert, aber ich muss die Größe meiner Fenster, um für das Diagramm, um Antworten auf die Eingänge gegeben. Im Grunde, wenn die Größe des graph wird neu gezeichnet /gestrichen... jetzt muss ich dies automatisch gemacht werden.

  • Befreien Sie sich von Ihrem Grafik-variable p, da sollte man nie versuchen zu halten, um die Komponente ein Graphics-Objekt und Farbe mit. Wenn es nicht zu einer NullPointerException auftreten, wird es einfach nicht funktionieren. Wenn Sie nicht bekommen eine anständige Antwort bald, betrachten die Erstellung und Veröffentlichung einer sscce.
InformationsquelleAutor Matthew | 2012-06-20
Schreibe einen Kommentar