1

i'm having hard time trying to understand and solve a problem while drawing on a JPanel within a JTabbedPane Basically i have this small app that draws stats graphics, its a simple JFrame with a JTabbedPane in it. Now, the JTabbedPane has 2 tabs, each one contains a JPanel (extended), a javax.swing.Timer starts after pressing the start button, and draws the graph on one JPanel, every second a new line, so far so good. If while the timer is running and drawing on the panel, i select the other tab ( its still empty), i see that the drawString method starts drawing on the selected panel, which does not contain any call to drawString method, I will paste the relevant code:

public class Monitor extends JPanel{

**private Timer timer=new Timer(1000,new PerformanceEvent(this));**


public Monitor(){

    this.setBackground(Color.BLACK);
    this.setPreferredSize(new Dimension(400,211));
    this.setBounds(new Rectangle(16,44,400,211));
    this.setVisible(true);

}

/**
 * This method contains the Graphoc tool to draw on the panel
 * @param g
 */
 public void analize(Graphics g){

if((ammountOfTimesAnalizeCalled / 10)==1){


  g.setColor(Color.red);
  g.drawLine(left1, high1, left2, high2);
  //print high2 variable on file
  Performance.report(high2);

  prepareForNext();
  ammountOfTimesAnalizeCalled++;

  System.out.println(ammountOfTimesAnalizeCalled);
}


 /**
  * This method is used by the GUI to start the timer<br/>
  * The timer starts and will  calls analize
  */
 public void start(){

     timer.start();
 }

     /**
      * This method stops the TimerTask
      */
     public void stop(){
         timer.stop();
     }

}

I have not reported the functionality of the analize method because its long and not relevant, all is done by the drawString Here the empty panel, which is setted in the second tab of the jtabbed pane, as you can see it does nothing yet

public class Informations extends JPanel{

/**
 * The constructor initializes the panel's appearence
 */
public Informations(){

    this.setBackground(Color.BLACK);
    this.setPreferredSize(new Dimension(400,211));
    this.setBounds(new Rectangle(16,44,400,211));
    this.setVisible(true);
}

}

The JTabbedPane

public class MyPane extends JTabbedPane{

private Monitor monitor=new Monitor();
private Informations informations=new Informations();

public MyPane(){
    monitor.setBounds(new Rectangle(5, 5, 400, 211));
    this.setPreferredSize(new Dimension(420,250));
    this.setBounds(new Rectangle(16,44,420,250));
    this.setVisible(true);
    this.addTab("Performance", monitor);
    this.addTab("Informations", informations);
}

}

And this is the event handler in the timer:

public class PerformanceEvent implements ActionListener{

private Monitor monitor=null;

public PerformanceEvent(Monitor monitor){

    this.monitor=monitor;
}

/**
 * Perform the drawing action
 */
public void actionPerformed(ActionEvent event) {

    monitor.analize(monitor.getGraphics());
}

}

Hope you have an advise, thx

JBoy
  • 5,398
  • 13
  • 61
  • 101

1 Answers1

0

Your problem is weird indeed. From the above code, all seems well. Could you test something, though? Since the drawing is only happening inside the Monitor-class, which extends JPanel, then instead of throwing the Graphics-object around, try just using Graphics g = getGraphics(); inside the "analize()" method to get the Graphics object. I'm not sure it will solve anything, but it seems weird to have one class hand another class something, which it already has.

Ultroman the Tacoman
  • 642
  • 1
  • 10
  • 16