1

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

paisanco
  • 4,098
  • 6
  • 27
  • 33
Mahmoud Adel
  • 1,262
  • 2
  • 13
  • 23
  • 1
    `JPanel.getGraphics()` is not the way to acquire the `Graphics` object for any `JComponent`. The painting related task needs to be done inside the overrridden `paintComponent(...)` method of the said `JComponent`. More information can be found at [performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/). One very [simple example](http://stackoverflow.com/a/10381004/1057230) – nIcE cOw Jun 08 '15 at 01:16
  • i have tried that's before but it didn't help me – Mahmoud Adel Jun 08 '15 at 01:40
  • `i have tried that's before but it didn't help me` - because you didn't follow the tutorial example. Download the last example from the tutorial to prove it works. And then make changes to the working example to do what you want, instead of starting with your own code that doesn't work. Also, variable names and method names should NOT start with upper case characters. You can learn that from the tutorial as well. – camickr Jun 08 '15 at 03:33
  • i will try again then , for variable names and methods i didn't care about that before but i will put it into consideration many thanks for your help – Mahmoud Adel Jun 08 '15 at 12:00

1 Answers1

1

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();
}
Pelit Mamani
  • 2,321
  • 2
  • 13
  • 11