Calling paint(g) on components

When can you call paint(g) on a component? Does it have to be on screen for it to have any effect? ie if I call paint(g) on a component when it is not visible will it have any effect on the g I pass to it? (g is a Graphics instance.)

Hi,
that actually depends on the component. JComponent for example first checks whether its size has width and height greater that zero. Unless it doesn't paint. And this may happen if you don't set the size manually if its not on screen.
Other components may have some check if they're visible or something.
However this depends on the component in question.
Michael

Similar Messages

  • Correct clipping when calling "paint()" from thread

    How do I achieve correct clipping around JMenus or JTooltips when calling paint() for a Component from a background thread?
    The whole story:
    Trying to implement some blinking GUI symbols (visualizing alerts), I implemented a subclass of JPanel which is linked to a Swing timer and thus receives periodic calls to its "actionPerformed()" methods.
    In the "actionPerformed()" method, the symbol's state is toggled and and repainting the object should be triggered.
    Unfortunately, "repaint()" has huge overhead (part of the background would need to be repainted, too) and I decided to call "paint( getGraphics() )" instead of it.
    This works fine as long as there is nothing (like a JMenu, a JComboBox or a JTooltip) hiding the symbol (partially or completely). In such case the call to paint() simply "overpaints" the object.
    I suppose setting a clipping region would help, but where from can I get it?
    Any help welcome! (I alread spent hours in search of a solution, but I still have no idea...)

    For all those interested in the topic:
    It seems as if there is no reliable way to do proper clipping when calling
    "paint()".
    The problem was that when my sub-component called "repaint()" for itself, the underlying component's "paintComponent()" method was called as well. Painting of that component was complex and avoiding complexity by restricting
    on the clipping region is not easily possible.
    I have several sub-components to be repainted regularly, resulting in lots of calls to my parent component's "paintComponent()" method. This makes the
    repainting of the sub-components awfully slow; the user can see each one begin painted!
    Finally I decided I had to speed up the update of the parent component. I found two possible solutions:
    a) Store the background of each of the sub-components in a BufferedImage:
    When "paintComponent()" is called: test, if the clipping rectangle solely
    contains the region of a sub-component. If this is true check if there
    is a "cached" BufferedImage for this region. If not, create one, filling
    it with the "real" "paintComponent()" method using the Graphic object of
    the BufferedImage. Once we have such a cached image, simply copy it the
    the screen (i.e. the Graphics object passed as method parameter).
    b) To avoid the handling of several of such "cached" image tiles, simply
    store the whole parent component's visible part ("computeVisibleRect()")
    in a BufferedImage. Take care to re-allocate/re-paint this image each
    time the visible part changes. (I need to restrict the image buffer to
    the visible part since I use a zooming feature: Storing the whole image
    would easily eat up all RAM!) In the "paintComponent()", simple check
    if the currently buffered image is still valid - repaint if not -
    and copy the requested part of it (clip rect) to the screen. That's it!
    The whole procedure works fine.
    Best regards,
    Armin

  • How to call paint() method during creating object

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    class SomeShape extends JPanel {
         protected static float width;
         protected BasicStroke line = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND);
    class Oval extends SomeShape {
         Oval(float width) {
              this.width = width;
              line = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND);
              repaint();
         public void paint(Graphics g) {
              Graphics2D pen = (Graphics2D)g;
              int i = 10;
              super.paint(g);
                   g.setColor(Color.blue);
                   g.drawOval(90, 0+i, 90, 90);
                   System.out.println("paint()");
    public class FinalVersionFactory {
        JFrame f = new JFrame();
        Container cp = f.getContentPane();
        float width = 0;
        SomeShape getShape() {
             return new Oval(width++); //I want to paint this oval when I call getShape() method
         public FinalVersionFactory() {
              f.setSize(400, 400);
    //          cp.add(new Oval()); without adding
              cp.addMouseListener(new MouseAdapter() {
                   public void mouseReleased(MouseEvent e) {
                        getShape();
              f.setVisible(true);
         public static void main(String[] args) { new FinalVersionFactory(); }
    }I need help. When I cliked on the JFrame nothing happened. I want to call paint() method and paint Oval when I create new Oval() object in getShape(). Can you correct my mistakes? I tried everything...Thank you.

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    class SomeShape extends JPanel {
         protected static float width;
         protected static BasicStroke line = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND);
    class Oval extends SomeShape {
         static int x, y;
         Oval(float width, int x, int y) {
              this.width = width;
              this.x = x;
              this.y = y;
              line = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND);
         public void paint(Graphics g) {
              Graphics2D pen = (Graphics2D)g;
                   g.setColor(Color.blue);
                   pen.setStroke(line);
                   g.drawOval(x, y, 90, 90);
                   System.out.println("Oval.paint()"+"x="+x+"y="+y);
    class Rect extends SomeShape {
         static int x, y;
         Rect(float width, int x, int y) {
              this.width = width;
              this.x = x;
              this.y = y;
              line = new BasicStroke(width, BasicStroke.CAP_ROUND, BasicStroke.CAP_ROUND);
         public void paint(Graphics g) {
              Graphics2D pen = (Graphics2D)g;
                   g.setColor(new Color(250, 20, 200, 255));      
                   pen.setStroke(line);
                   g.drawRect(x, y, 80, 80);
                   System.out.println("Rect.paint()"+"x="+x+"y="+y);
    public class FinalVersionFactory extends JFrame {
        Container cp = getContentPane();
        float width = 0;
        int x = 0;
        int y = 0;
            boolean rect = false;
        SomeShape getShape() {
             SomeShape s;
              if(rect) {
                   s = new Rect(width, x, y);
                   System.out.println("boolean="+rect);
              } else {
                   s = new Oval(width++, x, y);
                   System.out.println("boolean="+rect);
              System.out.println("!!!"+s); //print Oval or Rect OK
              return s; //return Oval or Rect OK
         public FinalVersionFactory() {
              setSize(400, 400);
              SomeShape shape = getShape();
              cp.add(shape); //First object which is add to Container(Oval or Rect), returned by getShape() method
              //will be paint all the time. Why? Whats wrong?
              cp.addMouseListener(new MouseAdapter() {
                   public void mouseReleased(MouseEvent e) {
                        x = e.getX();
                        y = e.getY();
                        rect = !rect;
                        getShape();
                        cp.repaint(); //getShape() return Oval or Rect object
                                      //but repaint() woks only for object which was added(line 67) as first
              setVisible(true);
         public static void main(String[] args) { new FinalVersionFactory(); }
    }I almost finish my program but I have last problem. I explained it in comment. Please look at it and correct my mistakes. I will be very greatful!!!
    PS: Do you thing that this program is good example of adoption Factory Pattern?

  • Error calling up user interface components

    Dear experts,
    I have this error that I'm having problems in solving.
    When i am accessing the /n/sapapo/scc07 transaction (Supply Chain Engineer), the following error pumps up "Error calling up user interface components".
    I have tried to solve it in various ways, reinstall the SAP GUI in order to have the last version, etc. Please advice regarding this. Keep in mind that I am a Functional Consultant so I will do my best to understand any technical solutions proposed by you.
    Thank you,

    Hi Alex.
    You can try reinstalling your SAP GUI Version (assuming you are using 6.40) or better to move to higher GUI version (7.10).
    While installing the SAP GUI select the SCM Add-On components also
    Refer to Note 396020 it may help you.
    Rgds
    Raja kiran R

  • Call paint or validate

    What should I call paint or validate once a person
    minimizes or maximizes a frame or a dialog box.
    I have little confusion on this.
    rajesh

    paint is to render, validate is for layout stuff. Sometimes the UI becomes dirty even if the layout did not change. If your UI seems to be painted but display a bad layout, call validate. Note that you also might have to call both, empiric testing will prevail in this domain since Containers are sometimes a bit strange on their behaviours.
    Also, do not use paint but rather repaint(), it would be better. Also note that there's an invalidate method, depending on what you are doing, invalidate might be your answer.
    conclusion:
    use repaint(), invalidate(), validate(). the use of 3, 2 or 1 of those methods will be answered by empiric tests.

  • How to call paint ( )?

    Hi,
    I am trying to display a tring but I dont know how to call paint ( ). I used show but its giving me mesg that show is deprecated.
    Can somebody help me in this regard?
    import java.awt.*;
    class DisplayText extends Frame{
    DisplayText (String s) {
      super(s);
    public void paint (Graphics g) {
      g.drawString("Hello World", 10,10);
    public static void main (String args[ ]) {
       DisplayText screen = new DisplayText("Example 1");
                screen.setSize(500,100);
                screen.setVisible(true);
    }Zulfi.

    I don't really recommend using the paint() methodfor
    drawing Strings on the Frame. Paint should be used
    just for Graphics
    So how do you make a component show a text? Use a Label :-)If your custom component needs to paint graphics and text then there is nothing wrong with drawing a String in the paintComponent() method. (In Swing you should override paintComponent(), not paint(). It doesn't make sense to create a JLabel with all the extra overhead that requires.
    The problem with this posting is overriding the paint(..) method of the entire frame is not a good idea.

  • SwingUtilities.paintComponent - problems with painting childs of components

    Maybe this question was mention already but i cant find any answer...
    Thats what could be found in API about method paintComponent in SwingUtilities:
    Paints a component c on an arbitrary graphics g in the specified rectangle, specifying the rectangle's upper left corner and size. The component is reparented to a private container (whose parent becomes p) which prevents c.validate() and c.repaint() calls from propagating up the tree. The intermediate container has no other effect.
    The component should either descend from JComponent or be another kind of lightweight component. A lightweight component is one whose "lightweight" property (returned by the Component isLightweight method) is true. If the Component is not lightweight, bad things map happen: crashes, exceptions, painting problems...
    So this method perfectly works for simple Java components like JButton / JCheckbox e.t.c.
    But there are problems with painting JScrollBar / JScrollPane (only background of this component appears).
    I tried to understand why this happens and i think the answer is that it has child components that does not draw at all using SwingUtilities.paintComponent()
    (JScrollBar got at least 2 childs - 2 buttons on its both sides for scroll)
    Only parent component appears on graphics - and thats why i see only gray background of scrollbar
    So is there any way to draw something like panel with a few components on it if i have some Graphics2D to paint on and component itself?
    Simple example with JScrollPane:
    JScrollPane pane = new JScrollPane(new JLabel("TEST"));
    pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    SwingUtilities.paintComponent(g2d, pane, new JPanel(), 0, 0, 200, 200);Edited by: mgarin on Aug 11, 2008 7:48 AM

    Well the thing you advised could work... but this wont be effective, because i call repaint of the area, where Components painted, very often..
    So it will lag like hell when i make 10-20 repaints in a second... (for e.g. when i drag anything i need mass repainting of the area)
    Isnt there any more optimal way to do painting of the sub-components?
    As i understand when any component (even complicated with childs) paints in swing - its paint method calls its child components paint() to show them... can this thing be reproduced to paint such components as JScrollPane?

  • Direct service calls in java-driven components

    Hello all,
    I am new to WebCenter Content customization and have run into a problem I have trouble with solving correctly.
    Context
    I try to concentrate as much logic in ordinary java (and perhaps an ini or yaml file) and avoid the more unknown .hda and the customized .htm files.
    I experimented with creating a service class, service handler and filter. No biggies there apart from the difficult and slow debugging.
    The long list of UCM/URM services and complexity of these services themselves made me inclined to write a different API to make UCM/URM programming fun; e.g. an adapter to simplify and abstract away from the standard service API. Think in terms of "checkin(...args...)" so I can rely on the IDE hints to develop faster.
    Infrastructure should be either Linux or Windows Server.
    Problem
    I have not been able to call UCM/URM services directly from java in UCM/URM components yet.
    Question
    What is the best way to go about this?
    Perhaps I'm reinventing the wheel, if so please enlighten me. To me it feels what I'm trying to do should be a basic feature but for now I've missed it completely. If it's not as simple as that I'm wondering why.
    I did find documentation on IdcCommand which relies heavily on .hda, a lengthy installation and even duplicating intradoc.cfg which is bound to give someone a headache at some point.
    I also read some documentation on UCPM API for JavaEE, but I'm not using JavaEE.
    If anyone knows what I've been missing out of, your help is much appreciated!
    Edited by: Niels Krijger on Sep 18, 2012 4:58 AM
    Edited by: Niels Krijger on Sep 18, 2012 5:03 AM

    Thank you! RIDC appears to be exactly what I was looking for.
    I also discovered I've been looking at the wrong documentation. I was looking in:
    Oracle® Fusion Middleware Developer's Guide for Content Server
    http://docs.oracle.com/cd/E14571_01/doc.1111/e10807/toc.htm
    instead of
    Oracle® WebCenter Content Developer's Guide for Content Server
    http://docs.oracle.com/cd/E23943_01/doc.1111/e10807/toc.htm
    While the first mentions RIDC very briefly (I didn't think much of it then) the latter is much more helpful.
    Thank you very much.

  • Use a different paint for different components

    Dear all,
    I would really appreciate it if you could help me with an AWT problem that I am facing. I want to develop an application in which several images will be printed into the same frame (i.e. a background image, several images containing objects etc.). But I want the images to be "updated" according to the user's prefereneces (e.g. when he/she presses button A, only the image containing the object A should be redrawn, whilst if he/she presses the button B, only the image containing object B should be repainted). Until this moment, I have implemented a class containing a paint method where I include different if cases in order to repaint the corresponding objects on the user's input. I was wondering if there is any other easier way to achieve my ends (e.g. use different paint functions in the same class, making each paint responsible for the drawing of a certain image, and then call the corresponding paint on the user's input). I would really appreciate if you could help me by giving me some advice and/or some sample source code.
    Thank you in advance,
    Sincerely yours,
    Enginejim

    Hi NK,
    if you use Transparent Data Encryption you can choose between column encryption and tablespace encryption (or a mix)
    and the master key for both can only be stored in the same wallet (in 11gR2 we have a unified master key for both).
    Also an important concept of TDE is that it is tranparent: application users do not need to know any encryption key (passphrase),
    when you are asking that
    need to encrypt certain columns and these columns should be accessible to certain users who know a "key phrase" - and not othersthen you are making a common conceptual mistake, which is to confuse encryption with access control, there's actually a good
    statement about this in the security guide here: Principle 1: Encryption Does Not Solve Access Control Problems
    http://docs.oracle.com/cd/E11882_01/network.112/e16543/data_encryption.htm#i1006159
    So if some users should not have access to certain data, please solve this with access controls in combination with VPD,
    trying to solve it with encryption is simply ill-advised,
    greetings,
    Harm ten Napel
    Edited by: hnapel on Jan 24, 2013 8:14 AM

  • Calling variables in different components

    hi there,
    i apologise if this is a very silly question, but i am fairly
    new to flex and busy doing my first project using it and struggling
    to find the information i need.
    i have created an application that uses components for a
    multitude of different display areas. one of my areas is a
    component that calls 2 other components to create an input area and
    a listing area on the same page.
    i now need the listing area to populate the fields of the
    input area with data from the listing on selection of certain items
    in my listing. however, i cannot simply call them by their id as i
    get the error "Access of undefined property". how do i access the
    input area component's scope from within the listing component?
    thanks a mil',
    nikki

    hi peter,
    thanks for the quick reply. this didn't initially work as the
    2 components are sitting on the same "level"... i.e. nested inside
    another component.
    then i found a reference to a "parentDocument" scope and gave
    this a bash... so, parentDocument.listing.input and it worked. i
    hadn't realised the different scopes such as parentDocument,
    Application and parentApplication existed, or at least how to
    reference them.
    happy days! :)
    thanks again,
    nikki

  • Painting behind transparent components

    Hi All,
    My problem is quite simple. I want to paint on a panel to which I then add a JEditorPane inside a JScrollPane both of which are transparent (using setOpaque(false)). I'm expecting whatever I paint to appear behind any text rendered by the JEditorPane as the component on which it is begin painted is behind the JEditorPane.
    What actually happens is that everything drawn on the panel appears above everything else. The following is a self contained example that should display the Google logo BEHIND the GNU GPL but instead paints it on top. Any ideas what the problem is?
    import javax.swing.*;
    import java.net.URL;
    import java.awt.*;
    public class Test extends JFrame
         public static void main(String args[]) throws Exception
              Test t = new Test();
         public Test() throws Exception
              setSize(500,500);
              setTitle("Test");
              JPanel backPanel = new JPanel(new BorderLayout())
                   ImageIcon background = new ImageIcon(new URL("http://www.google.com/images/logo.gif"));
                   public void paint(Graphics g)
                        super.paint(g);
                        g.drawImage(background.getImage(),50,150,this);
              backPanel.setOpaque(true);
              backPanel.setBackground(Color.white);
              JEditorPane textPane = new JEditorPane(new URL("http://www.gnu.org/licenses/gpl.txt"));
              textPane.setEditable(false);
              textPane.setOpaque(false);
              JScrollPane scroller = new JScrollPane(textPane);
              scroller.setOpaque(false);
              scroller.getViewport().setOpaque(false);
              backPanel.add(scroller,BorderLayout.CENTER);
              getContentPane().add(backPanel,BorderLayout.CENTER);
              setVisible(true);
    }

    You can paint directly on the text component:
    import java.awt.*;
    import javax.swing.*;
    public class TextPaneBackground extends JFrame
         ImageIcon icon;
         public TextPaneBackground()
              icon = new ImageIcon("mong.jpg");
              JTextPane textPane = new JTextPane()
                   public void paintComponent(Graphics g)
                        g.drawImage(icon.getImage(), 0, 0, null);
                        super.paintComponent(g);
              textPane.setOpaque( false );
              textPane.setText( "This is some text" );
              JScrollPane scrollPane = new JScrollPane( textPane );
              getContentPane().add( scrollPane );
         public static void main(String[] args)
              TextPaneBackground frame = new TextPaneBackground();
              frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
              frame.setSize(200, 200);
              frame.setVisible(true);
    }Not positive, but I believe this approach would be more efficient. When Swing trys to paint a non-opaque component it needs to search the components ancestors until it finds an opaque component to paint the background. So I think in your case you have multiple ancestor, JViewport, JScrollPane, JPanel.

  • How to call view from Diffrent Components

    Hi Experts,
    In my application 5 component(comp1,comp2,comp3,comp4,comp5) under one DC.
    In Comp1-> View1 have a drop down to select business operations, View Container with empty view while starting.
    Ex: one,two, three, four list of values in drop down.
    If i select Two from drop down, it should  display view from comp2->View1 in ViewContainer.
    Like this it should display view from diffrent component in comp1->View1 (Inter Componet Communication).
    This application developing in CE 7.1 & NWDS 7.1 not in 2004s..
    Please any one help me to do this requirement .....
    Thanks in advance..
    Regards, 
    Satya.

    Hi,
    The simplest solution in this case is to create another DC component.
    This will have a single window with (a) ViewSet Container(s). Then you will have to create a window per view in your first DC Component.
    After this - you will need to add the first DC component to the second DC component (under used DC Components). This is done by first creating public parts of the first DC and adding to the second, following which you need to right click "Used DC components" in the second DC and select the first. You may need to do this multiple times for different windows.
    After this - in your second DC you will need to embed the Component Interface views of each of the windows to the View Set Containers (as created previously).
    Next you will need to write some code in the onSelect Action for the DropDown that will manage the visibility of the different views.
    Let me know in case there are any further issues.
    Thanks.
    HTH.
    p256960.

  • URGENT!! How to avoid paint all the components?

    In the following code sample, when you move the mouse, the top-level JPanel (with the name p) repaints itself with a red circle. The strange thing is that also the 4 JButtons repaint themselves!! (this is shown by the System.out). If the container of the 4 JButtons and the "p" had too many components, then the repaint of "p" gets very slow!! If i delete the line "super.paintComponent(g)" in the class "Myb", then the JButtons do not show correctly.
    How can i avoid that the 4 JButtons do not repaint themselves all the time?
    import javax.swing.*;
    import java.awt.*;
    public class Fr extends javax.swing.JFrame {
        private javax.swing.JPanel p;
        private Ap ap1;
        private Myb b4;
        private Myb b3;
        private Myb b2;
        private Myb b1;
        public Fr() {
            p = new javax.swing.JPanel();
            ap1 = new Ap();
            b1 = new Myb();
            b2 = new Myb();
            b3 = new Myb();
            b4 = new Myb();
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    System.exit(0);
            p.setLayout(null);
            p.setBackground(new java.awt.Color(153, 153, 0));
            p.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                public void mouseMoved(java.awt.event.MouseEvent evt) {
                    ap1.repaintWith(SwingUtilities.convertPoint(p,evt.getPoint(),ap1));
            ap1.setFont(new java.awt.Font("Dialog", 0, 11));
            ap1.setOpaque(false);
            p.add(ap1);
            ap1.setBounds(-10, -40, 500, 500);
            b1.setText("b1");
            p.add(b1);
            b1.setBounds(0, 0, 150, 150);
            b2.setText("b2");
            p.add(b2);
            b2.setBounds(0, 150, 150, 150);
            b3.setText("b3");
            p.add(b3);
            b3.setBounds(150, 0, 150, 150);
            b4.setText("b4");
            p.add(b4);
            b4.setBounds(150, 150, 150, 150);
            getContentPane().add(p, java.awt.BorderLayout.CENTER);
            pack();
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setSize(new java.awt.Dimension(500, 500));
            setLocation((screenSize.width-500)/2,(screenSize.height-500)/2);
        public static void main(String args[]) {
            new Fr().show();
    class Ap extends JPanel
        private Point p;
        public Ap(){
            super();
            p = new Point(10,10);
        public void repaintWith(Point p){
            this.p = p;
            repaint();
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillOval(p.x-5,p.y-5,10,10);
    class Myb extends JButton
        int x = 0;
        public Myb() {
            super();
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            System.out.println(getText()+"-->"+(x++));
    }

    I did that, but still nothing!
    I inserted these lines!
            JPanel subp = new javax.swing.JPanel();
            subp.setBorder(new javax.swing.border.LineBorder(Color.blue, 2));
            subp.setOpaque(false);
            p.add(subp);
            subp.setBounds(200, 0, 200, 200);
            ap1.setOpaque(false);
            subp.add(ap1);
            ap1.setBounds(200, 0, 200, 200);

  • Problem about calling paint method

    I have created a class to ask user input 3 floating-point numbers, using JApplet. the code list below
    import java.awt.Graphics;
    import javax.swing.*;
    public class Numbers
    double average,sum,product;
    String result;
    public void init()
    String firstNumber,secondNumber,thirdNumber;
    double num1,num2,num3;
    firstNumber=JOptionPane.showInputDialog("enter first number");
    secondNumber=JOptionPane.showInputDialog("Enter second number");
    thirdNumber=JOptionPane.showInputDialog("Enter thrid number");
    num1=Double.parseDouble(firstNumber);
    num2=Double.parseDouble(secondNumber);
    num3=Double.parseDouble(thirdNumber);
    sum=num1+num2+num3;
    product=num1*num2*num3;
    average=(num1+num2+num3)/3;
    result="";
    if(num1<num2 && num2<num3)
    result=result+num3+" is the largest number";
    if(num2>num1 && num2>num3)
    result=result+num2 +" is the largest number";
    if(num1>num2 && num2>num3)
    result=result+ num1+" is the largest number";
    public void paint(Graphics g)
    super.paint(g);
    g.drawString("sum is"+sum,25,25);
    g.drawString("product is"+ product,25,40);
    g.drawString("average is"+ average,25,60);
    g.drawString(" the largest number is"+result,25,80);
    however, after I compiled, it gave me the error message as:
    java:40: cannot resolve symbol
    symbol : method paint (java.awt.Graphics)
    location: class java.lang.Object
    super.paint(g);
    ^(pointer should point to the dot)
    don't know why,
    Message was edited by:
    ritchie_lin

    your class doesn't extend japplet or any other object for that matter. It does extend Object as all classes do, and Object has no paint method.
    Consider changing:
      public class Numbersto
      public class Numbers extends JAppletAlso, please use code tags next time you're posting code.
    Addendum: If this is not the JApplet portion of your code, it still has to subclass a Swing component that can accept painting such as JPanel. Also, with Swing you use paintComponent rather than paint.
    Message was edited by:
    petes1234

  • Does repaint() not call paint()?

    I try to create a application where pictures are switching randomly one after another. User can specific how long this randomly displaying image application run. Then when the user click "start", the image will randomly change until it is timeout. As far as I know, I have this part finish.
    However, I want to add another part: when user click start a small clock display on the panel the amount of second counting down. However, when I set the timer to repaint() the application, it does not get in the paint() method. Below are my code, please help. Since my code are a bit long, I will post my entire code on a separate thread, hope that the admin and mod dont mind.
    This is where I implement paint(), and I click on the button, I repaint the panel
    public void paint(Graphics g)
            System.out.println("Inside Graphics");
            //Create an instance of Graphics2D
            Graphics2D g2D = (Graphics2D)g;
            g2D.setPaint(Color.RED);
            g2D.setStroke(stroke);
            DigitalNumber number = new DigitalNumber(numberX,numberY,numberSize,numberGap,Color.red, Color.white);
            //Get the time that the user input to the field
            String time = tf.getText();
            float locX = numberX;
            float locY = numberY;
            //Start drawing the number onto the panel
            for(int i=0; i<time.length(); i++){
                 number.drawNumber(Integer.parseInt(Character.toString(time.charAt(i))), g2D);
                 locX += numberSize + numberGap;
                 number.setLocation(locX, locY);
    private void createBtnPanel() {
          JButton startBtn = new JButton("Start");
          JButton stopBtn = new JButton("Stop");
          startBtn.addActionListener(new StartBtnListener() );
          stopBtn.addActionListener(new StopBtnListener());
          btnPanel.add(startBtn, BUTTON_PANEL);
          btnPanel.add(stopBtn, BUTTON_PANEL);
    private class StartBtnListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             //get the time from the user input
             String input = tf.getText();
             numberOfMilliSeconds = Integer.parseInt(input)*1000;
             //get the current time
             startTime = System.currentTimeMillis();
             java.util.Timer clockTimer = new java.util.Timer();
             java.util.TimerTask task = new java.util.TimerTask()
                 public void run()
                      mainPanel.repaint();
             clockTimer.schedule(task, 0L, 1000L);
             rotatePictureTimer.start();
       }

    I am sorry. However, even though I try to make my code self contained, it still too long to post here. It is the DigitalNumber.java that contain all the strokes for draw the number is all too long. But I can tell you that DigitalNumber.java is worked because I wrote a digital clock before, and it worked. But below is the link to my DigitalNumber.java if u guy want to look at it
    http://www.vanloi-ii.com/code/code.rar
    Here is my code for DisplayImage.java in self-contain format. Thank you
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.*;
    public class DisplayImage{
       private static final Dimension MAIN_SIZE = new Dimension(250,250);
       private static final String BUTTON_PANEL = "Button Panel";
       private JPanel mainPanel = new JPanel();
       private JPanel btnPanel = new JPanel();
       private JPanel tfPanel = new JPanel();
       private BorderLayout borderlayout = new BorderLayout();
       private JTextField tf;
       BasicStroke stroke = new BasicStroke(5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
       private float numberX = 100f;
       private float numberY = 100f;
       private float numberSize = 10f;
       private float numberGap = 2f;
       public DisplayImage() {
          mainPanel.setLayout(borderlayout);
          mainPanel.setPreferredSize(MAIN_SIZE);
          createTFPanel();
          createBtnPanel();
          mainPanel.add(tfPanel, BorderLayout.NORTH);
          mainPanel.add(btnPanel, BorderLayout.SOUTH);
       public void paint(Graphics g)
            System.out.println("Inside Graphics");
            //Create an instance of Graphics2D
            Graphics2D g2D = (Graphics2D)g;
            g2D.setPaint(Color.RED);
            g2D.setStroke(stroke);
            DigitalNumber number = new DigitalNumber(numberX,numberY,numberSize,numberGap,Color.red, Color.white);
            //Get the time that the user input to the field
            String time = tf.getText();
            float locX = numberX;
            float locY = numberY;
            //Start drawing the number onto the panel
            for(int i=0; i<time.length(); i++){
                 number.drawNumber(Integer.parseInt(Character.toString(time.charAt(i))), g2D);
                 locX += numberSize + numberGap;
                 number.setLocation(locX, locY);
       private void createBtnPanel() {
          JButton startBtn = new JButton("Start");     
          startBtn.addActionListener(new StartBtnListener() );    
          btnPanel.add(startBtn, BUTTON_PANEL);
       private void createTFPanel() {
          JLabel l = new JLabel("Enter Number of Seconds: ");
          tf = new JTextField(3);
          tfPanel.add(l);
          tfPanel.add(tf);
       private class StartBtnListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
             //get the time from the user input
             java.util.Timer clockTimer = new java.util.Timer();
             java.util.TimerTask task = new java.util.TimerTask()
                 public void run()
                      System.out.println("TimerTask");
                      mainPanel.repaint();
             clockTimer.schedule(task, 0L, 1000L);     
       private static void createAndShowUI() {
           DisplayImage displayImage = new DisplayImage();
          JFrame frame = new JFrame("Display Image");
          frame.getContentPane().add(displayImage.mainPanel);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setBackground(Color.white);
          frame.pack();
          frame.setLocationRelativeTo(null);  //center the windows
          frame.setVisible(true);
       public static void main (String args[]) {
          java.awt.EventQueue.invokeLater(new Runnable(){
             public void run() {
                createAndShowUI();
    }Edited by: KingdomHeart on Feb 22, 2009 6:12 PM

Maybe you are looking for

  • Capturing elements value in the selection screen for LDB during run time

    Hi, I have a program where LDB is used. Could anyone please suggest how to capture the values of the elements present in the LDB's default selection screen. Specially, the company code and the period values. Please reply . Its too urgent. Regards, Bi

  • Regarding conversion of image to video

    Hi All, I need a small help. My requirement is to convert a text file in to an image file by attaching a background and that image is converted into video file by attaching audio file. please guide me how i can achieve this using sun java API. Is thi

  • Exception handling In File Adapter

    My requirement is that i am reading a file using file adapter but the file is not in correct format so it is not reading that file and even not making any bpel instance. so i need to add exception handling that show me the above error(file read failu

  • How to retrieve a hosstory data waveform

    Hi, I want to show the temperature waveform in labview5.1.This is a real-time system.My problem is:The waveform may be changed as time goes.I want to compare the current and the history waveform in the same graph.The detail is,the temperature curve i

  • Customer for Many plants

    For Stock Transport scenario It is possible? to maintain different plants as a customer in XD01.Now it is only possible to maintain one plant as a vendor in customer master.