Object Graphics deleted on paint component

Hello everybody!
I have a question about the object graphics when paint a component in it. I'm trying to paint a component in a image, and after show it in the screen.
The problem is that in order to make it possible, I have to put the function that paint the image inside the code paint() of the principal class.
With this code in one class named: Program:
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g.create();      
            gt = new GraphicT(view);   <-HERE IS THE PROBLEM
            g2.setColor(getBackground());
            g2.fillRect(0,0,getWidth(),getHeight());                               
            g2.drawImage(gt.getImage(), 0, 0, this);
...and with this other in other named: GraphicT:
private JComponent view;
private Image image;
private Graphics gaux;
public GraphicT(JComponent view){
        this.view = view;
        this.image = new BufferedImage (view.getWidth(),view.getHeight(),BufferedImage.TYPE_INT_BGR);
        gaux = image.createGraphics();
      //  gaux=image.getGraphics();
        this.view.paint(gaux);       
public BufferedImage getImage(){              
        return this.image;
...I don't want to do it because each time that Program paint , create the image (object) in GraphicT. I want to create the image only one time, I mean to put the class GraphicT inside the Program like a object private. But if I do this, program doesn't paint anything.
Do you know why about this behaviour?
Thanks everybody
Edited by: ginnkk on Feb 5, 2009 3:15 AM

So you want to paint a component to an image and display the image. The reason it works if you put it in the paint() method is becasuse by that time the component has already been made displayable, approprietly sized, and consequently can be painted.
If all you are doing is painting an ordinary JComponent, then it's the approprietly sized part that matters.
Dimension prefSize = myJComponent.getPreferredSize();
myJComponent.setSize(prefSize.width,prefSize.height);
myJComponent.doLayout();
GraphicT gt = new GraphicT(myJComponent);This is more or less the code required to paint the component outside the paint method right after the JComponent has been constructed.

Similar Messages

  • Paint from a paint(Component) method? Is it possible?

    Hi all
    Here's my deal: I'd like to make a Paint (http://java.sun.com/javase/6/docs/api/java/awt/Paint.html) out of a component or even better, just something with a paint(Graphics) method. I looked into implementing my own Paint, but it looks very foreboding, esp the requirement to be able to get the Raster in the PaintContext object which I think I'd have to implement too.
    Any advice?

    Here's my attempt at it...
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Paint;
    import java.awt.PaintContext;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.awt.image.ColorModel;
    import java.awt.image.Raster;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import java.lang.reflect.Method;
    public class ImagePaint implements Paint {
        Object paintable;
        Method paintMethod;
        BufferedImage paint;
        public ImagePaint(Object paintable) {
            this.paintable = paintable;
            try{
                paintMethod =
                        paintable.getClass().getMethod("paint",Graphics.class);
            }catch(java.lang.NoSuchMethodException e) {
                throw new IllegalArgumentException("Paintable object does " +
                        "not have paint method!");
        public java.awt.PaintContext createContext(ColorModel cm,
                                                   Rectangle deviceBounds,
                                                   Rectangle2D userBounds,
                                                   AffineTransform xform,
                                                   RenderingHints hints) {
            /*the bounds being passed to this method is the bounding rectangle
             *of the shape being filled or drawn*/
           paint = new BufferedImage(deviceBounds.width,
                                     deviceBounds.height,
                                     BufferedImage.TYPE_3BYTE_BGR);
               /*note: if translucent/transparent colors are used in the
                *paintable object's "paint" method then the BufferedImage
                *type will need to be different*/
            Graphics2D g = paint.createGraphics();
                /*set the clip size so the paintable object knows the
                 *size of the image/shape they are dealing with*/
            g.setClip(0,0,paint.getWidth(),paint.getHeight());
            //call on the paintable object to ask how to fill in/draw the shape
            try {
                paintMethod.invoke(paintable, g);
            } catch (Exception e) {
                throw new RuntimeException(
                   "Could not invoke paint method on: " +
                        paintable.getClass(), e);
            return new ImagePaintContext(xform,
                                         (int) userBounds.getMinX(),
                                         (int) userBounds.getMinY());
        public int getTransparency() {
            /*Technically the transparency returned should be whatever colors are
             *used in the paintable object's "paint" method.  Since I'm just
             *drawing an opaque cross with this aplication, I'll return opaque.*/
            return java.awt.Transparency.OPAQUE;
        public class ImagePaintContext implements PaintContext {
            AffineTransform deviceToUser;
            /*the upper left x and y coordinate of the where the shape is (in
             *user space)*/
            int minX, minY;
            public ImagePaintContext(AffineTransform xform, int xCoor,
                                                            int yCoor){
                try{
                    deviceToUser = xform.createInverse();
                }catch(java.awt.geom.NoninvertibleTransformException e) {}
                minX = xCoor;
                minY = yCoor;
            public Raster getRaster(int x, int y, int w, int h) {
                /*find the point on the image that the device (x,y) coordinate
                 *refers to*/
                java.awt.geom.Point2D tmp =
                        new java.awt.geom.Point2D.Double(x,y);
                if(deviceToUser != null) {
                    deviceToUser.transform(tmp,tmp);
                }else{
                    tmp.setLocation(0,0);
                tmp.setLocation(tmp.getX()-minX,tmp.getY()-minY);
                /*return the important portion of the image/raster in the
                 * coordinate space of the device.*/
                return paint.getRaster().createChild((int) tmp.getX(),
                                                     (int) tmp.getY(),
                                                     w,h,x,y,null);
            public ColorModel getColorModel() {
                return paint.getColorModel();
            public void dispose() {
                paint = null;
        public static void main(String[] args) {
            /*as of java 1.6.10 d3d is enabled by default.  The fillRect
             * commands are significantly slower with d3d.  I think this is
             * a known bug.  Also, with d3d enabled I get all sort
             * weird painting artificats for this particular demonstration*/
            System.setProperty("sun.java2d.d3d","false");
            final ImagePaint paint = new ImagePaint(new Object() {
                public void paint(Graphics g) {
                    Rectangle r = g.getClipBounds();
                    g.setColor(Color.white);
                    g.fillRect(r.x,r.y,r.width,r.height);
                    //draw a red cross
                    g.setColor(Color.red);
                    g.fillRect(r.width/2-20,0,40,r.height);
                    g.fillRect(0,r.height/2-20,r.width,40);
                    g.dispose();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JComponent p = new JComponent() {
                public void paintComponent(Graphics g) {
                    Graphics2D g2d = (Graphics2D) g;
                    g2d.setPaint(paint);
                    g2d.fillOval(getWidth()/3,getHeight()/3,
                                 getWidth()/3,getHeight()/3);
                    g2d.dispose();
            p.setPreferredSize(new Dimension(500,500));
            f.setContentPane(p);
            f.pack();
            f.setVisible(true);
    }I don't think the unresponsiveness from larger areas is necessarily due to your code. For all of my GUI applications, resizing an
    already large frame tends to be prety sucky while resizing an already small frame tends to be pretty smooth.

  • Paint component inside JPanel

    Hi guys,
    I will start with the original problem and get to the only option that seems possible before hardcoding everything.
    I need to draw something like a binary tree. Jtree doesn't fall into binary because it does not have the root in the center. <1;2;6;8;9>, where 6 is the root, and 1,2 are its left child's children and 8,9 are right child's children. Since I couldn't find any possible solution of customizing JTree to draw it in binary format, I am left with the following option that is a step towards hardcoding.
    Create a subclass of JPanel and paint inside it. The full hardcode method would be to draw everything right here, ie the lines and the boxes.
    But since I need to listen to box selections using mouse, I was hoping to draw the boxes using JPanel/JLabel and override their paint() method.
    So is there a way to paint components(JLabel/JPanel and lines) into a painted component (JPanel)? Note, the container JPanel is not using any layout. everything is being drawn using paint method.

    You are probably going to want to create smaller objects that handle their own painting. For example... Create an extension of JComponent that handles painting a single node. Then create the JPanel to add those to. You can even go as far as writing your own LayoutManager to handle laying the Components out in your binary layout. This is really the most effective way to do it, especially if you want to be able to detect clicks and what not. You can just add MouseListener's to the individual components than.
    Hope this helps,
    Josh Castagno
    http://www.jdc-software.com

  • Paint component scope issues

    hi i have 2 classes one is my main and the other is the paint class with void paintcomponent() inside
    how do i pass the x variable inside main class so i can use it inside the Map class
    i know you can use scope e.g. Map map = new Map(x,y); but then i have to use a constructor with
    its own repaint(); method inside but it does not work.
    i was thinking of overloading a constructor aswell so inside one constructor is nothing and in the other with the (x, y) has
    the repaint method inside but that doesn't work either.
    main class
    public class Main
    public static void main(String[] args) {
    MainFrame frame = new MainFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);
    class MainFrame extends JFrame
    private int x;
    private int y;
    // map class with paint component
    Map map = new Map(x,y);
    public MainFrame(){
    this.add(map);
    }paint class
    public class Map extends JPanel {
    private int x;
    private int y;
    public Map(){}
    public Map(int x, int y){
    this.x = x;
    this.y = y;
    repaint();
        @Override
        public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }this does not work, any ideas?
    Edited by: nicchick on Nov 25, 2008 1:00 AM

    um that wasnt exactly what i was looking for i have a better example
    what im trying to do is pass/scope private varibles from the main class to the map class
    but since you use repaint(); to call the paintcomponent override method theres no way to scope varibles like
    x and y to panel?
    this example shows what im trying to do with a mouselistener
    main
    public class Main
    public static void main(String[] args) {
    MainFrame frame = new MainFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);
    class MainFrame extends JFrame
    private int x;
    private int y;
    // map class with paint component
    Map map = new Map();
    // handler
    HandlerMotion handler = new HandlerMotion();
    public MainFrame(){
    this.add(map);
    map.addMouseMotionListener(handler);
         private class HandlerMotion implements MouseMotionListener
            public void mouseDragged(MouseEvent e) {
            public void mouseMoved(MouseEvent e) {
            x = e.getX();
            y = e.getY();
            new Map(x,y);
            repaint();
    }map
    public class Map extends JPanel {
        private int x;
        private int y;
        public Map(){}
        public Map(int x, int y)
        this.x = x;
        this.y = y;
        System.out.println("this is map" + x);
        System.out.println("this is map" + y);
        repaint();
        @Override
        public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawLine(0, 0, x, y);
        System.out.println("this is panel" + x);
        System.out.println("this is panel" + y);
    }

  • Paint Component

    Hi all
    i have four classes plot , plot3d, plotsurface and test
    plot is derived from JPanel and plot3d and plotsurface extend plot class
    i am trying to do a very simple operation . just making a tool bar with 2 buttons to switch between plots .
    the 2 plot classes plot3d and plotsurface just draw a line on the screen
    problem is that when the user clicks one button it doesnot show up on the screen but when the window is resized it does call paint component of the corresponding class
    can Any one explain why this is happening......
    package test;
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    public abstract class plot extends JPanel {
    plot()
    public void paintComponent(Graphics g)
    Graphics2D graphics2D=(Graphics2D)g;
    super.paintComponent(g);
    package test;
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    public class plot3d extends plot {
    plot3d(boolean resizable)
    public void paintComponent(Graphics g)
    Graphics2D graphics2D=(Graphics2D)g;
    super.paintComponent(g);
    graphics2D.drawLine(200,200,320,320);
    package test;
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    public class plotsurface extends plot {
    plotsurface(boolean resizable)
    public void paintComponent(Graphics g)
    Graphics2D graphics2D=(Graphics2D)g;
    super.paintComponent(g);
    graphics2D.drawLine(120,120,140,140);
    package test;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.Graphics2D.*;
    import java.applet.*;
    public class Test extends javax.swing.JApplet {
    private Container container;
    public Rectangle rect;
    JPanel toolBar;
    JButton contourButton;
    JButton surfaceButton;
    JPanel toolPanel;
    public Graphics g;
    public test.plot3d graph3D;
    public test.plotsurface graphSurface;
    private int plotType=0;
    private void graph3D(Graphics2D g2)
    graph3D=new plot3d(false);
    public void graphSurface(Graphics2D g2)
              graphSurface=new plotsurface(false);
    private void changeplottoContour()
    if(plotType==0)
    return;
    plotType=0;
    g=container.getGraphics();
    Graphics2D g2=(Graphics2D)g;
    container.removeAll();
    repaint();
    graphSurface=null;
    System.gc();
    graph3D(g2);
    container.add(toolPanel,BorderLayout.NORTH);
    container.add(graph3D,BorderLayout.CENTER);
    private void changeplottoSurface()
    if(plotType==1)
    return;
    plotType=1;
    g=container.getGraphics();
    Graphics2D g2=(Graphics2D)g;
    container.removeAll();
    repaint();
    graph3D=null;
    System.gc();
    graphSurface(g2);
    container.add(toolPanel,BorderLayout.NORTH);
    container.add(graphSurface,BorderLayout.CENTER);
    private void surfaceButtonActionPerformed(java.awt.event.ActionEvent evt)
         changeplottoSurface();
         repaint();
    private void contourButtonActionPerformed(java.awt.event.ActionEvent evt)
         changeplottoContour();
         repaint();
    public void init()
         container=getContentPane();
    g=container.getGraphics();
    Graphics2D g2=(Graphics2D)g;
    Rectangle r1= new Rectangle();
    rect=new Rectangle();
    //r1=container.getBounds();
    toolPanel= new JPanel(new BorderLayout());
    toolBar = new JPanel();
    contourButton = new JButton("Contour");
    toolBar.add(contourButton);
    contourButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    contourButtonActionPerformed(evt);
    surfaceButton = new JButton("Surface Plot");
    toolBar.add(surfaceButton);
    surfaceButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    surfaceButtonActionPerformed(evt);
    toolBar.setBackground(Color.white);
    toolPanel.add(toolBar,BorderLayout.NORTH);
    container.add(toolPanel,BorderLayout.NORTH);
    Rectangle r2= toolPanel.getBounds();
    Dimension appletSize = this.getSize();
    int appletHeight= appletSize.height;
    int appletWidth= appletSize.width;
              rect.setBounds(0,(int)r2.getHeight(),appletWidth,appletHeight-(int)r2.getHeight());
    plotType=0;
         graph3D(g2);
         container.add(graph3D,BorderLayout.CENTER);

    in your button action listeneres (e.g. contourButtonActionPerformed()) don't only call repaint(), but update(this.getGraphics());
    this should help in most cases. other refreshing methods are:
    java -Dsun.java2d.noddraw=true HelloWorld
    java.awt.Component.repaint()
    java.awt.Component.update(Graphics) (e.g. c.update(c.getGraphics());)
    java.awt.Component.validate()
    javax.swing.JComponent.revalidate()
    javax.swing.JComponent.updateUI()
    javax.swing.SwingUtilities.updateComponentTreeUI(java.awt.Component)

  • How to trace if objects are deleted from a scenario

    Hi Experts,
    Can you please tell me if some of my objects get deleted from my scenario how can I trace? like by which user this is done or any log is there from where we can find this one?
    Thanks
    Sugata B

    Just run the scenario if you are aware of the entire process by disabling target system adapter (if needed)
    then you can easily trace it out.
    If you know details about mapping go to SXI_CACHE and chk there, for  objects of your scenario
    Rajesh

  • Keep the record in the view object if deletion failed.

    I have a view object which is based on the entity object, when I am trying to delete a row it failed because it has a child record associate with. I know the row is not delete from the database, but the record is deleted fromt the view.
    How to keep the record in the View object if delete failed?

    Hi,
    the row is not delete from the database,refresh the view . re-execute query and the view show the row

  • Objects for deletion included in support package are not deleted

    Hi experts,
    How can i create a support package using Add-on Assembly Kit that deletes several objects?
    This is the case:
    We have DEV, QA and PROD systems. The objects are deleted on DEV and the transports are released. The transports are automatically imported on QA and the objects are deleted -checked they are gone. A support package of the product is created on QA including the transports that delete the objects. The SP is released successfully and imported on PROD successfully. The objects that should be deleted still exist on PROD. I've checked the logs for errors - no errors found.
    Do you have any idea what could be the reason?
    Best regards,
    Petar

    Note number 1581093.
    As mentioned in the symptom. Version 2 of the note is created to extend the functionality provided in the Version 1. Version 3 of the note is created to correct the error in Version 2.
    Information from SPAM
    SAP_HR     600     0054     SAPKE60054     Human Resources
    How can I find the information required if neeed?
    WHen I try to reset the implementation I get the message
    No correction instructions implemented for SAP
    Note 0001581093
    Edited by: Wojciech Walczak ITMAXI.COM on May 25, 2011 12:00 PM

  • Sapscript: position OBJECT GRAPHICS ID

    Hi all,
    I hope there is someone can help me...
    My problem is: i've used the istruction to insert an image in a Sapscript
    /: BITMAP 'LOGO_TOPAZIO_DATI1' OBJECT GRAPHICS ID BMAP TYPE BCOL
    now, how can i position the image in the centre of page?
    Thanks a lots,
    regards,
    Alex.

    Hi,
    To print image, you have to import a tif file from your local hardisk via the SAP abap program RSTXLDMC.
    In the sap script, add in the following script.
    : / INCLUDE ZLOGO OBJECT TEXT ID ST LANGUAGE EN.
    or
    Tested in 4.6x
    Go to transaction SE78.
    Follow the path given below (on the left windows box) :-
    SAPSCRIPT GRAPHICS --> STORED ON DOCUMENT SERVER --> GRAPHICS --> BMAP
    Enter the name of the graphics file on the right side and Go to the menu GRAPHIC --> IMPORT.
    Thanks,
    Neelima.

  • Temporary objects are deleted incorrectly

    The temporary objects are deleted (their destructors called) when they are leaving the scope of the expression due to wich they were created. But the Standard says that a temporary object should be deleted when the full expression due to wich it was created is completely evaluated (if no reference to it was created).
    Consider the following example:
    #include <iostream>
    using namespace std;
    class A
    public:
    A() {
      cout << "A constructor" << endl;
    ~A() {
      cout << "A destructor" << endl;
    A foo() {
    cout << "foo has been called" << endl;
    return A();
    void foo2(A const& a) {
    cout << "foo2 has been called" << endl;
    int main(int argc, char *argv[], char *envp[])
    cout << "main started" << endl;
    foo2(foo()); // BUG
    cout << "main continued execution" << endl;
    return 0;
    }This test makes the following output:
    main started
    foo has been called
    A constructor
    foo2 has been called
    main continued execution
    A destructor
    As you may see, the temporary object of type A created as a return value of function foo() is deleted on main() exit, not on finishing evaluation of the expression "foo2(foo());".
    The expected output is:
    main started
    foo has been called
    A constructor
    foo2 has been called
    A destructor
    main continued execution
    Actually, if the line marked as BUG is enclosed in {} brackets the desired output is achieved.

    For compatibility with older compilers, the compiler by default destroys temporary objects at the end of the block in which they are destroyed.
    To get standard-conforming behavior, use the compiler option
    -features=tmplife
    on each module where temporary lifetime makes a difference.

  • Text object gets deleted in Adobe Reader 11

    In Adobe Acrobat X i was inserting text objects to one of the pdf . It opens properly in Adobe Reader 10, but in Adobe Reader 11, when I click on that PDF file, text objects which i have edited gets deleted.
    Here is the source pdf file link which i am editing(http://incometaxsoft.com/pdf/Src.pdf)
    This the pdf file  link which is already edited and for which text gets deleted when double click on pdf in adobe reader 11(http://incometaxsoft.com/pdf/result.pdf)
    So how can i prevent text object from deleting?

    The problem is not only seen in Reader XI. It is also seen in Acrobat Pro 9 and 10. The problem is caused by incorrect modifications to the PDF.
    It breaks this principle: if software creates an appearance stream for a "standard" annotation it must match the standard appearance of the annotation exactly. Software which supports annotation is free to use EITHER the appearance stream OR the annotation dictionary to show it on screen, and it is not predictable which one is used.
    The problem is that the original PDF contains a "FreeText" annotation with the text "WWWW" in top left. When you say the text is deleted that is not a full description of the problem. The new text disappears but the original WWWW reappears.
    This has started happening in Reader XI because it now has support for editing FreeText annotations, so it is doing the correct thing and rebuilding it from the dictionary.
    The editing solution is simple and neat. Unfortunately it is also fundamendally flawed because
    (a) the original annotation dictionary is not changed to match
    (b) in any case the free text annotation cannot express a detailed filling in of multiple locations
    If this had been tested using Acrobat the problem would have been seen before.
    I recommend you add actual page contents to the page.

  • Place to check deleted enhanced BSP component

    Hello everyone,
    Is there any place where we can check the details of deleted enhanced BSP component?
    For ex : Suppose I have a component ERP_INCOMP, and its enhancement storage component is ZERP_INCOMP..
    Now suppose someone has deleted this Z component, and I want to check the details or the change log for this..
    Is it possible. Kindly let me know if there is any possibility for this.
    Regards,
    Devashish

    Hello Devashish,
    Not sure about deletion but you can check change log in BSPC_DL_XMLSTRX2 table for custom config.
    Regards Harish Kumar

  • Insert Object graphic in standart text

    Hi Experts,
    I need put object graphic in the middle of line in standart text. When I insert this object it make 3 lines instade one. Could You help me??
    Example:
    Test A: < OBJECT GRAPHIC > OK
    Thanks in advance,
    Marcin
    Edited by: Marcin Szlenk on Feb 26, 2008 12:54 PM
    Edited by: Marcin Szlenk on Feb 26, 2008 12:55 PM

    Well, I found a solution.
    Instead of:
    p.addChild(inlineGraphic);
    where:
    p is my paragraphElement assigned to my textFlow
    inlineGraphic is my InlineGraphicElement with source, width and height
    I use:
    edition.insertInlineGraphic(inlineGraphic.source, inlineGraphic.width, inlineGraphic.height);
    where:
    edition is my EditionManager assigned to my TextFlow
    inlineGraphic is my InlineGraphicElement with source, width and height
    Now, I can insert graphics anywhere my cursor is placed.

  • Paint Component using scaled Graphics with TiledImage

    Hello,
    I am in the process of adding minor image manipulation in an application I created. I have paint overridden on the component that displays the image and in that component I am scaling the graphics to a representation of the image at 300 dpi versus the screen resolution. The image is a TiledImage which gave me pretty good performance benefits when I was painting at screen resolution. Now I want to know if there is something I can do to the tiles to get the performance benefit back. I understand this
    may not be possible because the entire image displays at once with the scaled graphics instance.
    Here is a copy of my paint method:
    public void paintComponent(Graphics gc) {
              Graphics2D g = (Graphics2D) gc;
              g.scale(scale, scale);
              Rectangle rect = this.getBounds();
              rect.width *= (1/scale);
              rect.height *= (1/scale);
              if ((viewerWidth != rect.width) || (viewerHeight != rect.height)) {
                   viewerWidth = rect.width;
                   viewerHeight = rect.height;
              setDoubleBuffered(false);
              g.setColor(Color.BLACK);
              g.fillRect(0, 0, viewerWidth, viewerHeight);
              if (displayImage == null)     return;
              int ti = 0, tj = 0;
              Rectangle bounds = new Rectangle(0, 0, rect.width, rect.height);
              bounds.translate(-panX, -panY);
              int leftIndex = displayImage.XToTileX(bounds.x);
              if (leftIndex < ImageManip.getMinTileIndexX())
                   leftIndex = ImageManip.getMinTileIndexX();
              if (leftIndex > ImageManip.getMaxTileIndexX())
                   leftIndex = ImageManip.getMaxTileIndexX();
              int rightIndex = displayImage.XToTileX(bounds.x + bounds.width - 1);
              if (rightIndex < ImageManip.getMinTileIndexX())
                   rightIndex = ImageManip.getMinTileIndexX();
              if (rightIndex > ImageManip.getMaxTileIndexX())
                   rightIndex = ImageManip.getMaxTileIndexX();
              int topIndex = displayImage.YToTileY(bounds.y);
              if (topIndex < ImageManip.getMinTileIndexY())
                   topIndex = ImageManip.getMinTileIndexY();
              if (topIndex > ImageManip.getMaxTileIndexY())
                   topIndex = ImageManip.getMaxTileIndexY();
              int bottomIndex = displayImage.YToTileY(bounds.y + bounds.height - 1);
              if (bottomIndex < ImageManip.getMinTileIndexY())
                   bottomIndex = ImageManip.getMinTileIndexY();
              if (bottomIndex > ImageManip.getMaxTileIndexY())
                   bottomIndex = ImageManip.getMaxTileIndexY();
              for (tj = topIndex; tj <= bottomIndex; tj++) {
                   for (ti = leftIndex; ti <= rightIndex; ti++) {
                        Raster tile = displayImage.getTile(ti, tj);
                        DataBuffer dataBuffer = tile.getDataBuffer();
                        WritableRaster wr = Raster.createWritableRaster(sampleModel,
                                  dataBuffer, new Point(0, 0));
                        BufferedImage bi = new BufferedImage(colorModel, wr,
                                  colorModel.isAlphaPremultiplied(), null);
                        if (bi == null) continue;
                        int xInTile = displayImage.tileXToX(ti);
                        int yInTile = displayImage.tileYToY(tj);
                        atx = AffineTransform.getTranslateInstance(
                                  xInTile + panX, yInTile + panY);
                        g.drawRenderedImage(bi, atx);
              imageDrawn = true;
              if (cropOn) {
                   Rectangle cropRect = getDimensions();
                   if (cropRect == null) return;
                   g.setColor(Color.BLACK);
                   g.drawRect(cropRect.x, cropRect.y, cropRect.width, cropRect.height);
                   g.setComposite(makeComposite(0.5f));
                   g.fillRect(0, 0, cropRect.x, cropRect.y);
                   g.fillRect(cropRect.x, 0, viewerWidth - cropRect.x, cropRect.y);
                   g.fillRect(0, cropRect.y, cropRect.x, viewerHeight - cropRect.y);
                   g.fillRect(cropRect.x + cropRect.width, cropRect.y,
                             viewerWidth - cropRect.x, viewerHeight - cropRect.y);
                   g.fillRect(cropRect.x, cropRect.y + cropRect.height, cropRect.width,
                             viewerHeight - cropRect.y);
              g.dispose();
              if (((double)GarbageCollection.getFreeMem() /
                        (double)GarbageCollection.getRuntimeMem()) > 0.7d) {
                   System.out.println("Memory Usage: " +
                             (int)(((double)GarbageCollection.getFreeMem() /
                             (double)GarbageCollection.getRuntimeMem()) * 100) + "%");
                   System.out.println("Requesting garbage collection.");
                   GarbageCollection.runGc();
              setDoubleBuffered(true);
    Thank you in advance for support

    I moved the sizing out of the paint method and put in a component listener. I am still getting approx. the
    same performance. Possibly because I am trying to do too much. I have a mouse listener that allows
    the user to pan the image. I also have crop, rotate, shear, and flip methods. The main problem I think
    is when I change graphics to scale at (72d / 300d) which puts the display comparable to 300 dpi.
    My main reason for this was to allow zooming for the user. Am I overtasking the graphics object by
    doing this?

  • How to fill polygon without using paint component ?

    hello,
    i have a class called "public class Arrow2DRhombus implements Shape" with the following constructor:
    public Arrow2DRhombus(Point2D begin, Point2D end) {
            this.x1 = begin.getX();
            this.y1 = begin.getY();
            this.x2 = end.getX();
            this.y2 = end.getY();
            this.initArrow();the code for .initArrow():
    private void initArrow(){
            int length = (int) Math.sqrt(Math.pow(Math.abs(x1-x2),2) +
                           Math.pow(Math.abs(y1-y2),2));
            length=length-19;
    Polygon poly = new Polygon();
            poly.addPoint((int) x1,(int) y1);
            poly.addPoint((int) x1+ length,(int) y1);
              poly.addPoint((int) x1+ length+10,(int) y1+5);
              poly.addPoint((int) x1+ length+20,(int) y1);
              poly.addPoint((int) x1+ length+10,(int) y1-5);
              poly.addPoint((int) x1+ length,(int) y1);
            double rad = this.calcAngle((float) this.x1,(float) this.y1,(float) this.x2,(float) this.y2);
            AffineTransform tx = AffineTransform.getRotateInstance(rad,x1,y1);
            this.arrow = tx.createTransformedShape((Shape)poly);
        }is there a way i can fill the polygon "poly" with the code above .
    i know that i can fill a polygon using when using paintcomponent(Graphics g)
    but here how can i do it.
    10x

    I don't really understand the question. "Filling" a shape implies (to me) that you have a Graphics Object and are doing custom painting. So you would do this:
    a) in the paintComponent() method of a component
    b) in the paintIcon method of an Icon
    c) by painting onto a BufferedImage
    I'm sure there are other ways, but I don't think you just "fill" a Shape object.

Maybe you are looking for