Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #2717 > unrolled thread

Graphics and JVMs

Started by"sshark" <sshark@THRWHITE.remove-dii-this>
First post2011-04-27 15:40 +0000
Last post2011-04-27 15:40 +0000
Articles 15 — 4 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Graphics and JVMs "sshark" <sshark@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Graphics and JVMs "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
      Re: Graphics and JVMs "sshark" <sshark@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
        Re: Graphics and JVMs "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
          Re: Graphics and JVMs "sshark" <sshark@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
            Re: Graphics and JVMs "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
        Re: Graphics and JVMs "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
      Re: Graphics and JVMs "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Graphics and JVMs "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Graphics and JVMs "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
      Re: Graphics and JVMs "sshark" <sshark@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
        Re: Graphics and JVMs "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
          Re: Graphics and JVMs "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
            Re: Graphics and JVMs "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
              Re: Graphics and JVMs "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000

#2717 — Graphics and JVMs

From"sshark" <sshark@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
SubjectGraphics and JVMs
Message-ID<1194233978.584277.256020@e9g2000prf.googlegroups.com>
  To: comp.lang.java.gui
I have written this small test code to show the difference in the
graphical output of 2 different JVMs i.e. Apple JDK 1.5.0 and Windows
JDK 1.5.0_13. When I execute this code in Apple JVM, I can see the
message "Hi There" quite clearly. Whereas, in Windows JVM, the message
is harder be seen if I set Thread.sleep to 100. If I increase the
sleep period, I could see a blinking "Hi There" message. Is this has
to do with the way I have written the code (probably, I am missing
something here) or the JVMs implementation?  Either way, how can make
this work the way I wanted it? Please advise. Thanks.

