Drawing a rectangle inside a label with a picture

Hi everyone,
i want to know how you can draw a rectangle inside a label that have an iconimage
thanks in advance,
kimos

It depends if you want it only to show on the GUI or really change the image inside the IconImage.
If you just want it to show in your GUI, extend JLabel and implement paintComponent to draw a rectangle on top.
     JLabel label = new JLabel() {
          protected void paintComponent(Graphics g) {
               super.paintComponent(g);
               g.drawRect(x, y, width, height);
     };

Similar Messages

  • Want to draw some rectangles inside a swing panel object

    HI,
    I want to draw some rectangles inside one JPanel object in swing. The JPanel object should also hold some buttons, labels, etc.. I can add simple buttons, label etc. easily to the panel object but how do I add graphics (like lines, rectangles) to it ??
    an immediate response would be highly appreciated!!

    I am not sure why you are talking about the rectangle.
    To repeat I fill in a bit of code (subject to compiler errors since I Do not have a machine where I can compile or test on at the moment):
      private class ColourCube extends JPanel 
         // The size of each rectangle with colour
         private static final int ROWS = 10;
         private static final int COLS = 10;
         // To store current selection
         private int selectedIndex[] = null;
         public ColourCube()
             addMouseListener( new MouseAdapter() {
                public void mouseReleased(MouseEvent me)
                   // Find the rectangle index by dividing
                   // x and y positions then set selectedIndex
                   // unless it is the same positions as previous
                   // then set it to be null
                   fireActionEvent();
         // needed listener methods for firing events
         public void addActionListener( ActionListener al)
            // add to list
         // remove etc....
         protected void fireActionEvent()
            // Construct action event
            // call actionPerformed( ae ) on all listeners
         public void paintComponent(Graphics g)
             // Depending on size and height of this component
             // calculate out colWidth and rowHeight
             // Loop and paint the rectangles in their different colours
             for(int i = 0; i < ROWS; i++)
                for(int j = 0; j < COLS; j++)
                   // set the colour to the graphics object here from the list of colours available
                   g.setColor( ... );
                   // Added a little bit extra to have some space between
                   // each rectangle
                   g.fillRect( i * rowHeight + 1, j * colWidth + 1, colWidth - 2, rowHeight - 2 );
             if( selectedIndex != null )
                // We have a selection, lets paint the selection
                g.setColor( Color.white ); // Selection colour
                // The index contains the row in index 0 and the col in index 1
                g.drawRect( selectedIndex[0] * rowHeight, selectedIndex[1] * colWidth, colWidth, rowHeight );
      }Lacking from the above component is lacking stuff like preferedSize.
    Now, all you do is instantiate the ColourChooser, add it to your applet iether by drag and drop if you have an WYSIWYG editor, or by hand if you are so inclined.
    Voila! A composite component appears, doing the stuff it needs to be doing.
    Regards,
    Peter Norell

  • How to draw a rectangle with rounded corners?

    Hi,
    I'm trying to draw a rectangle in a custom annotation and my appearance stream looks like:
    1 1 98 98 re 0.5 w 1 j 0 0 0 RG S
    The re-operator draws the rectangle an the j-operator along with the parameter 1 sets the join style to round.
    It does make the corners very slightly rounded, but it's only visible when setting the zoom level 200% and beyond.
    Do any of you know how to make the corners more rounded for the rectangle?

    I thought so
    So Cubic Bézier curves all the way.
    Could you help me with it?

  • I want to draw rounded rectangle with dashed border

    Hello,
    I am working on flex 4.5 air 2.7 . And i want to draw rounded rectangle which has dashed border or solid border as per user's selection. Also rectangle can be rounded or simple. I am able to draw simple rectangle with dashed border. I want a solution for rounded rectangle.
    If anyone have any idea then post as soon as possible..
    Thanks
    Dhwani

    Good day!
    Could you please post a screenshot with the Layers Panel visible?
    Is the iten by any chance a Shape Layer and therefore does have a Vector Mask?
    Regards,
    Pfaffenbichler

  • MFC: Draw rectangle inside a rectangle

    Hi,
    I have drawn a rectangle:
    dc.Rectangle(10,10,200,100);
    Now i want to draw rectangle inside it by reducing 10 from each side. i.e the new rectangle will be: CRect(20,20,190,90).
    I am manually adjusting the co-ordinate, Is there any API to do this.
    offsetRect() is not solving my issue.
    Thanks

    Hi,
    I have drawn a rectangle:
    dc.Rectangle(10,10,200,100);
    Now i want to draw rectangle inside it by reducing 10 from each side. i.e the new rectangle will be: CRect(20,20,190,90).
    I am manually adjusting the co-ordinate, Is there any API to do this.
    offsetRect() is not solving my issue.
    Thanks
    Just write yourself a function and use it as needed. Programming is not manual work; the computer does the heavy lifting.
    Edit: Actually, I think CRect::DeflateRect() does what you want.
    David Wilkinson | Visual C++ MVP

  • Draw a rectangle outline with a transparent fill

    I am newbie using Elements 10.
    How do I draw a rectangle that only displays the stroke.  That is, it has a transparent fill.
    Your help would be much appreciated.
    Thanking you in anticipation.
    Roger

    On the options bar of the rectangular marquee tool, select "fixed size" and enter equal w & h values to suit.. That should give you a precise square configuration, outlined by the stroke
    Enter text on a separate layer
    Use the line tool to connect the square boxes. The line tool allows one to use arrow-heads.
    If you are still stuck, please post a representative example for additional guidance.

  • Draw multiple rectangles with mouse and allow exclusion of individual rectangles

    Hi,
    I have the following AS code to draw a single rectangle using mouse, but I need a different code that allows drawing multiple rectangles and the selection/exclusion of individual rectangles like a modeling tool (eg.: MS Visio).
    Can you help me?
    Thanks.

    What if I use MovieClips as the rectangle? When the click occurs, a new MovieClip is added dynamically to stage and when the user clicks right in a MovieClip's border it can be deleted.
    I'll try this way. And if you have any suggestions please let me know.
    Thanks.
    PS: This is the code I just forgot to include in the past post.
    const CANVAS:Graphics = graphics;
    var _dragging:Boolean = false;
    var _corner:Point;
    stage.addEventListener(MouseEvent.MOUSE_DOWN, setAnchor);
    stage.addEventListener(MouseEvent.MOUSE_UP, completeRect);
    function setAnchor(e:MouseEvent):void{
        if(!_dragging){
            CANVAS.clear();
            _corner = new Point(e.stageX, e.stageY);
            _dragging = true;
            stage.addEventListener(MouseEvent.MOUSE_MOVE, liveDrag);
    function completeRect(e:MouseEvent):void{
        if(_dragging){    
            _dragging = false;
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, liveDrag);
            CANVAS.lineStyle(0, 0, 0);
            CANVAS.beginFill(0x222222)
            CANVAS.drawRect(_corner.x, _corner.y, e.stageX - _corner.x, e.stageY - _corner.y);
    function liveDrag(e:MouseEvent):void{
        CANVAS.clear();
        CANVAS.lineStyle(0, 0x999999);
        CANVAS.drawRect(_corner.x, _corner.y, e.stageX - _corner.x, e.stageY - _corner.y);

  • Draw a rectangle from a flex app

    Hello,
    GRAND CHALLENGE (should be "hello world" that anyone should
    easily find):
    1) - a flex app with a button
    2) - when clicked the button draw the rectangle
    PROPOSED NON WORKING SOLUTION:
    draw.mxml:
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="
    http://www.adobe.com/2006/mxml"
    layout="absolute">
    <mx:Script>import DrawApp;</mx:Script>
    <mx:Script>
    <![CDATA[
    function drawNow():void{
    var myRectangle:DrawApp = new DrawApp();
    myCanvas.addChild(myRectangle.returnRectangle());
    ]]>
    </mx:Script>
    <mx:Canvas width="100%" height="100%"
    id="myCanvas"></mx:Canvas>
    <mx:Button label="draw"
    click="drawNow()"></mx:Button>
    </mx:Application>
    DrawApp.as:
    // ActionScript file
    package {
    import flash.display.*;
    class DrawApp extends Sprite
    public function DrawApp()
    //don't use the constructor anymore since it won't return
    the rectangle
    public function returnRectangle():Shape
    var rectAndCircle:Shape=new Shape();
    rectAndCircle.graphics.lineStyle(1);
    rectAndCircle.graphics.beginFill(0x0000FF,1);
    rectAndCircle.graphics.drawRect(125,0,550,575);
    rectAndCircle.graphics.endFill();
    return rectAndCircle;
    }//class
    }//package
    I've got an error "1034" (BTW as a bonus if anyone know how i
    can avoid to have the localized AS3 error messages so I can paste
    full error messages in English)
    CHALLENGE:
    Can someone show me how to make this wok before Silverlight2
    Beta get out?
    Thanks from francois who's mad trying to solve this "should
    be obvious hello world" for too long and who also discovered
    lately:
    - the lack of threads in AS3
    - the insane security policy around Flash Player (must
    install a proxy to access a public API...)
    - etc.
    Help please Oo

    Ok found the solution: needed to change the "Shape" object by
    a "UIComponent" object (don't know why).

  • Drawing a rectangle

    I want to draw a rectangle - not a filled rectangle, I can do that but say a rectangle about 5 pixels wide that I can then drag to resize and use like a frame. The pre-defined shapes increase their line thickness as you make them bigger which is not what I want.
    I could of course draw an outer rectangle in colour and then a smaller white one inside to simulate the effect I want but I'm using transparency so I don't want a filled white area.
    Please tell me I've missed something simple!

    File>new> blank file
    Access rectangular marquee tool. In the tool's option bar, one can establish a fized size
    Edit>stroke. The width of the stroke can be configured in pixels.
    Any intervening color can be removed by selecting with magic wand tool

  • Drawing a rectangle around a group of components

    I want to draw a rectangle around a group of rectangular adjacent components that are in a Canvas.  I can't seem to find the right component to do this, or I am just not doing it properly.  I tried using a Canvas inside the Canvas with the inner borders set and adding the components to the inner Canvas and placing the inner Canvas at the location of the components it now contains, but it doesn't show up.  I also tried a transparent Canvas with backgroundAlpha set to 0.  Canvas is probably not the proper component to do this with.  Is there an easier way?

    I want to draw a rectangle around a group of rectangular adjacent components that are in a Canvas.  I can't seem to find the right component to do this, or I am just not doing it properly.  I tried using a Canvas inside the Canvas with the inner borders set and adding the components to the inner Canvas and placing the inner Canvas at the location of the components it now contains, but it doesn't show up.  I also tried a transparent Canvas with backgroundAlpha set to 0.  Canvas is probably not the proper component to do this with.  Is there an easier way?

  • Can you define a specific size of a Drawing Markup rectangle?

    Hi all,
    I am using Acrobat X and have been asked to find an answer to a 'problem' someone is having.
    If you have a site plan of one's property and they submit it to me for comment (as a pdf file) can I draw a box to a specific size? Say I set the 'scale ratio' to what the drawing scale is, I go to 'comments' and from the 'Drawing Markups' pane I choose 'draw rectangle; square', I then draw a rectangle to represent a shed of a specific size. I can dimension how far the rectangle/shed is to be from the property lines and I can dimension the rectangle/shed but I have to play with stretching it to get it to my desired size based on the scale I set.
    My question; is there anyway, when I am drawing the box, to either specify an x by y size or see the lenghts of their sides once I pick a corner and start to drag the box to the other corner?
    If not, would there be a java script that could accomplish this task?
    Any help would be appreciated.
    Thanks in advance.
    Paul

    Thanks Sabian, I understand that Acrobat is not a vector drawing application and I have been using all those programs (illustrator, AutoCAD, CorelDRAW etc.) for the past 20+ years. It wouldn't seem too much to ask considering that I can set a scale to my page (in Acrobat) and I can get Acrobat to give me a dimension from point A to point B based on that said scale, so why couldn't I expect to put a rectangle markup based on that same scale?
    As for the right tool? We are a municipality that is trying to make it easier for the public to submit applications online. Those submissions are in the format of a PDF because it is unrealistic to assume the everyone in the public has access to or can afford a program like AutoCAD or illustrator. Likewise it is too COSTLY to have everyone in the city have say 'AutoCAD' or even 'illustrator' to view the application documents to markup and send back.
    I believe  In 1991, Adobe Systems co-founder John Warnock outlined a system called "Camelot"[3] that evolved into PDF (from Wikipedia) and it was a way to have an independent viewing format regardless of what software application created it. I truly believe that Acrobat for what it's initial intent was that it should incorporate some of these features as it only makes sense in an evolutionary sense. Not to be a 'drawing package' but in it's markup/comments palette to expand on them.
    So keep your comments to 'no at this time acrobat does not allow this type of functionality but who knows in the future' and don't treat me as though I don't know anything about anything. In my decades of using these applications I have seen them evolve to include more functions and do more then when the product was originally released.
    I will now go on and recommend that they seriously look at Bluebeam Revu as it is not only cheaper but it has a ton more functionality specifically designed for the needs as listed above working with PDF's (and bitmap images to boot). I was just wanting to cover all bases and give all programs a fair look.
    Thanks again for your intuitive insight.
    Paul

  • Drag and Drop Labels with line interconnected

    Dear all,
    As before, I use java.awt.dnd objects and java.awt.datatransfer objects to help me to drag and drop some labels with right-click my mouse.
    Now I would like to left-click my mouse to draw a line between the two labels by pressing the left-click on label 1 and drag and drop to label 2. Then I would like to see a now line created betwwen label 1 and 2. I have an idea to addMouseListener and addMouseMotionListener on the labels to get the position of the labels and draw a line with the position. But I found that both right-click and left-click can trigger this event. Can I give some restriction on that so only left-click can trigger this event.
    Also I cannot do it successfully as I found that after I rewrite the paintComponent method, the older image cannot be cleared and make the screen become verbose. Do I missing something on rewrite the paintComponent method? Also do you have any other suggestion on the task of drawing a line between the 2 labels?
    Thanks a lot!
    Peter

    Of course you can select only left click events :
    You simply add a test like that in your Mouse(Motion)Listener's methods :
    if (SwingUtilities.isLeftMouseButton(aMouseEvent)) {
    I hope this helps,
    Denis

  • BUG: Rectangles inside FW symbols become blurry/misaligned

    There seems to be a problem with the use of Rectangles within Symbols in Fireworks CS6, CS5 and earlier.
    The problem is that they become blurry. More exactly, they become repositioned within the symbol—aligned to the half-pixel—and their edges become anti-aliased as a result. For example, here's a series of rectangles that I'd created on the canvas to visualize a set of color swatches:
    Here's the same set of rectangles after having been saved as a Symbol and imported into another document:
    At first I thought this was a problem with Symbols in general, but then I discovered that it only occurred with Rectangles—a.k.a. "rectangle primitives", which are a special kind of grouped vector object created using the Rectangle tool—and only when the contents of the Symbol in which they resided had a pixel width or height ending in an odd number (i.e., 1, 3, 5, 7 or 9).
    THE PROBLEM
    Here's what I've observed, in greater detail. Start with two Rectangles, 99 x 99 pixels—one with a stroke only, the other with a fill only. Select the first Rectangle and choose Modify > Symbol > Convert to Symbol, and then do the same for the second. Save the file. Everything looks OK, right?
    Now close the file, and reopen it. Have a look at the thumbnail previews within your Document Library. They look a little... fuzzy, yes?
    Click on the name of one of the symbols in the Document Library to open the Symbol Properties dialog, and then click OK. Now look on the canvas...
    Yikes. The symbol instance containing the stroked Rectangle (on the left) has become quite blurred. Now, back within the Document Library panel, click on the thumbnail preview of the other symbol to enter Edit Symbol mode, and then return to the document canvas...
    Yikes again. The symbol instance containing the fill Rectangle (on the right) has also become blurred—horizontally, in this case. They both look worse now.
    Note that it's not the instances nor their placement on the canvas that's the problem—you can move them anywhere on the canvas, and they'll still look just as bad—but the symbols themselves. Double click on one of the symbols to enter Edit Symbol mode and select the rectangle. Its X-Y position in the Properties panel will look normal, but if you open the Transform panel extension (http://www.senocular.com/fireworks/extensions/?entry=572), you'll see the real story: The rectangle has been re-aligned to the half-pixel.
    Importantly, the trigger here isn't the dimensions of the rectangle, per se, but of the symbol contents as a whole. If the symbol contents have an even-numbered dimension, alignment remains crisp and pixel-perfect along that dimension; if the symbol contents have an odd-numbered pixel dimension, a blurry misalignment occurs. For example, here's a series of "even" rectangles (20 x 20 pixels) that, with their single-pixel spacing, comprise an odd-numbered selection (125px), shown before and after symbol conversion:
    Incidentally, the same problem reveals itself when you import these symbols into another document using the Import Symbols command within the Document Library options menu.
    THE FIX
    The patch for this bug, once it's occurred, is to edit the symbol. In Edit Symbol mode, select all objects—or just the misaligned Rectangle objects—and choose Modify > Snap To Pixel.
    The fly in the ointment here is that, in many cases, the original alignment is not preserved. The rectangles may now be misaligned in relation to other objects in the symbol, as well as to other objects on the canvas. Therefore, to finish the job, you may need to also nudge the rectangle(s) by a pixel horizontally or vertically using the keyboard arrow keys.
    The good news is that once a symbol has been fixed using Snap To Pixel, its alignment and clarity should remain fixed.
    THE WORKAROUND
    The surest way to avoid this issue, of course, is to avoid including Rectangles in your symbols—not the geometric shape, but the grouped vector object as it's created by the Rectangle tool. Therefore, prior to converting a selection to a symbol, check for any Rectangles within the selection and ungroup them using the Modify > Ungroup command, which will convert them into simple paths, unaffected by this bug. (Chances are, by the time you're converting these objects to symbols, you won't need the fluid resizing or adjustable rounded corners that Rectangle objects offer; the trick will be remembering to perform this extra step.)
    THE BUG REPORT
    Lastly, here's the bug report that I submitted to Adobe regarding this issue:
    Product name: Fireworks
    Product Version: 12.0.0.236
    Product Language: English
    Your operating system: Mac OS 10.6.8
    ******BUG******
    Concise problem statement: Rectangles within Symbols become blurry if the width or height of the symbol contents is an odd number (e.g., 21, 23, 25, 27, 29, etc.). The blurriness is caused by misalignment of Rectangles on the half-pixel within the symbol, and becomes apparent upon closing and reopening the document and either a) viewing the symbol's Document Library thumbnail preview, or b) clicking to view Symbol Properties or to Edit Symbol. It can also be observed upon Importing the symbol into the Document Library of another document.
    Steps to reproduce bug:
    In an open FW document, draw a Rectangle and set the width and/or height to an odd number (e.g., 99 x 99 pixels).
    Select the Rectangle and Convert to Symbol.
    Save and close the document.
    Reopen the document. In the Document Library, double-click the symbol name to open the Symbol Properties dialog, and click OK. (Or double-click the symbol preview to Edit, and then return to the document canvas.)
    Results: The Symbol instance appears blurry on the canvas. (The Symbol itself also appears blurry within Edit mode.)
    Expected results: The Symbol should appear as it was originally created, without a loss of clarity or change in alignment.
    Note that this bug can occur whether a rectangle's dimensions are even or odd; the determining factor is the dimensions of the symbol contents as a whole. However, the bug affects Rectangles only; it has not been observed with paths, ellipses, auto shapes, bitmaps, or groups.
    The Transform panel extension was used to view the precise pixel alignment of objects and determine the cause of the blurriness.
    For more information, including graphic demonstrations and workarounds, please see the following forum post: http://forums.adobe.com/thread/1073489?tstart=0
    This bug has also been observed in Fireworks 8 and CS5 on Mac OS 10.6.8 (Snow Leopard).
    POSTSCRIPT
    Amazingly, this bug can also affect Symbol instances on the canvas that have been "broken apart". To see this for yourself, create a symbol like one of the preceding examples, select the symbol instance on the canvas and choose Modify > Symbol > Break Apart. Save and close the file, then reopen it. The "broken apart" instance on the canvas will now appear blurry/misaligned(!).

    Thanks, Petros_!
    That's good input. If this issue is important to you, consider submitting your own bug report. Feel free to copy and paste from mine, if it helps. (Just be sure to verify all results on your own setup first.)
    Whenever possible, I'm trying to post bug reports on this forum in addition to submitting them to Adobe. Partially, this is to allow me to include graphics or go into slightly more detail, and partially it's to make bug reporting more transparent—to raise awareness of known issues among fellow users and reduce duplication of effort, to create a record, and to increase public visibility of these issues. It also allows for corrections or additions to be made by myself or other users.
    However, it's not a true bug reporting system. So we don't know whether the Fireworks team has looked at this yet, whether it has been or will be assigned to someone, or whether your comment on this thread will be read by anyone on the Fireworks team.
    Nevertheless, it is quite an investment of time tracking down a bug and writing it up like this, so I appreciate the kudos!

  • HELP! Handles missing when I draw a rectangle?!

    HELP! The handles are missing when I draw a rectangle with the rectangle tool. So I can't transform it Both Bounding box and edges are set to be visible, so it's not that! Is it a bug in the latest version? I have never had trouble with that before!

    camras,
    I am afraid you have come across the Live Rectangle bug which is limited to the MAC versions starting with 10.7 and 10.8, but not 10.9 (Mavericks) or 10.10 (Yosemite). Hopefully, the bug will be fixed soon.
    So a switch to Mavericks or Yosemite with a reinstallation might be the way to solve it here and now.
    To get round it in each case, it is possible to Expand the Live Rectangles to get normal old fashioned rectangles, or to Pathfinder>Unite, or to use the Scale Tool or the Free Transform Tool.
    A more permanent way round that is to create normal old fashioned rectangles, after running the free script created by Pawel, see this thread with download link:
    https://forums.adobe.com/thread/1587587

  • Drawing a rectangle using a mouse

    Need a simple application that demostrates the use of the mouse on an application window. All that I require is that a user can draw a rectangle by dragging the mouse on the application window. The upper left coordinate should be the location where the user presses the mouse button and the lower right should be where the user releases the mouse button. Should also display the area of the rectangle in a JLabel in the SOUTH region of a BorderLayout.
    Thanks

    How is this:
    import java.awt.event.*;  
    import java.awt.*;
    import javax.swing.*;
    public class DrawR extends JFrame
         JLabel label = new JLabel();
    public DrawR()
         addWindowListener(new WindowAdapter()
        {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);
         setBounds(50,50,450,400);
         getContentPane().setLayout(new BorderLayout());
         Draw draw = new Draw();     
         getContentPane().add("Center",draw);
         getContentPane().add("South",label);
        setVisible(true);     
    public class Draw extends JPanel
         Rectangle r = null;
    public Draw()
         addMouseListener(new MouseAdapter()     
         {     public void mousePressed(MouseEvent m)
                    r = new Rectangle(m.getX(),m.getY(),1,1);
              public void mouseReleased(MouseEvent m)
         addMouseMotionListener(new MouseMotionAdapter()          
         {     public void mouseDragged(MouseEvent m)
                   r.width  = m.getX() - r.x;
                   r.height = m.getY() - r.y;
                   repaint();
                   label.setText("   "+r);
    public void paintComponent(Graphics g)
         super.paintComponent(g);
         if (r != null) ((Graphics2D)g).draw(r);
    public static void main(String[] args )
         new DrawR();
    Noah
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class DrawR extends JFrame
         JLabel label = new JLabel();
    public DrawR()
         addWindowListener(new WindowAdapter()
    {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);
         setBounds(50,50,450,400);
         getContentPane().setLayout(new BorderLayout());
         Draw draw = new Draw();     
         getContentPane().add("Center",draw);
         getContentPane().add("South",label);
    setVisible(true);     
    public class Draw extends JPanel
         Rectangle r = null;
    public Draw()
         addMouseListener(new MouseAdapter()     
         {     public void mousePressed(MouseEvent m)
                   r = new Rectangle(m.getX(),m.getY(),1,1);
              public void mouseReleased(MouseEvent m)
         addMouseMotionListener(new MouseMotionAdapter()          
         {     public void mouseDragged(MouseEvent m)
                   r.width = m.getX() - r.x;
                   r.height = m.getY() - r.y;
                   repaint();
                   label.setText(" "+r);
    public void paintComponent(Graphics g)
         super.paintComponent(g);
         if (r != null) ((Graphics2D)g).draw(r);
    public static void main(String[] args )
         new DrawR();
    }

Maybe you are looking for