• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

access JPanel s paint-method

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.awt.Graphics;

import javax.swing.JPanel;

public class Panel1 extends JPanel{

String s;

public void paint(Graphics g) {
g.drawString(s, 50, 50);
g.fillRect(10,10,100,100);
}
}


import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestGraphics extends JFrame {

JPanel panel = new JPanel();

Panel1 panel1;

Panel1 panel2;

public TestGraphics() {

panel1 = new Panel1();
panel1.setPreferredSize(new Dimension(100, 100));
panel1.s = "panel1....";
panel1.getGraphics().drawString("eeee", 100, 100);
panel.add(panel1);

panel2 = new Panel1();
panel2.setPreferredSize(new Dimension(200, 100));
panel2.s = "panel2...";
panel.add(panel2);

panel2.s = "panel2...upsa";

getContentPane().add(panel);

setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

/**
* @param args
*/
public static void main(String[] args) {
new TestGraphics();

}
}

Hello

i want to test the access to JPanels paint-method...

is something like above possible? - printed in bold letters .

thanks andrea
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
never use getGraphics() (well, rarely anyway)
1) it is null until the component is visible
2) if you (e.g.) minimize then maximize the frame, the graphics you have
created do not get redrawn - repaint() wipes 'em out.

> public void paint(Graphics g)
never, ever override paint() in a JComponent - you will end up with very odd behavior
instead, override paintComponent(..), and, just about always, have as the first line
super.paintComponent(g);

[edit]
just seen this post had been answered earlier in sun's forum, and I have
just wasted my time with the reply.

won't happen again.
[ November 23, 2006: Message edited by: Michael Dunn ]
 
andrea regina buegler
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mr Dunn,

i know that the code seems to be rarely...
But I only wanted to know if something like this is possible....
to use getGraphics()....You don't waste your time.

I only thaught ask the question in several groups brings more information for me.

Thank you for the answer... even if wasn't very friendly.
a,buegler
 
reply
    Bookmark Topic Watch Topic
  • New Topic