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


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

Re: Graphics and JVMs

From "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Subject Re: Graphics and JVMs
Message-ID <DxIXi.3405$Tp3.1077@newsfe15.lga> (permalink)
Newsgroups comp.lang.java.gui
References <1194276455.292195.36770@q3g2000prf.googlegroups.com>
Date 2011-04-27 15:40 +0000
Organization TDS.net

Show all headers | View raw


  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

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