Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using repaint() method with actionPerformed

How to use the repaint() method when a button is pressed and Graphics p has to re-paint everything from scratch?

Thanks.

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

This is the code that has to be called and repainted:

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

  }
}

At the moment the resize button works, but I need to resize my window in order for the graph to respond to the inputs given. Basically when resizing the graph is being redrawn / repainted... now I need this to be done automatically.

like image 525
MBC870 Avatar asked Jun 20 '12 23:06

MBC870


People also ask

Why repaint () method is used?

repaint(): The repaint() is intended to allow various methods to call for a re-rendering of the component. No graphics context is needed for repaint(). A call to repaint() calls update().

What is paint () and repaint () methods?

Paint() and Repaint() paint(): This method holds instructions to paint this component. In Java Swing, we can change the paintComponent() method instead of paint() method as paint calls paintBorder(), paintComponent() and paintChildren() methods. We cannot call this method directly instead we can call repaint().

What does repaint () method does when it is called?

- The repaint() method is asynchronous. - The repaint() requests an erase and redraw (update) after a small time delay. - When you invoke repaint(), a message is sent to the native GUI requesting it to perform the action sometime in the distant future. - This method does not invoke paint() method directly.


1 Answers

It's hard to tell what's wrong based on snippets of code, but it appears that your graph is drawn on the Graph object, called graph (please correct me if I'm wrong), and you appear to be trying to repaint (or paint -- never call that directly!) your ControlsB panel. If so, then you may be calling methods on the wrong object. Perhaps you need to do something like:

// graph is final so it may be used in an inner class
public ControlsB(Box box2, final Graph graph) {

     // .....

      btn1.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent event){

            f = Integer.parseInt(f1.getText());
            g = Integer.parseInt(f2.getText());
            System.out.println(f + "  " + g);

            // invalidate();
            // validate();
            //  paint(p); ***** NEVER do this

            graph.repaint();
        }
    });
}

Also:

  • Never call paint(...) directly on a component except in very special circumstances (this isn't it).
  • Never try to hold on to a components Graphics object and draw with it as this will often lead to a NullPointerException occurring, and it surely won't work.
  • Do read up on how to do drawing in Swing in the standard Swing tutorials. You would likely learn much from doing this.
  • Again, if you don't get a decent answer soon, consider creating and posting an sscce.
like image 144
Hovercraft Full Of Eels Avatar answered Oct 24 '22 17:10

Hovercraft Full Of Eels