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


Groups > comp.lang.java.gui > #2718

Re: Graphics and JVMs

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!transit3.readnews.com!textspool1.readnews.com!news-out.readnews.com!news-xxxfer.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail
From "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Subject Re: Graphics and JVMs
Message-ID <7abdeb2ca253a@uwe> (permalink)
X-Comment-To comp.lang.java.gui
Newsgroups comp.lang.java.gui
In-Reply-To <1194233978.584277.256020@e9g2000prf.googlegroups.com>
References <1194233978.584277.256020@e9g2000prf.googlegroups.com>
Content-Type text/plain; charset=IBM437
Content-Transfer-Encoding 8bit
X-Gateway time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92]
Lines 132
Date Wed, 27 Apr 2011 15:40:46 GMT
NNTP-Posting-Host 96.60.20.240
X-Complaints-To news@tds.net
X-Trace newsreading01.news.tds.net 1303918846 96.60.20.240 (Wed, 27 Apr 2011 10:40:46 CDT)
NNTP-Posting-Date Wed, 27 Apr 2011 10:40:46 CDT
Organization TDS.net
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.gui:2718

Show key headers only | View raw


  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

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web