Drawing in the Swing

Hi all ...
really i am new to Swing and would like to know what is the best way to draw in a JFrame for example ? and is it like the awt or there are diffrences , thanks .

http://www.google.com/search?q=JProgressBar+%2Bexamples&ie=utf-8
First link returned is this:
http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html

Similar Messages

  • Line not drawing at the correct location.

    I have a JPanel inside a JScrollPane I am drawing on and drawing is going good except for one problem. At the very end of my paint method, I want to draw a vertical line on the panel that is exactly in the middle of the current visible rectangle. From what I can tell, it should be repainting in the dead center every time repaint is called, but for some reason it isn't.
    Here is my paintComponent method:
        public void paintComponent(Graphics g)
            int wordWidth = 0;
            int wordHeight = 0;
            int duration = 0;       
            super.paintComponent(g);       
            g2d = (Graphics2D) g;
            g2d.setFont(font);
            /* draw a gradient background */
            g2d.setPaint(new GradientPaint(0, 0, new Color(100, 100, 100), 0, this.getSize().height, new Color(255, 255, 255)));
            g2d.fillRect(0, 0, this.getSize().width, this.getSize().height);
            /* draw lines for each note */
            g2d.setColor(Color.BLACK);
            for (int i = 0; i < GuiConstants.notes.values().length; i++)
                g2d.drawLine(0, (int) (GuiConstants.SONG_DISPLAY_HEIGHT - ((GuiConstants.SONG_DISPLAY_HEIGHT / 24) * (i))), this.getSize().width, (int) (GuiConstants.SONG_DISPLAY_HEIGHT - ((GuiConstants.SONG_DISPLAY_HEIGHT / 24) * (i))));
            synchronized (words)
                for (int i = 0; i < words.size(); i++)
                    /* get the width and height of the text so we can draw a box around it */
                    wordWidth = g.getFontMetrics().stringWidth(words.get(i).getLyric());
                    wordHeight = g.getFontMetrics().getHeight();
                    /* get the duration of the word */
                    duration = ((words.get(i).getEndSample() / 200) - (words.get(i).getStartSample() / 200));
                    /* draw the main box that the text sits in */
                    g2d.setColor(Color.DARK_GRAY);
                    g2d.fillRoundRect(words.get(i).getPoint().x, words.get(i).getPoint().y, wordWidth, wordHeight, 3, 3);
                    /* draw a box that will be a duration */
                    if ((i % 2) == 0)
                        g2d.setColor(Color.BLUE);
                    else
                        g2d.setColor(Color.ORANGE);
                    g2d.fillRoundRect(words.get(i).getPoint().x, words.get(i).getPoint().y, duration, wordHeight, 3, 3);
                    /* draw the text in the box */
                    g2d.setColor(Color.WHITE);
                    g2d.drawString(words.get(i).getLyric(), words.get(i).getPoint().x, words.get(i).getPoint().y + 16);
            /* draw vertical line in the middle of the panel */
            int x = ((int) this.getVisibleRect().getX()) + ((int) this.getVisibleRect().getWidth() / 2);
            System.out.println("Drawing line at " + x);
            g2d.setColor(Color.RED);
            g2d.drawLine(x, 0, x, GuiConstants.SONG_DISPLAY_HEIGHT);
        }This section is the other part I am having problems with.
    /* draw vertical line in the middle of the panel */
            int x = ((int) this.getVisibleRect().getX()) + ((int) this.getVisibleRect().getWidth() / 2);
            System.out.println("Drawing line at " + x);
            g2d.setColor(Color.RED);
            g2d.drawLine(x, 0, x, GuiConstants.SONG_DISPLAY_HEIGHT);When the panel displays for the first time, the line is painted directly in the center. When I click the right arrow on my JScrollPane, the output to the console says that is it drawing at the correct point, but actually the is never repainted and it moves with the panel as I scroll across. Now, if I click in the middle off the scrollbar to force the scrollbar indicator to scroll to where my mouse is pressed, it actually repaints correctly.
    I am just confused on how I can be printing to the console the correct location where it "says" it's painting, but it doesn't actually repaint. Any ideas?

    so wouldn't putting a panel in front of it prevent me from being able to main panel I am painting to?wouldn't it be easier to whip up a simple app, to test?
    (take only 5 minutes)
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Testing
      int xPos, yPos;
      public void buildGUI()
        JPanel overlayPanel = new JPanel();
        OverlayLayout overlay = new OverlayLayout(overlayPanel);
        overlayPanel.setLayout(overlay);
        JPanel draggingPanel = new JPanel(null);
        final JLabel label = new JLabel("Drag me");
        label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        Dimension dim = label.getPreferredSize();
        label.setBounds(50,50,dim.width,dim.height);
        draggingPanel.add(label);
        final JPanel linePanel = new JPanel(){
          public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());
        linePanel.setOpaque(false);
        overlayPanel.add(linePanel);
        overlayPanel.add(draggingPanel);
        JFrame f = new JFrame();
        f.getContentPane().add(overlayPanel);
        f.setSize(400,300);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
         label.addMouseListener(new MouseAdapter(){
          public void mousePressed(MouseEvent me){
            xPos = me.getX();
            yPos = me.getY();
        label.addMouseMotionListener(new MouseMotionAdapter(){
          public void mouseDragged(MouseEvent me){
            Point me_p = me.getPoint();
            Point l_p = label.getLocation();
            label.setLocation(l_p.x + me_p.x - xPos,l_p.y + me_p.y - yPos);
            linePanel.repaint();
      public static void main(String[] args)
        SwingUtilities.invokeLater(new Runnable(){
          public void run(){
            new Testing().buildGUI();
    }

  • How to make the diagonal line in the swing table cell?

    Hi all,
    How to make the diagonal line in the swing table cell just like the link below?
    http://61.132.17.188/webber/table.gif

    One improvement.. To get a line that is neat, use a bit of Graphics2D
        public void paintComponent(java.awt.Graphics g)
            int w = getSize().width;
            int h = getSize().height;
            // draw a line between (0,0) and (w,h)  
            g.setColor(getForeground());
            if (strTop == null)
                strTop = "  ";
            if (strBottom == null)
                strBottom = "  ";
                java.awt.Graphics2D g2 = (java.awt.Graphics2D)g;
              g2.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING,java.awt.RenderingHints.VALUE_ANTIALIAS_ON);
              //g2.setStroke(new java.awt.BasicStroke(14.0f));
            g.drawLine(0, 0, w, h);
            //the following lines will draw the two strings,
            //one above the top of the line and the above below it.     
            g2.drawString(strTop, (w/2)+2, (h/2)-2);
            g2.drawString(strBottom, 2, (h / 2) + 2);
            //the x,y for drawString are only based on assumption.   //do necessary changes to suit ur needs.
        }Then create a TableCellRenderer and set it to the Table Header for ur table's 1st row.....
    Cheers

  • How to draw on the title bar of JFrame ?

    Is there any way in which I can draw on the title bar of the JFrame. I want to display animation on the JFrame title bar. Is it possible in Swing! For example I want to display a set of concentric circles increasing and decreasing in radii.

    What child window are you referring to? Do you mean a browser window opened with web.show_document?
    If so, you can use javascript to hide browser toolbars and so on. Search the forum for some javascript examples where windows are opened with toolbar=no.

  • Draw image over Swing to mask.

    I have a window and I've overridden it make it draw all of it's components and then draw images over them (on a back buffer and then flip so the swing shouldn't ever be seen).
    It works perfectly and the swing components (JButtons and the like) catch the clicks when I click on the images masking them. The problem is when I click them for a very brief moment I see the Swing component come through. How is Swing doing this? Since I blt in a specific order and then flip. Somehow it is using the Graphics for the window directly - I overrode invalidate in a few ways but that just broke the components altogether.
    Anyone masked Swing components with images using a BufferStrategy? I would love some advice.

    AbstractButton (which JButton extends) calls JComponent.paintImmediately upon a click, so I don't think you can stop it without overriding AbstractButton and JButton (not recommended).
    Either override the paint method for your components to look like your overlaid image or remove your Swing components and test the mouse click location in the MouseListener.
    --Jon

  • Using the swing worker to populate the data in to jtablle

    Hello, i am developing my 1st application in java swings , i have developed it but when i run a module which fetchces the data from the mysql and display it in jtable my swing gui got blank for some initial time, i was thinking for some progress bar type thing but when i came acroos google i found that i can use swing worker to load my GUI on time and in the back ground i can do the task of fetching the data from the data base and populating the Jtable, but i had done a lot of try but not succeded to implement the swing worker . Please help me to implement the swing worker :( . i am using the abstractTableModal to populating the jtable it is working fine but swing worker is not there .
    Scenerio is like this i have base frame on which i got 1 or more btton one of those button is Clients when i click on it a Jdialog box appears with the jtable . Pleae tell me how to implement the swing worker class between this. Pleaseeeeeeeeeeeeeeeeeeeeee :( I am help less now .

    Fahim i want to display some photograph in to a swing application after fetching these photo graph from the database table , i dont know what swing component will be better for it and i m designing this in netbeans , so please assist me with that , how to go now and please note that it is dynamic so component should be like that :(. i m trying to create a photo album where i upload the multiple photograph first then i would like to show them each photograph will a text box when some one write some text in that text box and press enter i would like to save that text with that photo graph , I AM DONE WITH THE PHOTO UPLOADING TASK BUT DONT KNOW HOW TO DO THE REST LIKE DISPLAYING THE PHOTOGRAPH WITH THAT TEXT BOX .
    Edited by: kamal.java on Apr 27, 2012 8:59 AM

  • I can't figure out the Keynote title option. I'm a newbie so forgive my ignorance. I want to put an image in the swing arm, but I can't figure it out.

    I even tried opening in Motion and inserting it there, but when I bring it back to fc it's not there. I also want to change the background to an image or at least solid white. When I put an image in the Dropzone it doesn't show up. Any help would be GREATLY appreciated!

    To change the title background to use a clip in the drop zone, do just as Jim said.
    Also, place the clip that want to appear in the "swing arm" right below the title in the timeline.

  • Opening a new file in photoshopcc and the file name is displayed but no white working canvas. you can see it as a layer in the layers panel and if i draw on the grey area i can see int on the layer but not on the main screen.

    opening a new file in photoshop cc and the file name is displayed but no white working canvas. you can see it as a layer in the layers panel and if i draw on the grey area i can see int on the layer but not on the main screen.

    Graphics card is the problem.
    Trying to update drivers now.
    Thanks for your help
    john

  • Using the swing clipboard and DefaultEditorKit.copytAction

    Hi all
    Ive heard that i can use the swing DefaultEditorKit to basically copy and paste text throughout my application (like using Ctrl+C ctrl+V)
    basically i have a number of elements such as textpanes and edit boxes and i want to be able to copy and paste text between them with the least amount of effort, most examples i have seen about implementing copy and paste require you to specify the elements that you need to copy and paste from
    i dont want to have to specify every element, i just want a simple action that says copy the selected text, and paste the selected text (dont have to worry about cut really)
    Ctrl+C and Ctrl+V both do this natrually, but how do i call a simple method that does the same thing that the keystrokes do.
    I just want two menu items that when i invoke their click listeenrs to say something like
    copylistener{
    copy();
    Ive seen a few examples of the swing methods but these also seem to need to invoke actions on the elements that you want to copy and paste between, but the keystroke dont need any of this

    Sorry, I see your confusion now.
    Yes, you need a few helper methods to get the actual Action, but you don't need to do this for every text component because the Action is shared by all text components.
    Text components use an EditorKit. The editor kit provides support for basic functionality of each text component. (ie. cursor movement, text selection, cut, copy, paste...). The EditorKit uses Actions to implement the basic support. Each Action extends TextAction which contains a method "getFocusedComponent" which returns the last text component that currently has focus. (clicking on a menu item is only a temporary loss of focus).
    So the code in the TextComponentDemo is being very efficient and is reusing the static Action that was created by the DefaultEditorKit. So even though your form may have multiple text components, you only need to create your menu item once using any text component.
    If you don't want your code to reuse the existing Actions, you can create your own Action directly from the EditorKit. Then this Action can be used to create a JMenuItem or JButton. Here is a simple example of using this approach:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=302962

  • [svn] 3580: MXMLG-243 - Path does not draw in the correct location when width and height are set

    Revision: 3580
    Author: [email protected]
    Date: 2008-10-10 16:24:50 -0700 (Fri, 10 Oct 2008)
    Log Message:
    MXMLG-243 - Path does not draw in the correct location when width and height are set
    Fixed MatrixUtil.transformBounds to offset the four bound points by the origin
    Bug: MXMLG-243
    QA: Yes
    Doc: No
    Review: Evtim
    Ticket Links:
    http://bugs.adobe.com/jira/browse/MXMLG-243
    http://bugs.adobe.com/jira/browse/MXMLG-243
    Modified Paths:
    flex/sdk/trunk/frameworks/projects/flex4/src/mx/utils/MatrixUtil.as

    Hi,
    For web application problem, please post your thread in
    ASP.NET forum.
    Best Wishes!
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and
    makes it easier for other visitors to find the resolution later.

  • Why do I need the swing libraries?

    I made a simple "Hello World" applet and deployed it to my Apache web server. The only way I can make it run in Internet Explorer is if I deploy it with the swing libraries compiled into it. I thought that if I had a Java enabled web browser these libraries would already be on the client. Apparently this isn't so. Can anybody shed some light? Am I doing something wrong?

    Hi,
    The JVMs supplied with Both Netsape 4.x and Internet Explorer are based on Java 1.1, which did not include Swing as part of the APIs supplied with the runtime.
    You may want to consider using the Java Plugin, which allows applets to run using the latest versions of Java - see
    http://java.sun.com
    for more details.
    Failing that, you either have to require clients to have the swing libraries already or have them download with the applet (or use AWT instead of Swing).
    Brian

  • Log of the swing application

    When I run a swing application inside JDeveloper, if an exception is thrown, I see a log in Jdeveloper in 'Running' window.
    Is there such log for the swing application deployed using webstart (somewhere on the client machine)?

    Hi,
    see http://www.lecture123.com/elearning/jws-log.html
    Frank

  • How can I create a drawing like the one pictured from a photo?

    How can I create a drawing like the one pictured from a photo?

    OK, you are getting closer. Now, use your result, and place a Layer above that. Use the Pen Tool to create the "pen strokes" basically tracing over your background image. Then, when you have created each Path (or Sub-Path), choose Stroke Path, with probably the Pencil Tool and a Brush size that is small enough.
    Not sure which versions of PS these are good for, but here are some plug-ins, that might be helpful.
    AKVIS Sketch
    EdgeLine
    Sketch Effects
    Sketch Master
    I see that one of my old favorites, Flaming Pear's India Ink is not around any more.
    Good luck,
    Hunt

  • Semi transparent drawing over the picture

    Hello every one
    I am drawing the arrow image over the picture , so after drawing the arrow over the picture how to make it semi transparent so that background picture also should appear . 
     Please see the attached vi
    Attachments:
    arrow1.zip ‏2436 KB

    Thank you Strokes
    See the below pic i need this kind of output, transparent  drawing over the 2picture 
    Attachments:
    arrow transparent.PNG ‏411 KB

  • How to create file menu in the swing?

    How to create file menu or any menu in the swing?
    Please help me.

    How to create file menu or any menu in the
    swing?
    Please help me.By file menu, do you mean where you can choose a file to open or save to? If so, have you had a look at the File Chooser component? If not, please click on the link in the previous sentence to the Sun tutorial.
    Good luck
    /Pete

Maybe you are looking for