Printing lines above JPanles
Hi, i'm trying to a simple Applet where I need to draw some lines that pass above some JPanels objects with the click of the mouse. The program is articulated in one only class as follow:
- one single main class extends JApplet (actually I've used the NetBeans GUI) and implements mouseListener.
- paint(Graphics g) method calls the super paint method and print the lines stored in an ArrayList.
The implementation of MouseClicked updates the Jpanel with a number and after it, repaint() is called.
Now, I'm a beginner in Java, and I guess that the repaint() call the paint() method (implemented in the same class), which with super.paint() initialize the JPanels I've created with NetBeans drag&drop. So after super.paint() the following instructions draw all the lines with g.drawLines(p1.x, p1.y, p2.x, p2.y), where p1 and p2 are starting and end point.
By doing in this way the lines pass behind the Jpanel but I cannot understand why, infact after the Jpanel is updated with a number I call immediately repaint() which only paints the lines.
Do somebody know where is the problem?
I cannot upload the code in this moment, but iI hope to have explained well this very simple program.
Thanks!
Tom
Re: Printing lines above JPanles
Is your paint method in the JApplet class itself? It shouldn't be. You should in fact be drawing in the paintComponent method of a JPanel class that is displayed by the JApplet as is well explained in the drawing with Swing tutorials..
If you're still stuck, consider creating and posting an SSCCE.
Re: Printing lines above JPanles
Quote:
Originally Posted by
Fubarable
Is your paint method in the JApplet class itself? It shouldn't be. You should in fact be drawing in the paintComponent method of a JPanel class that is displayed by the JApplet as is well explained in the drawing with Swing tutorials..
If you're still stuck, consider creating and posting an
SSCCE.
Hi, thanks for replying so quickly.
The paint method was in the JApplet class :s:, you was right! So, I'm now implementing the paintComponent in the extended JPanel class. If I have problems again I'll come back.
Thanks,
Tom
Re: Printing lines above JPanles
Hi, the code is still not drawing the line above he Jpanel, I cannot understand why.
Could somebody just give me an advise please, it is a very simple code actually. :s:
Thanks,
Tom
Code:
public class INVERTgameLev1 extends JApplet {
tomographyPanel myPanel;
raysInteract raysClick;
int w, h;
@Override
public void init() {
w = super.getSize().width;
h = super.getSize().height;
myPanel = new tomographyPanel();
myPanel.setLayout(null);
raysClick = new raysInteract(myPanel);
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
initComponent(myPanel);
add(myPanel);
myPanel.addMouseListener(raysClick);
repaint();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void initComponent(tomographyPanel p){
Rectangle bounds1 =new Rectangle(Math.round(w/2)-100, Math.round(h/2)-100, 100, 100);
Rectangle bounds2 =new Rectangle(Math.round(w/2)-100, Math.round(h/2), 100, 100);
for(int i=0; i<=1; i++){
bounds1.x = bounds1.x + i*100;
bounds2.x = bounds2.x + i*100;
JPanel panel1= new JPanel();
p.add(panel1);
panel1.setBounds(bounds1);
panel1.setBorder(BorderFactory.createLineBorder(Color.black, 2));
panel1.setBackground(Color.WHITE);
JPanel panel2= new JPanel();
p.add(panel2);
panel2.setBounds(bounds2);
panel2.setBorder(BorderFactory.createLineBorder(Color.black, 2));
panel2.setBackground(Color.WHITE);
}
}
}
class tomographyPanel extends JPanel{
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(2, 5, 300, 300); //Example of a line drawn above a JPanel
}
}
class raysInteract extends MouseInputAdapter {
tomographyPanel panel;
ArrayList start, end;
int lev;
raysInteract(tomographyPanel panel) {
this.panel = panel;
lev=-1;
start = new ArrayList();
end = new ArrayList();
}
@Override
public void mouseClicked(MouseEvent e) {
Component[] jPanel = panel.getComponents();
int extLine = 20;
int wd,ht;
// Here I should save in start and end Arrays the starting points and end points of the lines. Not yet done
panel.repaint();
e.consume();
}
}
Re: Printing lines above JPanles
Quote:
Originally Posted by
imMadmartigan
Code:
public class INVERTgameLev1 extends JApplet {
tomographyPanel myPanel;
raysInteract raysClick;
:
:
}
class tomographyPanel extends JPanel{
:
:
}
class raysInteract extends MouseInputAdapter {
:
:
}
Learn and respect the Java coding conventions: class names start with an uppercase letter.
Code Conventions for the Java Programming Language: Contents
db
Re: Printing lines above JPanles
I ran your code (after refactoring it to conform to the conventions and to improve readability) and I see the line, partly covered by the contained JPanels.
What do you see and what did you expect?
db
Re: Printing lines above JPanles
Hi thanks for replying. Any correction is very welcome.
I expected a line passing above all the contained JPanels. But is not. I thought that, because the line is drawn as the last command, the line would have passed over everything, so I'm definitely stuck.
Tom
Re: Printing lines above JPanles
Read the API for JComponent#paint(...). What are the three methods it delegates to, and in what order?
Now do you understand?
Oh, and Java doesn't have commands. Java has statements.
db
Re: Printing lines above JPanles
So, it seems that paintComponent causes paint to be executed and thus: paintComponent(Graphics g), paintBorder(Graphics g) and paintChildren(Graphics g). The last one set the Panels I added in the object myPanel. So they are painted at the end. Am I right?
So I should need a Layered Panes or a Glass Pane to order the depth of each component or to print over everything.
Hoping it is right, thanks for driving me to the solution!! :)
Tom
Re: Printing lines above JPanles
Quote:
Originally Posted by
imMadmartigan
So, it seems that paintComponent causes paint to be executed
No, it's quite the opposite. The paint method will call paintComponent, paintBorder, and paintChildren. paintComponent will not call paint. Note that repaint() will usually result in a call to paint, and perhaps that is what you were referring to.
Quote:
So I should need a Layered Panes or a Glass Pane to order the depth of each component or to print over everything.
Not necessarily. If you're adding components to layers, then yes, this is helpful, but if you're just drawing, then this isn't necessary.