• 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

paint() problems

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

i am interest in Swing, following is a part of code copied from a website which discuss paintImmediately().


1public void actionPerformed(ActionEvent e) {
2if (e.getActionCommand().equals("repaint")) jpl.repaint();
3else jpl.paintImmediately(50 , 50 , 100 , 100);
4
5try { Thread.sleep(3000); }
6catch(Exception err) {}
7}
8
9class JPanelEx extends JPanel {
10int backColor = 0xFF; //0xFF color blue
11
12public void paint(Graphics g) {
13
14//0xFF0000 color red
15backColor = (backColor == 0xFF0000 ? 0xFF : 0xFF0000);
16Dimension dim = getSize();
17g.setColor(new Color(backColor));
18g.fillRect(0 , 0 , dim.width , dim.height);
19}
20}

however something i can't understand. First, the paint() method has been called twice, why? Will all paint() methods in Swing be called twice? Second, when the paint() method first call, it will paint a blue rectangle, but not red, unless i call repaint(), why?

jack
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

In your haste to come in and ask a question, you seem to have missed reading our policy on display names, which quite clearly states that you must use a real (sounding) first and last name for your display name -- no joke names, "handles," or last initials are acceptable. You can fix your display name here. We take this rule rather seriously. Thanks for your cooperation!

Now, as to your problem: in Swing, you never override the paint(Graphics) method -- you instead override paintComponent(Graphics). Further, the first line of your paintComponent() method should always be a call to super.paintComponent(). If you override paint() on a Swing component, or don't call super.paintComponent(), all manner of strange things can happen. Change your code to override the correct painting method, and then see how it behaves. Until then, you're simply treading in the murky waters of undefined behavior.
[ September 03, 2005: Message edited by: Ernest Friedman-Hill ]
 
jack davis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Thanks for ur reply, i rewrite the code that i have posted, following is the complete code, i add a debug code on line 41 which will print two lines to STDOUT, it proofs that the paintComponent() will be called twice. i think the paintComponent() first call on line 54, second call on line 18, i don't know it is right or not. i also can't understand why a blue rectangle will be painted first. Due to line 42, i think a red rectangle will be painted at the first call. Any idea? Thanks




1 public class PaintImmediatelyTest extends JPanel
2 implements ActionListener{
3
4private JButton bt1;
5private JButton bt2;
6private JPanel jpl;
7
8public PaintImmediatelyTest() {
9
10setLayout(new FlowLayout());
11
12bt1 = new JButton("paintImmediately");
13bt2 = new JButton("repaint");
14
15bt1.addActionListener(this);
16bt2.addActionListener(this);
17
18jpl = new JPanelEx();
19jpl.setPreferredSize(new Dimension(200 , 200));
20
21add(bt1);
22add(bt2);
23add(jpl);
24}
25
26public void actionPerformed(ActionEvent e) {
27if (e.getActionCommand().equals("repaint")) jpl.repaint();
28else jpl.paintImmediately(50 , 50 , 100 , 100);
29
30try { Thread.sleep(3000); }
31catch(Exception err) {}
32}
33
34class JPanelEx extends JPanel {
35int backColor = 0xFF; //0xFF is blue, 0xFF0000 is red
36
37public void paintComponent(Graphics g) {
38
39super.paintComponent(g);
40
41System.out.println(backColor);
42backColor = backColor == 0xFF0000 ? 0xFF : 0xFF0000;
43Dimension dim = getSize();
44g.setColor(new Color(backColor));
45g.fillRect(0 , 0 , dim.width , dim.height);
46}
47}
48
49public static void createAndShowGUI() {
50
51try {
52 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
53 } catch (Exception evt) {}
54 JFrame f = new JFrame("PaintImmediatelyTest");
55 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
56 JComponent newContentPane = new PaintImmediatelyTest();
57 newContentPane.setOpaque(true);
58
59 f.setContentPane(newContentPane);
60
61 f.setSize(500, 400);
62 f.pack();
63 f.setVisible(true);
64}
65
66public static void main(String[] args) {
67
68javax.swing.SwingUtilities.invokeLater(new Runnable() {
69 public void run() {
70 createAndShowGUI();
71 }
72 });
73}
74}
 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just out of interest, what was wrong with the name 'jack davis'. Doesn't sound ficticious to me
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Has it occurred to you that he might have changed it?
reply
    Bookmark Topic Watch Topic
  • New Topic