Overriding paint

Is there any way to override paint only for one instance. For example, I have an image that I want to display, but after it is displayed, I want to be able to modify it. Problem is is that whenever a window is passed over the image, it updates and calls the paint method, which erases the modifications I made to the image. Is there anyway to only override the paint method only when I want to initially display the image?
Also how would I scale down an ImageIcon so that its smaller?
Thanks,
Rick

Don't modify the original image. Create a second image, paint your original image into the second image and then modify the second image. Each time paint is called, paint the second image to screen. The second one will always have your modifications, so it's safe to paint it multiple times. If you need to change the image agian, paint your original image over the first image again (to get an new clean copy) and modify it again. That's probably the most simplest thing you could do.
You could also convert an image to a byte array, modify the byte array and use an ImageProducer (please look up that class) to paint your byte array to the screen. If you marked it as animated, you can update the byte array as often as you want and the changes will be viewable on the next call of paint.

Similar Messages

  • Overriding paint method hides my status bar icons

    Hi All,
                I use setStatus on MainScreen to add some image ButtonFields to my status bar.
                It shows up fine.
                But when I override paint method , it hides all the icons I added using setStatus.
               Any clue on this ? Thnx in advance.
              Here is my paint method:
    protected void paint(Graphics graphics) {
    super.paint(graphics);
    Bitmap image = ImageUtility.loadBitMap("header2.jpg");
    graphics.drawBitmap(0, 0, 500, image.getHeight(), image, 0, 0);
    for(int i =0; i < 5; i++ ){
    fieldList.drawListRow(fieldList, graphics, i, 50 + (i*50), 20);
    protected void paint(Graphics graphics) {         
             Bitmap image = ImageUtility.loadBitMap("header2.jpg");
             graphics.drawBitmap(0, 0, 500, image.getHeight(), image, 0, 0); for(int i =0; i < 5; i++ ){           fieldList.drawListRow(fieldList, graphics, i, 50 + (i*50), 20);
    Aranya

    Try Calling super.paint at the start or at the end ... it should do the trick

  • Overriding paint for customizing JButton??? Shouldn't be paintComponent?

    Hi
    Shouldn't we always override paintComponent to customize the painting of a component?
    Why MetalScrollButton in javax.swing.plaf.Metal overrides paint instead of paintComponent?
    Should I override paint if I want to write my own LnF?
    Thanks.

    As is said in the online doc about paint(), "This method actually delegates the work of painting to three protected methods: paintComponent, paintBorder, and paintChildren. They're called in the order listed to ensure that children appear on top of component itself. Generally speaking, the component and its children should not paint in the insets area allocated to the border. Subclasses can just override this method, as always. A subclass that just wants to specialize the UI (look and feel) delegate's paint method should just override paintComponent."
    That means you can override paint(), , but in this case you'll have to draw the border yourself... and if you have a composite custom component (a panel containing labels, for example), you will have to call the label's paintComponent() methods by yourself...
    Hope this helped,
    Regards

  • Working in a jFrame - overriding paint

    Hey guys, currently I'm programming a game in Java.
    I use a JFrame with a CustomJPanel.
    In my CutomJPanel i override the Paint method.
    This is working well, i have something about 200FPS, but it's still simple.
    Today I read a Article about JavaFX and saw it has GPU Support.
    So if i use a CustomJFXPanel in my JFrame instead, are I just getting the advantage of GPU-Support by it?
    Or do i lose the benefits be overriding the Paint Method?

    This question cannot be answered in a simple yes/no way. It depends on the kind of graphics operation that you want to perform. In some areas JavaFX is slower than good old AWT. So it depends.

  • Overriding paint() method...

    i have a number of JComponents which I am displaying within a JPanel.
    Each JComponent holds a GIF image. Therefore, i overrode the paint(Graphics g) method of my JComponent Subclass.
    Now i need to connect each JComponent with lines. I had to do this within the JPanel, so i added the following to the paint method of the JComponent:
    public void paint(Graphics g) {
      //code here to display JComponent with image
      //then:
      JPanel panel = this.getParent();
      panel.getGraphics().drawLine(this.xCoordinate, this.yCoordinate, myJComponent2.getXCoordinate(), myJComponent2.getYCoordinate());
    }The JPanel displays correctly when i run it. But as soon as i resize the JFrame which holds the JPanel (for example, maximise the window):
    - The images of the JComponents within the JPanel remain visible
    - BUT the lines i drew disappear.
    Any ideas why the lines are disappeaing?
    How can it be solved?
    thanks

    further, I would strongly argue that trying to draw on other components from the paint method of another component is a bad bad bad idea.
    Calling setPreferredSize in the paint methods is also not something you should do either.
    The paint method is for painting in that component only. Setting size and location and doing other layout things in the paint methods is not the appropriate place for those things.
    If you want to draw on the panel, then draw on the panel in the panel, not in some child component. If you want to assign a default preferred size, do so in the constructor.

  • Stuck with the paint and repaint methods

    I am supposed to create a JApplet with a button and a background color. When the button is clicked the first time a word is supposed to appear (I got that part), when the button is clicked the second time the word is supposed to appear again in a different color and in a different location on the Applet (I got that part).
    The problem is that the first name is supposed to disappear when the second word appears. So I know I have to repaint the screen when the button is clicked the second time and draw the first string in the same color as the background color to make it invisible.
    My problem is I am not sure how I can code to apply different settings each time the button is clicked. Can anyone help? Please let me know if my explanation sucks. I will try to explain better. However here is the code I have so far. I added a counter for the button just for testing purposes.
    I just need some hints on what to do and if there is a easier way than using that if statement please let me know. I probably make it harder than it is.
    Thanks in advance and Merry Christmas.
    import javax.swing.*;
       import java.awt.*;
       import java.awt.event.*;
        public class DisplayMyName extends JApplet
        implements ActionListener
          String myName = "DOG";
          String myName1 = "DOG";
          JButton moveButton = new JButton("Move It");
          Font smallFont = new Font("Arial", Font.BOLD, 12);
          Font largeFont = new Font("Lucida Sans", Font.ITALIC, 20);
          int numClicks = 0;
          JLabel label = new JLabel("Number of button clicks:" + numClicks);
           public void init()
             Container con = getContentPane();
             con.setBackground(Color.RED);
             con.setLayout( new FlowLayout() );
             con.add(moveButton);
             con.add(label);
             moveButton.addActionListener(this);
           public void paint(Graphics g)
             numClicks++;
             label.setText("Number of button clicks: " + numClicks);
             if (numClicks == 2)
             { g.setFont(smallFont);
                g.setColor(Color.BLUE);
                g.drawString(myName, 50, 100);
                   else
             if (numClicks == 3)
             { g.setFont(largeFont);
                g.setColor(Color.YELLOW);
                g.drawString(myName, 100, 200);
           public void actionPerformed(ActionEvent move)
             repaint();
       }

    You're putting your program logic in the paint method, something you should not do. For instance, try resizing your applet and see what effect that has on number of button clicks displayed. This is all a side effect of the logic being in the paint method.
    1) Don't override paint, override paintComponent.
    2) Don't draw/paint directly in the JApplet. Do this in a JPanel or JComponent, and then add this to the JApplet. In fact I'd add the button, the and the label to the JPanel and add the label to the JApplet's contentPane (which usually uses BorderLayout, so it should fill the applet).
    3) Logic needs to be outside of paint/paintComponent. the only code in the paintComponent should be the drawing/painting itself. The if statements can remain within the paintComponent method though.
    4) When calling repaint() make sure you do so on the JPanel rather than the applet itself.
    For instance where should numClicks++ go? Where should the code to change the label go? in the paint/paintComponent method? or in the button's actionlistener? which makes more sense?
    Edited by: Encephalopathic on Dec 24, 2008 9:37 AM

  • Paint() method is in infinite loop when focus is set to JTextPane

    Hi All,
    I am creating one JScrollPane, JPanel and JTextPane. JPanel is added to JScrollPane and JTextPane is added to JPanel. If I override paint() method in JPanel, its in infinite loop. I don't know why it is behaving like this. Can anyone solve my problem. Since, I am doing so much drawings in paint() method, its getting to slow.
    Here is the code what I wrote.
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TestTextPane1 extends JFrame {
         JPanel panel;
         JScrollPane pane;
         public TestTextPane1() {
              panel = new JPanel(new BorderLayout()) {
                   public void paint(Graphics g) {
                        super.paint(g);
                        System.out.println("inside paint");
              JTextPane textPane = new JTextPane();
              panel.add(textPane);
              pane = new JScrollPane(panel);
              getContentPane().add(pane);
              setSize(300, 200);
              setVisible(true);
         public static void main(String [] args) {
              new TestTextPane1();
    Any help will be useful for me.
    Regards
    Kishore.

    What are you trying to do? You are adding the textPane to the panel and then overriding the paint() method. The textPane will never be painted if you do this.
    In Swing, you should override the paintComponent() method, not the paint() method. See the Swing tutorial on Painting:
    http://java.sun.com/docs/books/tutorial/uiswing/overview/draw.html
    Don't forget to read the "Working With Graphics" link at the bottom.

  • Calling a method within paint

    Greetings. I am currently writing my major project for computer science. I am just here to ask one question. I called a method from within paint which made my program spit the dummy. The method was supposed to draw a question on the screen but instead it just skipped through many questions as paint was called multiple times. Why would paint have been called multiple times?
    Thankyou

    Thanks for replying. Would you be able to explain
    that in simpler terms?Hardly possible. Paint is painting the component to the screen. Sometimes, what was painted has to be refreshed - if the window moved or was minimized, if something was dragged across it or some other reasons. Whenever that happens, pant() is called.
    By the way, if oyu use Swing, you should not override paint() but paintComponent() to draw something.
    I am studying java without
    swing. I solved the problem by adding a start button
    and putting the method call into action performed,
    but i'm just wondering why it would have mattered
    that it was in paint.Because as you pointed out, paint is called arbitrarily whenever it's needed. Furthermore, it's not the view's task to modify the data model (see MVC pattern) which it did in your case.

  • Re-painting in JApplet

    I'm trying to build a very simple picture browser from JApplet. There is just a picture, next, previous, last and first buttons.
    I have got the picture viewer working to a point. The program loads the default image to start. However if I click the first or last button the program will display the image but only after I click one of the scroll bars, the the correct image is loaded into the display.
    Here is a code sample to the first button ActionListener
    current = 0;
    thePic.setImage(new ImageIcon(theNames[0]).getImage());
    theTopTitle.setText("First Record (" + current + ")");     
    Does anyone know how to update the display when I click the button so the image is actually shown/updated? Do I need to use a Graphics object or paint method etc?
    thanks
    from Phil

    My experience of using graphics in Java is that it is always best to use a paint() method, as otherwise you seem to get all sorts of minor display artefacting. If you create a class that extends Canvas for your picture & then override paint() in this class, you can simply call repaint() when your button is pressed & the whole image should re-paint.
    Something like the following ( the update() method is only necessary if you wish to avoid display flicker ):
    (Initialise at start: )
    MapCanvas canvas = new MapCanvas( );
    (In your button method: )
    canvas.repaint( );
    class MapCanvas extends Canvas
    Image offScrImg;
    public void update( Graphics g )
    if( offScrImg == null ) { offScrImg = createImage( getSize( ).width, getSize( ).height ); }
    Graphics og = offScrImg.getGraphics( );
    paint( og );
    g.drawImage( offScrImg, 0, 0, this );
    og.dispose();
    public void paint( Graphics g )
    (Insert method to draw image here)
    }

  • How to drag a painted square from one panel to the other?

    Hi there,
    I'm a student IT, and for the course Programming, which uses Java, we have to write a game.
    I've got a JFrame with two JPanels. Both are overriding paint(Graphics g). One JPanel, let's say JP1, draws a board with 196 squares, the other, JP2, draws 21 pieces made up from the same size squares. What I want is, when I press on a piece in JP2, that I can drag that piece over to the board, JP1, and when I release the mousebutton, the piece is drawn in the board.
    How can I realize the dragging part? I've tried with an extra JPanel, with the size of the JFrame, but then either JP1, JP2, or both won't draw anymore.
    Can someone help me with this?
    Check here for a paint what I mean (don't mind my painting skills :p). At no.1, the mousebutton is pressed on the piece, at no. 2 the dragging starts, and at no. 3 the mousebutton is released again.
    Hope I've provided you enough information, I would post the source code here, but it's a lot in Dutch, and it may just confuse you ;)
    Greetings,
    Niek

    Ok, I'll study the link, and try to post my threads in the right section next time ;)
    Didn't even came up with me there might be a tutorial or something for this..
    Thank you! ;)
    Edited by: Niekfct on 17-mrt-2009 21:19

  • Painting JComponent on BufferedImage

    Hi!
    My problem concerns painting component derived from JComponent class onto a BufferedImage.To be more precise I've got a JPanel onto which I draw a BufferedImage and then I add my component to the panel. Everything is OK when the component is opaqued but when it is not it doesn't show at all (BufferedImage displays every time). I've been playing with types of BufferedImage (BufferedImage.TYPE_INT_ARGB etc.),double buffering of my component and the panel but with no result. I've also discovered that it's meaningless whether I paint component before drawing Image or after. Here's a piece of code that should help in better understanding what's on my mind:
    public class A extends JPanel {
        public void paint(Graphics g) {
            removeAll();
            g.drawImage(bi,0,0,null); //bi - BufferedImage
            TrojkatnyButton t = new TrojkatnyButton();
            t.setSize(20,20);
            t.setLocation(30,30);
            add(t);
            t.setVisible(true);
            t.repaint();
    public class TrojkatnyButton extends JComponent {
        public boolean isOpaque() {
            return false;
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.ORANGE);
            g.fillOval(0, 0, getWidth(), getHeight());
    }Thanks in advance for answers!

    strannik wrote:
    public class A extends JPanel {
    public void paint(Graphics g) {
    removeAll();
    g.drawImage(bi,0,0,null); //bi - BufferedImage
    TrojkatnyButton t = new TrojkatnyButton();
    t.setSize(20,20);
    t.setLocation(30,30);
    add(t);
    t.setVisible(true);
    t.repaint();
    In this code you are adding a component to another component from within a paint method, and this is something you should never do. Remember that the logic part of the program should not be placed into the paint portion of the program. I'd fix this first and then see if this fixes your problem. If not, I recommend that you post an SSCCE (Short, Self Contained, Correct (Compilable), Example). For more info on SSCCEs please look here:
    [http://homepage1.nifty.com/algafield/sscce.html|http://homepage1.nifty.com/algafield/sscce.html]
    Again, if the code is compilable and runnable more people will be able to help you.
    Best of luck.
    edit: also, since you are coding in Swing, you should not override paint at all 99% of the time. You are far better off overriding paintComponent of your JPanels and other JComponents. Also, don't forget to call super.paintComponent(g) as the first line of your paintComponent override.
    HTH.
    Edited by: Encephalopathic on Jan 3, 2009 6:52 AM

  • The bane of my existance = paint()  Please help!

    I am forever getting frustrated with trying to draw things in java and even when I went through my classes in college we hit the basics but no further with this. Basically I am attempting to set up a checker board and want to draw multiple canvases in different states obviously to render a working checker board with pieces, moves etc.
    Every book and online reference that I can find has all the work done in the paint method of the canvas that is being drawn on and not from the parent(if this is even possible...is it???).
    I would like to do something like this from the parent frame:
         private void initializeBlackLayers(int initState) {
              JPanel initPanel;
              // for each state the square can be in create a panel to display that state
              for (int numState = 1; numState < 11; numState++) {
                   // create a new panel and set the size of the panel for the square
                   initPanel = new JPanel();
                   initPanel.setSize(sqSize);
                   // need to figure out which state we are creating for the square and
                   // then create that specific state for the current panel
                   switch (numState) {
                        case 1:
                             initPanel.setBackground(Color.BLACK);  // the plain black sq
                             break;
                        case 2:
                             initPanel.setBackground(Color.BLUE);  // the plain black sq selected for move
                             break;
                        case 3:
                             //NEED TO DRAW A RED CIRCLE, BLACK BACKGROUND
                             break;
                        case 4:
                             initPanel.setBackground(Color.BLUE);
                             // NEED TO DRAW A RED CIRCLE
                             break;
                        // insert code adding new panel to this layered pane and end switch, function etcI figure I either need to set up a new class for each state of the square and draw individually(which I REALLY don't want to do, read: there has GOT to be a better way) or figure out how to set the global color and just draw a filled in oval with the current color in a generic ovalCanvas for each state in the code above in the parent class. If anyone could enlighten me, I would be forever grateful for getting me out of having an extremely frustrating time with this.

    camickr wrote:Well, first of all why would people who have never written a Swing application no anything about the paint() method.Secondly, you should not be overriding the paint() method. Custom painting in Swing is done by overriding the paintComponent() method. Overriding paint() is the way it is done when using AWT.>
    Regardless, it's still the same question, whether you are using paint or paintComponent, my question is the same: How can you get a parent class to draw multiple different canvases(if using AWT) or JPanels (if swing) without constructing the individual components individually. For example, I want to have a Frame (seeing as we are not in the swing forum) that has 2 canvases one with a red circle on a black background and one with a green triangle on a blue background. Is it possible to write this from the frame, initalize 2 canvases and again from the frame, tell the canvases what you want on them?
    Start by reading the Swing tutorial on [How to Use Layered Panes|http://java.sun.com/docs/books/tutorial/uiswing/TOC.html]. The tutorial also has a section on Custom Painting.
    I did, it doesn't explain what I want to do. In their example they are just coloring the background to show up so you can see the individual panes. This is where I get stuck and what I am asking about: Write the same code they use in that example but put something on those colored backgrounds as well. Can you do it from the JLayeredPane class?

  • Super.paint() before or after?

    Hi,
    I am overriding paint, like so
    public void paint(Graphics g) {
          super.paint(g);
         Graphics2D g2d = (Graphics2D) g;
         g2d.drawImage(image, x, y, null);
    }I have a panel, which has smaller "floating" panles on top of it, and i am painting an image in the center. if a panel is dragged over top of the image, i want the image to not be visible.....the image should only be visible when nothing is on top of it. i realize this has to do with the order in which i paint...
    so should i draw the image, and then do super.paint(g) ? or do i need to do super.paint(g2d)?
    can u post the correct way of doing it, using the code given above.
    thanks

    protected void paintComponent(Graphics g) {
              if (myimage != null) {
                   g.drawImage(myimage, 10, 10, null);
              setOpaque(false);
                    g.setColor(getBackground());
              g.fillRect(0, 0, getWidth(), getHeight());
              g.setColor(getForeground());
              super.paintComponent(g);
         }now i have a background color i want...but the image disappeared....in what order should i do these things?
    thanks

  • Help me make my painting go faster

    Hi!
    This is the paint code for my drawingprogram. It is a JPanel which contains everything that users has drawn AND is drawing. When I've drawn alot of objects then it starts getting slower and the painting can't follow up with my mouseDragged event. This is what I need fixed!
    First off I paint all the objects that are STORED in a model. Then I start painting the object(only 1 at the time) that the user is CURRENTLY drawing.
    All calls are made by invoking repaint().
    question: should I override paint() or paintComponent()? What will be the difference.
    I need some tips of how I can make this code quicker..I know for example I should use a different call to repaint by specifying only the area needed to paint out again. But do you have more tips?
    public void paintComponent(Graphics gx)
            super.paintComponent(gx);
            Graphics2D g = (Graphics2D) gx.create();  //make a copy of the graphics object
            LinkedList listOfDrawingObjects = controller.getListOfDrawingObjects();
            LinkedList currentDrawingObjects = new LinkedList(listOfDrawingObjects); //make a copy to avoid ConcurrentModificationException
            ListIterator it = currentDrawingObjects.listIterator();
    /* PAINT OUT ALL OBJECTS IN THE MODEL */
            while(it.hasNext())
                DrawingObject ob = (DrawingObject) it.next();
                if(ob instanceof MyLine)
                    MyLine myLine = (MyLine) ob;
                    g.setColor(myLine.getColor());
                    g.setStroke(stroke.getDrawingStrokeSize(myLine.getStrokeSize()));
                    g.drawLine((int)myLine.getX1(), (int)myLine.getY1(), (int)myLine.getX2(), (int)myLine.getY2());
                else if(ob instanceof MyRectangle)
                    MyRectangle myRectangle = (MyRectangle) ob;
                    g.setColor(myRectangle.getColor());
                    g.setStroke(stroke.getDrawingStrokeSize(myRectangle.getStrokeSize()));
                    if(myRectangle.getFilled())
                        g.fillRect((int)myRectangle.getX(), (int)myRectangle.getY(), (int)myRectangle.getWidth(), (int)myRectangle.getHeight());
                    else
                        g.drawRect((int)myRectangle.getX(), (int)myRectangle.getY(), (int)myRectangle.getWidth(), (int)myRectangle.getHeight());     
                else if(ob instanceof MyOval)
                    MyOval myOval = (MyOval) ob;
                    g.setColor(myOval.getColor());
                    g.setStroke(stroke.getDrawingStrokeSize(myOval.getStrokeSize()));
                    if(myOval.getFilled())
                        g.fillOval((int)myOval.getX(), (int)myOval.getY(), (int)myOval.getWidth(), (int)myOval.getHeight());
                    else
                        g.drawOval((int)myOval.getX(), (int)myOval.getY(), (int)myOval.getWidth(), (int)myOval.getHeight());
                else if(ob instanceof MyRoundRectangle)
                    MyRoundRectangle myRoundRectangle = (MyRoundRectangle) ob;
                    g.setColor(myRoundRectangle.getColor());
                     g.setStroke(stroke.getDrawingStrokeSize(myRoundRectangle.getStrokeSize()));
                    if(myRoundRectangle.getFilled())
                        g.fillRoundRect((int)myRoundRectangle.getX(), (int)myRoundRectangle.getY(), (int)myRoundRectangle.getWidth(), (int)myRoundRectangle.getHeight(), (int)myRoundRectangle.getArcWidth(), (int)myRoundRectangle.getArcHeight());
                    else
                        g.drawRoundRect((int)myRoundRectangle.getX(), (int)myRoundRectangle.getY(), (int)myRoundRectangle.getWidth(), (int)myRoundRectangle.getHeight(), (int)myRoundRectangle.getArcWidth(), (int)myRoundRectangle.getArcHeight());
                else if(ob instanceof MyFreeDraw)
                MyFreeDraw myFreeDraw = (MyFreeDraw) ob;
                    g.setColor(myFreeDraw.getColor());
                    g.setStroke(stroke.getDrawingStrokeSize(myFreeDraw.getStrokeSize()));
                    LinkedList tempPoints = myFreeDraw.getAllPoints();
                    LinkedList allPoints = new LinkedList(tempPoints); //avoids ConcurrentException
                    if(allPoints.size()>=2)
                        ListIterator it2 = allPoints.listIterator(0);
                        ListIterator it3 = allPoints.listIterator(1); //must be one step ahead since two points are needed to draw
                        while(it2.hasNext() && it3.hasNext())
                            Object ob2 = it2.next();
                            Object ob3 = it3.next();
                            if(ob2 instanceof Point)
                                Point p = (Point) ob2;
                                Point t = (Point) ob3;
                                g.drawLine(p.x, p.y, t.x, t.y);
                else if(ob instanceof MyPolygon)
                    MyPolygon myPolygon = (MyPolygon) ob;
                    g.setColor(myPolygon.getColor());
                    g.setStroke(stroke.getDrawingStrokeSize(myPolygon.getStrokeSize()));
                    if(myPolygon.getFilled())
                        g.fillPolygon(myPolygon);
                    else
                        g.drawPolygon(myPolygon);
                else if(ob instanceof MyText)
                    MyText myText = (MyText) ob;
                    g.setColor(myText.getColor());
                    g.setFont(myText.getFont());
                    g.drawString(myText.getText(), (int)myText.getStart().getX(), (int)myText.getStart().getY());
    /* STARTS PAINTING OUT ALL TEMPORARY OBJECTS*/
            if(rectangle!=null && rectangle.getStart()!=null && rectangle.getStop()!=null)
                g.setColor(rectangle.getColor());
                g.setStroke(stroke.getDrawingStrokeSize(rectangle.getStrokeSize()));
                if(rectangle.getFilled())
                    g.fillRect((int)rectangle.getX(), (int)rectangle.getY(), (int)rectangle.getWidth(), (int)rectangle.getHeight());
                else
                    g.drawRect((int)rectangle.getX(), (int)rectangle.getY(), (int)rectangle.getWidth(), (int)rectangle.getHeight());     
            else if(oval!=null && oval.getStart()!=null && oval.getStop()!=null)
                g.setColor(oval.getColor());
                g.setStroke(stroke.getDrawingStrokeSize(oval.getStrokeSize()));
                if(oval.getFilled())
                    g.fillOval((int)oval.getX(), (int)oval.getY(), (int)oval.getWidth(), (int)oval.getHeight());
                else
                    g.drawOval((int)oval.getX(), (int)oval.getY(), (int)oval.getWidth(), (int)oval.getHeight());
            else if(line!=null && line.getStart()!=null && line.getStop()!=null)
                g.setColor(line.getColor());
                g.setStroke(stroke.getDrawingStrokeSize(line.getStrokeSize()));
                g.drawLine((int)line.getX1(), (int)line.getY1(), (int)line.getX2(), (int)line.getY2());
            else if(roundRectangle!=null && roundRectangle.getStart()!=null && roundRectangle.getStop()!=null)
                g.setColor(roundRectangle.getColor());
                g.setStroke(stroke.getDrawingStrokeSize(roundRectangle.getStrokeSize()));
                if(roundRectangle.getFilled())
                    g.fillRoundRect((int)roundRectangle.getX(), (int)roundRectangle.getY(), (int)roundRectangle.getWidth(), (int)roundRectangle.getHeight(), (int)roundRectangle.getArcWidth(), (int)roundRectangle.getArcHeight());
                else
                    g.drawRoundRect((int)roundRectangle.getX(), (int)roundRectangle.getY(), (int)roundRectangle.getWidth(), (int)roundRectangle.getHeight(), (int)roundRectangle.getArcWidth(), (int)roundRectangle.getArcHeight());
            else if(freeDraw!=null && freeDraw.getSize()>=2)
                g.setColor(freeDraw.getColor());
                g.setStroke(stroke.getDrawingStrokeSize(freeDraw.getStrokeSize()));
                LinkedList tempPoints = freeDraw.getAllPoints();
                LinkedList allPoints = new LinkedList(tempPoints);
                if(allPoints.size()>=2)
                    ListIterator it2 = allPoints.listIterator(0);
                    ListIterator it3 = allPoints.listIterator(1); //must be one step ahead since two points are needed to draw
                    while(it2.hasNext() && it3.hasNext())
                        Object ob2 = it2.next();
                        Object ob3 = it3.next();
                        if(ob2 instanceof Point)
                            Point p = (Point) ob2;
                            Point t = (Point) ob3;
                            g.drawLine(p.x, p.y, t.x, t.y);
            else if(polygon!=null && polygon.getStart()!=null && polygon.getStop()!=null)
                g.setColor(polygon.getColor());
                g.setStroke(stroke.getDrawingStrokeSize(polygon.getStrokeSize()));
                LinkedList temp = polygon.getAllLines();
                LinkedList linesList = new LinkedList(temp);
                ListIterator it2 = linesList.listIterator();
                while(it2.hasNext())
                    MyLine tempLine = (MyLine) it2.next();
                    g.drawLine((int)tempLine.getX1(), (int)tempLine.getY1(), (int)tempLine.getX2(), (int)tempLine.getY2());
                MyLine end = (MyLine) linesList.getLast();
                if(end!=null)
                    g.drawLine((int)end.getX2(), (int)end.getY2(), (int)polygon.getStop().getX(), (int)polygon.getStop().getY());
            else if(text!=null && text.getStart()!=null)
                g.setColor(text.getColor());
                g.setFont(text.getFont());
                g.drawString(text.getText(), (int)text.getStart().getX(), (int)text.getStart().getY());
            g.dispose(); //release the copy of the graphics object
        }And here comes mouseDragged...(Not much of help maybe)
      public void mouseDragged(MouseEvent e)
            if(geometry==0 || geometry==3)
                freeDraw.mouseDragged(e);
                repaint();
            else if(geometry==1 || geometry==8)
                rectangle.mouseDragged(e);
                repaint();
             //   repaint((int)rectangle.getX(), (int)rectangle.getY(), (int)rectangle.getStop().getX(), (int)rectangle.getStop().getY());
            else if(geometry==2 || geometry==9)
                oval.mouseDragged(e);
                repaint();
            else if(geometry==4)
                line.mouseDragged(e);
                repaint();
            else if(geometry==6 || geometry==11)
                roundRectangle.mouseDragged(e);
                repaint();
        }

    thanks!
    advice nr.2 was good! good enough for 2 duke stars! :)
    Looks better now (any more ways)?:
        public void paintComponent(Graphics gx)
            super.paintComponent(gx);
            Graphics2D g = (Graphics2D) gx.create();  //make a copy of the graphics object
            LinkedList listOfDrawingObjects = controller.getListOfDrawingObjects();
            LinkedList currentDrawingObjects = new LinkedList(listOfDrawingObjects); //make a copy to avoid ConcurrentModificationException
            ListIterator it = currentDrawingObjects.listIterator();
            while(it.hasNext())
                DrawingObject ob = (DrawingObject) it.next();
                ob.paint(g);
            if(rectangle!=null && rectangle.getStart()!=null && rectangle.getStop()!=null)
                rectangle.paint(g);
            else if(oval!=null && oval.getStart()!=null && oval.getStop()!=null)
                oval.paint(g);
            else if(line!=null && line.getStart()!=null && line.getStop()!=null)
                line.paint(g);
            else if(roundRectangle!=null && roundRectangle.getStart()!=null && roundRectangle.getStop()!=null)
                roundRectangle.paint(g);
            else if(freeDraw!=null && freeDraw.getSize()>=2)
                freeDraw.paint(g);
            else if(polygon!=null && polygon.getStart()!=null && polygon.getStop()!=null)
                polygon.paint(g);
            else if(text!=null && text.getStart()!=null)
                text.paint(g);
            g.dispose(); //release the copy of the graphics object
         }Message was edited by:
    CbbLe
    Message was edited by:
    CbbLe

  • Painting a Component on a Canvas

    Hello communtiy :)
    I'm creating a program to view images on an old laptop remade as a digital photo frame running Ubuntu. Seeing as I'm dealing with Linux I want the program to use AWT rather than Swing. I use a java.awt.Canvas to display the image, which works fine, but now I want to add a description to each image. I thought I'd do this by adding the description text to a TextArea and then draw the TextArea somewhere on the Canvas, so as to get the line wrap for free. This is where I'm stuck.
    So, I want to paint a java.awt.TextArea on to a java.awt.Canvas but I can't get the TextArea to show. The dummy code below shows only the text drawn by g.drawString(String, int int), not the one from textArea.paint(Graphics). What am I doing wrong?
    Frame frame = new Frame();
    frame.add(new java.awt.Canvas(){
        public void paint(Graphics g){
             g.drawString("g.drawString", 20, 20);
              java.awt.TextArea textArea = new java.awt.TextArea("TextArea");
              textArea.paint(g);
    frame.pack();
    frame.setVisible(true);Kind Regards

    The old AWT classes work through peers, where there is a heavyweight OS component (like a frame) underlying the AWT component. The problem with this, as you are finding, is that it is extremely limiting. In almost all cases, you can't change how it looks (overridding paint does nothing) or override its default event handling. And calling paint on an AWT class with an Image graphics object paints nothing, so you either have to replicate how your AWT class paints it thru code somehow, or not use AWT.

Maybe you are looking for

  • Functional and performance testing on Forms 6.0 and higher

    I want to make functional tests on GUI Oracle Forms ver. 6.0 and higher. I'm thinking in a IBM tool called Rational Robot but it's seems not very good. HP also has this type of tool with forms support but I haven't tested it, yet. Does Oracle have so

  • IDVD is not starting - only bounces once

    I have reinstalled iLife 06 and all other programs work except that iDVD only bounces once when clicked. I need to burn a DVD from iMovie asap and it won't load. I do have enough HD memory when I reinstalled. Help Please.

  • How do i get music into my libary that is only on my ipod

    I have music on my ipod that i have lost on CD and all in other forms. How do i get this music back out of my Ipod and into my itunes. I cant seam to open it just as a drive through my computer to do it that way. Any ideas i just dont want to lose it

  • Goods receipt slip and creates multiple objkey in nast for edi

    Dear SAP Guru, I have a po that has 2 lines and I do the goods receipt using the MIGO of all 2 line items.  I also have all the setup so that I create an idoc when goods receipt  is performed.  The problem is multiple idocs are created. So, for this

  • Pdf form text field

    Hello there, if text  contain "(" or ")" brakets are not displaying in PDF textfiled,if i convert "(" to "[" then the text is displaying in the pdf textfield.how do I allow "(" inside text.and i'm creating pdf programatically in java. thanks in advan