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


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

Re: How an I use a copy o

From "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Subject Re: How an I use a copy o
Message-ID <DIPmh.31667$2U4.31550@newsfe16.lga> (permalink)
Newsgroups comp.lang.java.gui
References <1167819356.183978.272880@48g2000cwx.googlegroups.com>
Date 2011-04-27 15:28 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
djubre@myway.com wrote:
> Knute Johnson wrote:
>> That is not how it works.  The component's Graphics is used to draw on
>> it.  Why don't you just put the JPanel with the drawing on it where you
>> want it to show up?
> 
> I need the Graphics2D instance to do some modifications. I can't change
> the original renedering because it is not my code.
> All I can do is obtain the Graphics2D instance with getGraphics(), and
> hope I can do someting with it.
> Can I make an image out of the Graphics2D instance.
> I think of the Graphics2D instance as a drawing surface. Can I
> transform it to an image? Can I use it somewhere else, after all the
> drawings were done.
> 

Extend the component that you want to draw on, call 
super.paintComponent() from the new components paintComponent method and 
then draw away.  This isn't going to work if it is some sort of active 
component like a button or something.

import java.awt.*;
import javax.swing.*;

public class test {
     public static void main(String[] args) {
         Runnable r = new Runnable() {
             public void run() {
                 class OriginalThing extends JPanel {
                     public OriginalThing() {
                         setPreferredSize(new Dimension(400,300));
                     }
                     public void paintComponent(Graphics g) {
                         g.setColor(Color.BLUE);
                         g.fillRect(0,0,getWidth(),getHeight());
                     }
                 }
                 class ThingIWantToDrawOnToo extends OriginalThing {
                     public void paintComponent(Graphics g) {
                         super.paintComponent(g);
                         g.setColor(Color.RED);
                         g.drawString("I drew this on the original!",20,30);
                     }
                 }

                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                 f.add(new ThingIWantToDrawOnToo());

                 f.pack();
                 f.setVisible(true);
             }
         };
         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


Thread

How an I use a copy of Gr "djubre" <djubre@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
  Re: How an I use a copy o "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
    Re: How an I use a copy o "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
  Re: How an I use a copy o "djubre" <djubre@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
    Re: How an I use a copy o "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
      Re: How an I use a copy o "djubre" <djubre@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
        Re: How an I use a copy o "Daniel Pitts" <daniel.pitts@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
        Re: How an I use a copy o "Fred Kleinschmidt" <fred.kleinschmidt@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
        Re: How an I use a copy o "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
          Re: How an I use a copy o "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
    Re: How an I use a copy o "Judy Szikora" <judy.szikora@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000

csiph-web