Drawing on a panel but when adding it through another frame, nothing draws

103 views Asked by At

I'm working on a project and the general idea of my work is to do every part of it in a stand alone project, then add it when finished to the main project through libraries.

My problem is - one part I have done was to perform a painting on a panel. When I add a layer pane which connects it to the main project , there is no drawing actually happening.

Here is my project sample code:

In code sample 1 there is JLayeredPane which contains my panel to perform drawing.

In code sample 2 there is a button. Its actionPerformed is to add that JLayeredPane to a panel. But the problem is that the drawing is not appearing after adding the JLayeredPane.

Code sample 1:

public class GraphGui extends javax.swing.JFrame {

/**
 * Creates new form GraphGui
 */
adjacencyMatrix m = new adjacencyMatrix();
Dfs df = new Dfs();
int[] x = new int[df.MAX_VERTS];
int[] y = new int[df.MAX_VERTS];
public Graphics2D d;
public Graphics2D doo;
int i = 0;


public GraphGui() {
    setlookAndFeel();
    initComponents();
    setLocationRelativeTo(null);
    DFS.setVisible(true);
    adjMatrics.setVisible(false);

//display is the panel that draw over
    d = (Graphics2D) display.getGraphics();
    doo = (Graphics2D) jPanel2.getGraphics();


}

//crating an initialization  of components are done automatically 

public void jButton1ActionPerformed(java.awt.event.ActionEvent evt)  {                                         

    df.dfs();
    doo.setFont(new Font("TimesRoman", Font.BOLD, 19));
    doo.drawString("visits: " + df.out, 5, 20);
    df.out = "";
}                                        

public void AddE1ActionPerformed(java.awt.event.ActionEvent evt) {                                      
    String j = start1.getText();
    int k = Integer.parseInt(j) - 1;
    String c = end1.getText();
    int v = Integer.parseInt(c) - 1;

    df.addEdge(k, v);
    d.setFont(new Font("TimesRoman", Font.BOLD, 17));
    d.drawLine(x[k] + 30, y[k] + 20, x[v], y[v] + 19);
}                                     

public void AddV1ActionPerformed(java.awt.event.ActionEvent evt) {                                      
    String f = ver1.getText();
    String toUpperCase = f.toUpperCase();
    char r = toUpperCase.charAt(0);
    df.addVertex(r);
    int radius = 30;
    int R = (int) (Math.random() * 256);
    int G = (int) (Math.random() * 256);
    int B = (int) (Math.random() * 256);
    x[i] = R % 320;
    y[i] = B % 167;

    d.setColor(new Color(R, G, B));
    d.setFont(new Font("TimesRoman", Font.BOLD, 15));
    d.drawOval(x[i], y[i], radius, radius);
    d.fillOval(x[i], y[i], radius, radius);
    d.setColor(Color.BLACK);
    d.drawString(r + "", x[i] + 10, y[i] + 20);
d.drawOval(0, 0, radius, radius);
    i++;
}  

What code sample 1 is supposed to do is shown at this link:

https://pbs.twimg.com/media/CG8INXZXAAEqthh.png:large

Code Sample 2

{
    private void graphBTActionPerformed(java.awt.event.ActionEvent evt)  {                                        
    GraphGui gr=new GraphGui();

    jPanel2.removeAll();

    jPanel2.add(gr.DFS);

    MainLayer.setVisible(false);
    Displaylayer.setVisible(true);
}         

}

And at the link below is what I got after adding the panel- nothing draws.

https://pbs.twimg.com/media/CG8IR7rWoAA3qKG.png:large

1

There are 1 answers

1
Pelit Mamani On BEST ANSWER

There are 2 separate issues here.

(1) Simplest answer is - you need to call 'getGraphics' from within your action method. Not from the constructor. E.g.

public void jButton1ActionPerformed(java.awt.event.ActionEvent evt)  {                                         
      Graphics2D doo = (Graphics2D) jPanel2.getGraphics();
      ...
      doo.setFont(...);
      doo.drawString(...);
}  

(2) This would yield visible drawings, but they'll disappear whenever java decides to repaint - e.g. if you minimize the frame. This can be solved by paintComponent() as mentioned in the remarks. The basic idea is that your component (eg jPanel2) would hold a data structure of eveything it needs to paint - Strings, edges, vertexes etc. In paintComponent you draw them all. In actionPerformed() you change the datastructure and invoke 'repaint'. A sketch of this approach:

class MyPanel extends JPanel{
    private String text;
    private Point[] vertextes;
    public void addVertext(..)
    public void paintComponent(Graphics g){
            ... use g to drawString, drawOval... according to 'text' and 'vertexes'
    }
}
// Then in your JFrame:
private MyPanel p;
...
actionPerfomred(...){
   p.addVertext(..)
   p.repaint();
}