[CODE]
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


    public class TestRandomBoxes extends JFrame {
        private Random pointRandomizer = new Random();

        public TestRandomBoxes() {

            final JPanel p = new JPanel() {
                @Override
                public void paint(Graphics g) {
                    g.setColor(getBackground());
                    g.fillRect(0, 0, 200, 200);
                    g.setColor(Color.BLACK);
                    g.fillRect(50 + randomPoint().x, 50 +
randomPoint().y, 50, 50);

                    drawMessage(getParent(), "Hi There");
                }
            };
            p.setPreferredSize(new Dimension(200, 200));

            Thread animator = new Thread() {
                @Override
                public void run() {
                    while (true) {
                        p.repaint();
                        try {
                            Thread.sleep(100); // reduce to increase
update speed
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            animator.start();

            getContentPane().add(new JScrollPane(p));
        }

        private void drawMessage(Container c, String message) {
            if (message.length() < 1) {
                return;
            }

            Graphics g = c.getGraphics();
            int w = g.getFontMetrics().stringWidth(message) + 10;
            int h = g.getFontMetrics().getHeight() + 4;

            g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
            g.fillRect(5, 5, w, h);
            g.setColor(new Color(0x33, 0xFF, 0xFF));
            g.drawRect(5, 5, w, h);
            g.setColor(Color.BLACK);
            g.drawString(message, 8, (5 + h - 4));
        }

        private Point randomPoint() {
            return new Point(pointRandomizer.nextInt(50),
                pointRandomizer.nextInt(50));
        }

        public static void main(String[] args) {
            TestRandomBoxes sl = new TestRandomBoxes();
            sl.setVisible(true);
            sl.pack();
            sl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            sl.setBounds(600,450, 100, 100);
        }
}
[/CODE]

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#2718

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<7abdeb2ca253a@uwe>
In reply to#2717
  To: comp.lang.java.gui
sshark@gmail.com wrote:
..
>...Is this has
>to do with the way I have written the code ..(?)

Yes.  At least, most of it is, since the code made a variety
of mistakes, such as using the 'push' model for Graphics.
Very unreliable and unpredictable behaviour (as you noticed).

Instead, the JFrame should be painted the same way you have 
the panel, by overriding the paint()/paintComponent() method.

There is still some flickering in this example*.  to remove that,
you might use an Image (more probably BufferedImage) as a 
backing store to write all the Graphics to, then simply 
Graphics.drawImage() it, once done.

Also, please watch the line length when posting to usenet.
It is best to keep line width under around 62 chars.  I fixed**
that for this example by moving the thread comment to the
previous line.

**Here is a tool that helps check text width.
<http://www.physci.org/twc.jnlp>

* E.G.
<sscce>
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

   public class TestRandomBoxes extends JFrame {
       private Random pointRandomizer = new Random();
       String message = "Hi There";

       public TestRandomBoxes() {

           final JPanel p = new JPanel() {
               @Override
               // override paintComponent() for Swing *
               public void paintComponent(Graphics g) {
                   g.setColor(getBackground());
                   g.fillRect(0, 0, 200, 200);
                   g.setColor(Color.BLACK);
                   g.fillRect(50 + randomPoint().x,
                     50 + randomPoint().y, 50, 50);
               }
           };
           p.setPreferredSize(new Dimension(200, 200));

           Thread animator = new Thread() {
               @Override
               public void run() {
                   while (true) {
                       // repaint the entire frame
                       repaint();
                       try {
                           // reduce to increase update speed
                           Thread.sleep(200);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
               }
           };
           animator.start();

           getContentPane().add(new JScrollPane(p));
       }

       // Except for Swing root components, use 'paint()'.
       // but then, it is unwise to be rendering directly
       // to root components.  Better to render to a
       // JPanel and add it to the root component.
       public void paint(Graphics g) {
           super.paint(g);

           if (message.length() < 1) {
               return;
           }

           int x=50;
           int y=50;

           int w = g.getFontMetrics().stringWidth(message)
             + 10;
           int h = g.getFontMetrics().getHeight() + 4;

           g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
           g.fillRect(x, y, w, h);
           g.setColor(new Color(0x33, 0xFF, 0xFF));
           g.drawRect(x, y, w, h);
           g.setColor(Color.BLACK);
           g.drawString(message, x, (y + h - 4));
       }

       private Point randomPoint() {
           return new Point(pointRandomizer.nextInt(50),
               pointRandomizer.nextInt(50));
       }

       public static void main(String[] args) {
           TestRandomBoxes sl = new TestRandomBoxes();
           sl.pack();
           sl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           sl.setBounds(100, 100, 300, 300);

           // move the setVisible to last..
           sl.setVisible(true);
       }
}
</sscce>

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200711/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2719

From"sshark" <sshark@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<1194276455.292195.36770@q3g2000prf.googlegroups.com>
In reply to#2718
  To: comp.lang.java.gui
Thank you guys for guidances. It works well for this example but it
doesn't fit into my final code. Not because of the technique used but
the component used to displayed the message. If I used JFrame,as
mentioned by Knute, the message will "eat into" the scroll bars when I
resize the frame to  smaller. At the same time, I would like to scale
the "drawing" in JPanel without scaling my message. My best guess is
to override JViewport's paint(...) method to do this. I didn't
success. I overrode the paint(Graphics g) or paintComponents(...)
method in JViewport but it doesn't work. Any pointers here?

Please forgive me for not mentioning this earlier.

Andrew,

I don't quite get what you commentede,

// Except for Swing root components, use 'paint()'.
// but then, it is unwise to be rendering
// to root components.  Better to render to a
// JPanel and add it to the root component.

I suppose JFrame is the root or the lowest component of all the
components it contains. If so, why do you override paint(...) instead
of paintComponents(...) in the sample code above?

Thanks

/lim/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2720

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<DxIXi.3405$Tp3.1077@newsfe15.lga>
In reply to#2719
  To: comp.lang.java.gui
sshark@gmail.com wrote:
> Thank you guys for guidances. It works well for this example but it
> doesn't fit into my final code. Not because of the technique used but
> the component used to displayed the message. If I used JFrame,as
> mentioned by Knute, the message will "eat into" the scroll bars when I
> resize the frame to  smaller. At the same time, I would like to scale
> the "drawing" in JPanel without scaling my message. My best guess is
> to override JViewport's paint(...) method to do this. I didn't
> success. I overrode the paint(Graphics g) or paintComponents(...)
> method in JViewport but it doesn't work. Any pointers here?
> 
> Please forgive me for not mentioning this earlier.
> 
> Andrew,
> 
> I don't quite get what you commentede,
> 
> // Except for Swing root components, use 'paint()'.
> // but then, it is unwise to be rendering
> // to root components.  Better to render to a
> // JPanel and add it to the root component.
> 
> I suppose JFrame is the root or the lowest component of all the
> components it contains. If so, why do you override paint(...) instead
> of paintComponents(...) in the sample code above?
> 
> Thanks
> 
> /lim/
> 

I'm not clear what you want to do with your message.  It is very easy 
however to scale the box drawings.  Assume a standard size, in this case 
400x300, and transform your Graphics component accordingly.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class test9 extends JComponent implements Runnable {
     private final Random random;
     private final int prefX=400, prefY=300;

     public test9() {
         random = new Random(new Date().getTime());
         setPreferredSize(new Dimension(prefX,prefY));
     }

     public void paintComponent(Graphics g2D) {
         Graphics2D g = (Graphics2D)g2D;
         double scaleX = getWidth() / (double)prefX;
         double scaleY = getHeight() / (double)prefY;
         int x = random.nextInt(prefX - 40);
         int y = random.nextInt(prefY - 40);
         AffineTransform at = g.getTransform();
         g.scale(scaleX,scaleY);  // adjust for scale
         g.setColor(Color.WHITE);
         g.fillRect(0,0,prefX,prefY);
         g.setColor(Color.BLACK);
         g.fillRect(x,y,40,40);
         g.setTransform(at);  // return to original
     }

     public void run() {
         while (true) {
             repaint();
             try {
                 Thread.sleep(200);
             } catch (InterruptedException ie) { }
         }
     }

     public static void main(String[] args) {
         Runnable r = new Runnable() {
             public void run() {
                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 test9 t9 = new test9();
                 f.add(t9,BorderLayout.CENTER);
                 f.pack();
                 f.setVisible(true);
                 new Thread(t9).start();
             }
         };
         EventQueue.invokeLater(r);
     }
}

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2724

From"sshark" <sshark@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<1194324481.487224.326510@y27g2000pre.googlegroups.com>
In reply to#2720
  To: comp.lang.java.gui
>
> I'm not clear what you want to do with your message.  It is very easy
> however to scale the box drawings.  Assume a standard size, in this case
> 400x300, and transform your Graphics component accordingly.
>
> ### code omitted ###
>
>
> --
>
> Knute Johnson
> email s/nospam/knute/

Yes I used the same setup to transform my graphics objects but like I
said I only want to scale the drawing in my panel but not the message
e.g. the boxes will be twice in size after scaling but the message
character size remains.

>From the help I got from here, I arrived to this and this is what I
wanted. I find that if I use double buffer method I will lose the
message box transparency. This is probably caused by drawImage(...)
method


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;


    public class TestRandomBoxes extends JFrame {
        private Random pointRandomizer = new Random();

        public TestRandomBoxes() {

            final JPanel p = new JPanel(){
                @Override
                public void paintComponent(Graphics g) {
                    g.setColor(getBackground());
                    g.fillRect(0, 0, 200, 200);
                    g.setColor(Color.BLACK);
                    g.fillRect(50 + randomPoint().x,
                        50 + randomPoint().y, 50, 50);
                }
            };
            p.setPreferredSize(new Dimension(200, 200));

            final JViewport viewport = new JViewport() {
                @Override
                public void paint(Graphics g) {
                    super.paint(g);
                    int x=50;
                    int y=50;
                    String message = "Hi! There";

                    int w = g.getFontMetrics().stringWidth(message)
                      + 10 ;
                    int h = g.getFontMetrics().getHeight() + 4;

                    g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
                    g.fillRect(x, y, w, h);
                    g.setColor(new Color(0x33, 0xFF, 0xFF));
                    g.drawRect(x, y, w, h);
                    g.setColor(Color.BLACK);
                    g.drawString(message, x, y + h - 4);

                    /* double buffering method
                    Image bufferImage = createImage(w + 1, h + 1);
                    Graphics buffer = bufferImage.getGraphics();
                    buffer.setColor(new Color(0x33, 0xFF, 0xFF, 50));
                    buffer.fillRect(0, 0, w, h);
                    buffer.setColor(new Color(0x33, 0xFF, 0xFF));
                    buffer.drawRect(0, 0, w, h);
                    buffer.setColor(Color.BLACK);
                    buffer.drawString(message, 0, h - 4);

                    g.drawImage(bufferImage, x, y, null);
                    buffer.dispose();
                    */
                }
            };
            viewport.add(p);
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setViewport(viewport);

            getContentPane().add(scrollPane);

            Thread animator = new Thread() {
                @Override
                public void run() {
                    while (true) {
                        repaint();
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            animator.start();
        }

        private void drawMessage(Graphics g, String message) {
            int w = g.getFontMetrics().stringWidth(message) + 10;
            int h = g.getFontMetrics().getHeight() + 4;

            g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
            g.fillRect(5, 5, w, h);
            g.setColor(new Color(0x33, 0xFF, 0xFF));
            g.drawRect(5, 5, w, h);
            g.setColor(Color.BLACK);
            g.drawString(message, 8, (5 + h - 4));
        }

        private Point randomPoint() {
            return new Point(pointRandomizer.nextInt(50),
                pointRandomizer.nextInt(50));
        }

        public static void main(String[] args) {
            TestRandomBoxes sl = new TestRandomBoxes();
            sl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            sl.setBounds(600,450, 100, 100);
            sl.validate();
            sl.setVisible(true);
        }
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2726

From"Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<5eTXi.1683$Od.1406@newsfe16.lga>
In reply to#2724
  To: comp.lang.java.gui
sshark wrote:
>> I'm not clear what you want to do with your message.  It is very easy
>> however to scale the box drawings.  Assume a standard size, in this case
>> 400x300, and transform your Graphics component accordingly.
>>
>> ### code omitted ###
>>
>>
>> --
>>
>> Knute Johnson
>> email s/nospam/knute/
> 
> Yes I used the same setup to transform my graphics objects but like I
> said I only want to scale the drawing in my panel but not the message
> e.g. the boxes will be twice in size after scaling but the message
> character size remains.
> 
>>From the help I got from here, I arrived to this and this is what I
> wanted. I find that if I use double buffer method I will lose the
> message box transparency. This is probably caused by drawImage(...)
> method
> 
> 
> import java.awt.Color;
> import java.awt.Dimension;
> import java.awt.Graphics;
> import java.awt.Point;
> import java.util.Random;
> 
> import javax.swing.JFrame;
> import javax.swing.JPanel;
> import javax.swing.JScrollPane;
> import javax.swing.JViewport;
> 
> 
>     public class TestRandomBoxes extends JFrame {
>         private Random pointRandomizer = new Random();
> 
>         public TestRandomBoxes() {
> 
>             final JPanel p = new JPanel(){
>                 @Override
>                 public void paintComponent(Graphics g) {
>                     g.setColor(getBackground());
>                     g.fillRect(0, 0, 200, 200);
>                     g.setColor(Color.BLACK);
>                     g.fillRect(50 + randomPoint().x,
>                         50 + randomPoint().y, 50, 50);
>                 }
>             };
>             p.setPreferredSize(new Dimension(200, 200));
> 
>             final JViewport viewport = new JViewport() {
>                 @Override
>                 public void paint(Graphics g) {
>                     super.paint(g);
>                     int x=50;
>                     int y=50;
>                     String message = "Hi! There";
> 
>                     int w = g.getFontMetrics().stringWidth(message)
>                       + 10 ;
>                     int h = g.getFontMetrics().getHeight() + 4;
> 
>                     g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
>                     g.fillRect(x, y, w, h);
>                     g.setColor(new Color(0x33, 0xFF, 0xFF));
>                     g.drawRect(x, y, w, h);
>                     g.setColor(Color.BLACK);
>                     g.drawString(message, x, y + h - 4);
> 
>                     /* double buffering method
>                     Image bufferImage = createImage(w + 1, h + 1);
>                     Graphics buffer = bufferImage.getGraphics();
>                     buffer.setColor(new Color(0x33, 0xFF, 0xFF, 50));
>                     buffer.fillRect(0, 0, w, h);
>                     buffer.setColor(new Color(0x33, 0xFF, 0xFF));
>                     buffer.drawRect(0, 0, w, h);
>                     buffer.setColor(Color.BLACK);
>                     buffer.drawString(message, 0, h - 4);
> 
>                     g.drawImage(bufferImage, x, y, null);
>                     buffer.dispose();
>                     */
>                 }
>             };
>             viewport.add(p);
>             JScrollPane scrollPane = new JScrollPane();
>             scrollPane.setViewport(viewport);
> 
>             getContentPane().add(scrollPane);
> 
>             Thread animator = new Thread() {
>                 @Override
>                 public void run() {
>                     while (true) {
>                         repaint();
>                         try {
>                             Thread.sleep(100);
>                         } catch (InterruptedException e) {
>                             e.printStackTrace();
>                         }
>                     }
>                 }
>             };
>             animator.start();
>         }
> 
>         private void drawMessage(Graphics g, String message) {
>             int w = g.getFontMetrics().stringWidth(message) + 10;
>             int h = g.getFontMetrics().getHeight() + 4;
> 
>             g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
>             g.fillRect(5, 5, w, h);
>             g.setColor(new Color(0x33, 0xFF, 0xFF));
>             g.drawRect(5, 5, w, h);
>             g.setColor(Color.BLACK);
>             g.drawString(message, 8, (5 + h - 4));
>         }
> 
>         private Point randomPoint() {
>             return new Point(pointRandomizer.nextInt(50),
>                 pointRandomizer.nextInt(50));
>         }
> 
>         public static void main(String[] args) {
>             TestRandomBoxes sl = new TestRandomBoxes();
>             sl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>             sl.setBounds(600,450, 100, 100);
>             sl.validate();
>             sl.setVisible(true);
>         }
> }
> 
> 

Your example doesn't scale the black box when the window is scaled.  I 
thought that is what you wanted to do?  What is the JScrollPane for? 
The whole thing doesn't make a lot of sense.

This scales the boxes when you resize the frame and leaves the 
transparent text box unchanged.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class test8 extends JComponent implements Runnable {
     private final Random random;

     public test8() {
         random = new Random(new Date().getTime());
         setPreferredSize(new Dimension(400,300));
     }

     public void paintComponent(Graphics g) {
         double scaleX = getWidth() / 400.0;
         double scaleY = getHeight() / 300.0;
         int x = random.nextInt(getWidth() - (int)(40 * scaleX));
         int y = random.nextInt(getHeight() - (int)(40 * scaleY));
         g.setColor(Color.WHITE);
         g.fillRect(0,0,getWidth(),getHeight());
         g.setColor(Color.BLACK);
         g.fillRect(x,y,(int)(40 * scaleX),(int)(40 * scaleY));

         x=50;
         y=50;
         String message = "Hi! There";
         int w = g.getFontMetrics().stringWidth(message) + 10 ;
         int h = g.getFontMetrics().getHeight() + 4;
         g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
         g.fillRect(x, y, w, h);
         g.setColor(new Color(0x33, 0xFF, 0xFF));
         g.drawRect(x, y, w, h);
         g.setColor(Color.BLACK);
         g.drawString(message, x, y + h - 4);
     }

     public void run() {
         while (true) {
             repaint();
             try {
                 Thread.sleep(200);
             } catch (InterruptedException ie) { }
         }
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 test8 t8 = new test8();
                 f.add(t8,BorderLayout.CENTER);
                 f.pack();
                 f.setVisible(true);
                 new Thread(t8).start();
             }
         });
     }
}

-- 

Knute Johnson
email s/nospam/knute/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2723

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<7aca8eb4483d6@uwe>
In reply to#2719
  To: comp.lang.java.gui
sshark@gmail.com wrote:
..
>I don't quite get what you commentede,
>
>// Except for Swing root components, use 'paint()'.
>// but then, it is unwise to be rendering
>// to root components.  Better to render to a
>// JPanel and add it to the root component.
>
>I suppose JFrame is the root or the lowest component of all the
>components it contains. If so, why do you override paint(...) instead
>of paintComponents(...) in the sample code above?

OK - this ties in with my advice to 'not paint to root GUI
components' (at all).  

1) As mentioned above, JPanel is much more verstatile.  A 
JPanel might be put in any 'root container' as well as other 
JPanels.  Putting one JPanel in another is probably what 
you would need to do, to get the effect you are after.

2) The 'paint()/paintComponent()' breakdown between Swing and the
AWT is effectively turned on its head by the root components
(so it adds confusion/complexity to the mix).

We might say "For AWT, override paint(), and for Swing, override
paintComponent()" ..except for the irritating fact that with Swing you
might *usually* override paintComponent(), except for those root
components where it is paint(), such as JFrame, JWindow, JDialog, 
JOptionPane.. no ..wait, JOP inherits from JComponent and *has* 
a paintComponent() method - another inconsistency.  A

ll up, I think it is best to advise "with Swing custom painting, 
override paintComponent(Graphics) unless the compiler tells 
you there is 'no such symbol' on the method name, then 
override paint(Graphics)."!

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200711/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2721

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<58nui3l8m4o5d3b1tirtlcqqah7foijv0q@4ax.com>
In reply to#2718
  To: comp.lang.java.gui
On Mon, 05 Nov 2007 04:29:49 GMT, "Andrew Thompson" <u32984@uwe>
wrote, quoted or indirectly quoted someone who said :

>
>Very unreliable and unpredictable behaviour (as you noticed).

 A few thoughts. 

1. with Swing you should override paintComponent not paint.
http://mindprod.com/jgloss/paintcomponent.html

2. You should do a setOpaque( true ) on your component since you paint
the entire window yourself. http://mindprod.com/jgloss/opaque.html

3. Your timer loop would be more idiomatically handled with a Swing
timer.  See http://mindprod.com/jgloss/timer.html 

Repaint is thread safe, but it never hurts to do all your Swing stuff
on the EDT  thread.
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2722

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<thnui3tm9edilvjekhfuln8p37pqtl3sf2@4ax.com>
In reply to#2717
  To: comp.lang.java.gui
On Mon, 05 Nov 2007 03:39:38 -0000, sshark@gmail.com wrote, quoted or
indirectly quoted someone who said :

>   sl.pack();
>   sl.setBounds(600,450, 100, 100);

This does not make sense. pack means shrink everything to minimal
size.  

I think what you meant is:
 sl.setBounds(600,450, 100, 100);
sl.validate();

or just plain:
sl.pack();
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2725

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<qvuvi3dps7v6eir0q1j6uef4js9nlebmds@4ax.com>
In reply to#2717
  To: comp.lang.java.gui
On Mon, 05 Nov 2007 03:39:38 -0000, sshark@gmail.com wrote, quoted or
indirectly quoted someone who said :

>  Graphics g = c.getGraphics();
>            int w = g.getFontMetrics().stringWidth(message) + 10;
>            int h = g.getFontMetrics().getHeight() + 4;
>
>            g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
>            g.fillRect(5, 5, w, h);
>            g.setColor(new Color(0x33, 0xFF, 0xFF));
>            g.drawRect(5, 5, w, h);
>            g.setColor(Color.BLACK);
>            g.drawString(message, 8, (5 + h - 4));
>        }

the indentations are not regular so it is hard to tell, but should
that code not be living inside some method?
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2727

From"sshark" <sshark@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<1194328309.987241.61750@i13g2000prf.googlegroups.com>
In reply to#2725
  To: comp.lang.java.gui
On Nov 6, 1:31 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Mon, 05 Nov 2007 03:39:38 -0000, ssh...@gmail.com wrote, quoted or
> indirectly quoted someone who said :
>
> >  Graphics g = c.getGraphics();
> >            int w = g.getFontMetrics().stringWidth(message) + 10;
> >            int h = g.getFontMetrics().getHeight() + 4;
>
> >            g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
> >            g.fillRect(5, 5, w, h);
> >            g.setColor(new Color(0x33, 0xFF, 0xFF));
> >            g.drawRect(5, 5, w, h);
> >            g.setColor(Color.BLACK);
> >            g.drawString(message, 8, (5 + h - 4));
> >        }
>
> the indentations are not regular so it is hard to tell, but should
> that code not be living inside some method?
> --
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com

The indentations are fine on Google Groups.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2743

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<4e0aj3p03ferb4mmbul0r0qddrf7o89otk@4ax.com>
In reply to#2727
  To: comp.lang.java.gui
On Tue, 06 Nov 2007 05:51:49 -0000, sshark <sshark@gmail.com> wrote,
quoted or indirectly quoted someone who said :

>The indentations are fine on Google Groups.

could be you are using tabs.  Use spaces to make sure everyone can
read it.
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2744

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<7afb9c58d4a1a@uwe>
In reply to#2743
  To: comp.lang.java.gui
Roedy Green wrote:
>>The indentations are fine on Google Groups.
>
>could be you are using tabs.  ...

Nope.  I went and rechecked the OP's first post after your initial
claim.  It had one wrapped line.  Once that was fixed, it had a 
consistent indentation.  There were *no* tab characters in the 
source anywhere.

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200711/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2749

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<09iej31o3rdfo9gmcpdunufi0in3n451t9@4ax.com>
In reply to#2744
  To: comp.lang.java.gui
On Sat, 10 Nov 2007 02:15:30 GMT, "Andrew Thompson" <u32984@uwe>
wrote, quoted or indirectly quoted someone who said :

>Nope.  I went and rechecked the OP's first post after your initial
>claim.  It had one wrapped line.  Once that was fixed, it had a 
>consistent indentation.  There were *no* tab characters in the 
>source anywhere.

Looking at it again, you are right. I am used to deeper indentation.
For some reason it appear as jumbled to my eyes rather than just
shallow indent.
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#2750

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:40 +0000
Message-ID<7b111a91273b6@uwe>
In reply to#2749
  To: comp.lang.java.gui
Roedy Green wrote:
>>Nope.  
..
>Looking at it again, you are right. I am used to deeper indentation.
>For some reason it appear as jumbled to my eyes rather than just
>shallow indent.

I am generally replacing tabs with just 2 chars each, prior 
to posting code samples nowadays.

I thought (almost presumed) that everybody besides me was
using some super-duper megala-IDE that could instamagically
reformat code to their own tastes.  

Go figure.

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200711/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web