Draw a rectangle on an image in an applet

Hello
I have an applet that has 3 buttons. Each of them creates an image, which is displayed on the screen. Now, I want to draw a rectangle on these images. Since I already have a paint method for displaying the images correctly, I would need a second paint method for the rectangle. (I can't put everything in the same paint() method, because I have an horrible resukt)
Does anyone have an idea of a method that would be able to replace a paint method?
Any help would be appreciated
Thanks
Philippe

Have you thought about using one of your button to get the action done? You could use the Graphics method to create your object rectangle.
Don't know if it would work but seems like a good idea to try.

Similar Messages

  • Drawing a rectangle on an Image embedded in a JLabel

    hey everybody,
    I'm trying to implement a program that can select parts of an image using a rectangle that's formed while the mouse is dragging. The image is embedded within the JLabel and I have already overriden the paintcomponents method. So far, I'm able to draw lines and shapes on the image before the program runs. But I need to be able to draw in real time and I don't know how to do that. Can someone please help me? Thanks a lot.
    Here's my code if anyone wants to look at it:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import javax.imageio.*;
    public class test{
         static int x1, y1, x2,y2;
         static boolean drawing;     
         static Graphics gr;
         public static void main(String[] args){
         JFrame jframe = new JFrame();
         jframe.setSize(1000,800);
         jframe.setVisible(true);
              WindowListener c = new WindowAdapter() {
                   public void windowClosing(WindowEvent e) {
                        System.exit(0);
              jframe.addWindowListener(c);
         ImageIcon icon = new ImageIcon("c:/60.jpg");
         Image i = icon.getImage();
         JLabel lab = new JLabel(icon)
              public void paintComponent(Graphics gr) {
                        super.paintComponent(gr);               
                        paints(gr);          
         MouseMotionListener drag = new MouseMotionAdapter() {
                        public void mouseDragged(MouseEvent e) {
                             x2 = e.getX();
                             y2 = e.getY();
                             drawing = true;
                             //System.out.println(e.getX()+" "+e.getY());
         MouseListener click = new MouseAdapter() {
                   public void mousePressed(MouseEvent e) {
                        x1 = e.getX();
                        y1 = e.getY();
                        drawing = true;
                   public void mouseReleased(MouseEvent e) {
                        x2 = e.getX();
                        y2 = e.getY();
                        drawing = false;
         lab.addMouseMotionListener(drag);
         lab.addMouseListener(click);
         JPanel pan = new JPanel();
         pan.add(lab);
         jframe.getContentPane().add(pan);
         jframe.setVisible(true);
    public static void paints (Graphics gr)
         Graphics2D write = (Graphics2D)gr;
                   write.setColor(Color.BLUE);
                   int x3 = Math.abs(x1-x2);
                   int y3 = Math.abs(y1-y2);
                   write.drawRect(x1,y1,x3,y3);
    }

    hey everybody,
    I'm trying to implement a program that can select parts of an image using a rectangle that's formed while the mouse is dragging. The image is embedded within the JLabel and I have already overriden the paintcomponents method. So far, I'm able to draw lines and shapes on the image before the program runs. But I need to be able to draw in real time and I don't know how to do that. Can someone please help me? Thanks a lot.
    Here's my code if anyone wants to look at it:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    import javax.imageio.*;
    public class test{
         static int x1, y1, x2,y2;
         static boolean drawing;     
         static Graphics gr;
         public static void main(String[] args){
         JFrame jframe = new JFrame();
         jframe.setSize(1000,800);
         jframe.setVisible(true);
              WindowListener c = new WindowAdapter() {
                   public void windowClosing(WindowEvent e) {
                        System.exit(0);
              jframe.addWindowListener(c);
         ImageIcon icon = new ImageIcon("c:/60.jpg");
         Image i = icon.getImage();
         JLabel lab = new JLabel(icon)
              public void paintComponent(Graphics gr) {
                        super.paintComponent(gr);               
                        paints(gr);          
         MouseMotionListener drag = new MouseMotionAdapter() {
                        public void mouseDragged(MouseEvent e) {
                             x2 = e.getX();
                             y2 = e.getY();
                             drawing = true;
                             //System.out.println(e.getX()+" "+e.getY());
         MouseListener click = new MouseAdapter() {
                   public void mousePressed(MouseEvent e) {
                        x1 = e.getX();
                        y1 = e.getY();
                        drawing = true;
                   public void mouseReleased(MouseEvent e) {
                        x2 = e.getX();
                        y2 = e.getY();
                        drawing = false;
         lab.addMouseMotionListener(drag);
         lab.addMouseListener(click);
         JPanel pan = new JPanel();
         pan.add(lab);
         jframe.getContentPane().add(pan);
         jframe.setVisible(true);
    public static void paints (Graphics gr)
         Graphics2D write = (Graphics2D)gr;
                   write.setColor(Color.BLUE);
                   int x3 = Math.abs(x1-x2);
                   int y3 = Math.abs(y1-y2);
                   write.drawRect(x1,y1,x3,y3);
    }

  • Filling a rectangle space with a photo image in an applet

    I'm creating a web page that will display an image of a persons wall and I want to be able to use the mouse to draw a rectangle on the image of the wall and fill the space with a selected photo image.
    I have found out that you can use the MousePress and MouseDrag events to draw a rectangle and "paint" the rectangle in a componant inside the applet. But I'm not sure how to fill the rectangle with an image instead of a color. Can someone tell me if there is an AWT or Swing method that can do this?
    I was thinking of filling the applet window with a label component and then filling it with the wall image. Then some how using the coordinates given from the drag event to creat a new label component and filling it with the photo image. Hope I'm on the right path. Thanks for all the help.

    Great thanks, I looked it up in the docs and I found one that will work for me but I'm not sure how to use the arguements. It's the drawImage method that uses the scalebility. This would work perfect seeing as though the image to be posted will be various sizes. Can any one explain this method in more detail for me. Thanks again.

  • Draw a rectangle..PLease Help !!Its Urgent

    Hi all
    I need to draw a rectangle on an image background.I caught hold of the graphics object of the image and tried to draw a rectangle but it doesnot seem to work
    Can anyone help me!!Please Its Urgent

    You didn't post any code so I have no idea what you're doing.
    Here is an example how to create an empty image and draw a black rectangle on it:
    BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, image.getWidth(), image.getHeight());
    g.setColor(Color.BLACK);           
    g.drawRect(10, 10, 80, 80);

  • 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

  • How to draw 2D shapes on the image generated by JMF MediaPlayer?

    Hello all:
    IS there any way that we can draw 2D shapes on the image generated by JMF
    MediaPlayer?
    I am currently working on a project which should draw 2D shapes(rectangle, circle etc) to
    mark the interesting part of image generated by JMF MediaPlayer.
    The software is supposed to work as follows:
    1> first use will open a mpg file and use JMF MediaPlayer to play this video
    2> if the user finds some interesting image on the video, he will pause the video
    and draw a circle to mark the interesting part on that image.
    I know how to draw a 2D shapes on JPanel, however, I have no idea how I can
    draw them on the Mediaplayer Screen.
    What technique I should learn to implement this software?
    any comments are welcome.
    thank you
    -Daniel

    If anyone can help?!
    thank you
    -Daniel

  • Need help about dragging a rectangle on a image

    hello
    i need to draw a rectangle on image .this is my code does this code support my requirement .
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    public class RectangleDrag extends JPanel {
    Rectangle tt=new Rectangle();
    int sx=0,sy=0,dx=0,dy=0;
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d=(Graphics2D)g;
    g2d.draw(tt);
    private MouseInputAdapter mia = new MouseInputAdapter() {
    public void mousePressed(MouseEvent e) {
    sx=e.getX(); //start x
    sy=e.getY(); // start y
    public void mouseDragged(MouseEvent e) {
    dx=e.getX(); //end x
    dy=e.getY(); // end y
    int width=Math.abs(dx-sx);
    int height=Math.abs(dy-sy);
    int side=Math.max(width, height);
                   int side1=Math.min(width,height);
    int x, y;
    if(sx<=dx) {
    x=sx;
    } else {
    x=sx-side;
    if(sy<=dy) {
    y=sy;
    } else {
    y=sy-side;
    tt.setRect(x,y,side,side1);
    repaint();
         private JPanel getContent(BufferedImage image) {
    JPanel panel = new JPanel(new GridLayout(1,0));
    panel.add(new JLabel(new ImageIcon(image)));
    panel.add(new JLabel(new ImageIcon(getImage(image))));
    return panel;
    public static void main(String[] args) {
    RectangleDrag test = new RectangleDrag();
    test.addMouseListener(test.mia);
    test.addMouseMotionListener(test.mia);
              String path = "C:/Documents and Settings/v.kirankumar/My Documents/jpg/32370.jpg";
    BufferedImage image = ImageIO.read(new File(path));
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.getContentPane().add(test.getContent(image));
    // f.pack();
    // f.getContentPane().add(test);
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }

    this is the modified code here am getting an image but i couldnt draw rectangle exactly on the image.
    getImage(image); isnt making any difference
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    public class RectangleDrag extends JPanel {
    Rectangle tt=new Rectangle();
    int sx=0,sy=0,dx=0,dy=0;
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d=(Graphics2D)g;
    g2d.draw(tt);
    private MouseInputAdapter mia = new MouseInputAdapter() {
    public void mousePressed(MouseEvent e) {
    sx=e.getX(); //start x
    sy=e.getY(); // start y
    public void mouseDragged(MouseEvent e) {
    dx=e.getX(); //end x
    dy=e.getY(); // end y
    int width=Math.abs(dx-sx);
    int height=Math.abs(dy-sy);
    int side=Math.max(width, height);
    int side1=Math.min(width,height);
    int x, y;
    if(sx<=dx) {
    x=sx;
    } else {
    x=sx-side;
    if(sy<=dy) {
    y=sy;
    } else {
    y=sy-side;
    tt.setRect(x,y,side,side1);
    repaint();
    private JPanel getContent(BufferedImage image) {
    JPanel panel = new JPanel(new GridLayout(1,0));
    panel.add(new JLabel(new ImageIcon(image)));
    return panel;
    public static void main(String[] args) {
    RectangleDrag test = new RectangleDrag();
    test.addMouseListener(test.mia);
    test.addMouseMotionListener(test.mia);
    String path = "C:/Documents and Settings/v.kirankumar/My Documents/jpg/32370.jpg";
    BufferedImage image = ImageIO.read(new File(path));
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(test.getContent(image));
    f.pack();
    f.getContentPane().add(test);
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    }

  • How to draw an arc in the image window?

    Now I try to draw an arc in the image window, but when I try to use IMAQ Overlay Arc.vi, it need Bounding Rectangle that made a problem because the arc I draw has a big size whose bounding rectangle was out of the image window. What should I do?

    Hello,
    Bounding rectangle parameters will allow you to enter larger values than your image size as well as negative numbers in order to position your arc properly.
    I hope this helps!
    Regards,
    Yusuf C.
    Applications Engineering
    National Instruments

  • Drawing on top of an image

    Hello!
    I am trying to create a Panel on which I can draw stuff using the mouse. I have managed to get it working, apart from one small part. I want to have an image (jpg) as a background, and as soon as I try to add an image either the image does not show or the drawing stuff stops working.
    Here is my working drawing code (i.e. without image):
    public void paintComponent(Graphics g) {
             super.paintComponent(g);
             if (image == null || image.getWidth(null) != getWidth()
                   || image.getHeight(null) != getHeight()) {
                image = (BufferedImage) createImage(getWidth(), getHeight());
                graph = (Graphics2D) image.getGraphics();
                graph.fillRect(0, 0, getWidth(), getHeight());
                graph.setColor( Color.white );
                graph.setStroke(new BasicStroke(3));
                graph.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                      RenderingHints.VALUE_ANTIALIAS_ON));
             Rectangle r = g.getClipBounds();
             g.drawImage(image, r.x, r.y, r.x + r.width, r.y + r.height, r.x, r.y,
                   r.x + r.width, r.y + r.height, null);
             if (endPoint != null) {
                g.setColor(currentColor);
                if (shapeType == 0)
                   g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
                if (shapeType == 1)
                   g.drawOval(pn.x, pn.y, pn.width, pn.height);
                if (shapeType == 2)
                   g.drawRect(pn.x, pn.y, pn.width, pn.height);
             public void mouseDragged( MouseEvent m ) {
                if (shapeType > 3 && shapeType != 8)
                   return;
                if (endPoint == null)
                   endPoint = new Point();
                repaint(pn.x, pn.y, pn.width + 1, pn.height + 1);
                pn.x = Math.min(startPoint.x, m.getX());
                pn.y = Math.min(startPoint.y, m.getY());
                pn.width = Math.abs(m.getX() - startPoint.x);
                pn.height = Math.abs(m.getY() - startPoint.y);
                if (shapeType == 0) {
                   endPoint.setLocation(m.getPoint().getLocation());
                if (shapeType == 3) {
                   graph.setColor(currentColor);
                   graph.drawLine(startPoint.x, startPoint.y, m.getX(), m.getY());
                   startPoint = m.getPoint();
                repaint(pn.x, pn.y, pn.width + 1, pn.height + 1);
             public void mouseReleased(MouseEvent m) {
                if (endPoint != null)
                   draw();
                endPoint = null;
             public void mousePressed(MouseEvent m) {
                startPoint = m.getPoint();
             public void draw() {
                graph.setColor(currentColor);
                if (shapeType == 0)
                   graph.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
                if (shapeType == 1)
                   graph.drawOval(pn.x, pn.y, pn.width, pn.height);
                if (shapeType == 2)
                   graph.draw(pn);
                repaint(pn.x, pn.y, pn.width + 1, pn.height + 1);
             }To me it seemed logical to add:
    graph.drawImage(my_gif, 0, 0, this);at the end of the if statement, but that did not work.
    Where and how should I add the image?
    Thanks!

    Thanks for answering!
    Here is the entire code:
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.BufferedImage;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    @SuppressWarnings("serial")
    public class ImageExample extends JApplet {
       private static final Dimension DRAWING_AREA_SIZE = new Dimension(300,300);
       private static final Point DRAWING_AREA_UPPER_LEFT = new Point(100,50);
       private static final Color BACKGROUND_COLOR = Color.black;
       private Image my_gif;
       private Color currentColor = Color.white;
       private JLabel currentColorIndicator;
       private JPanel pane = new JPanel(null);
       Point startPoint, endPoint;
       Rectangle pn = new Rectangle();
       BufferedImage image;
       Graphics2D graph;
       int shapeType = 3;
       public void init() {
          setContentPane(pane);
          my_gif = getImage( getDocumentBase(), "fractal.gif" );
          pane.setOpaque(true);
          pane.setBackground(BACKGROUND_COLOR);
          createColorMap();
          DrawingPanel drawingPanel = new DrawingPanel();
          drawingPanel.setBounds(DRAWING_AREA_UPPER_LEFT.x, DRAWING_AREA_UPPER_LEFT.y, DRAWING_AREA_SIZE.width,
                DRAWING_AREA_SIZE.height);
          pane.add(drawingPanel);
       // Private methods
       private void createColorMap() {
          ColorPickerListener listener = new ColorPickerListener();
          JLabel colorText = new JLabel("Color");
          colorText.setBounds(10, DRAWING_AREA_UPPER_LEFT.y-20, 50, 25);
          colorText.setForeground(Color.white);
          pane.add(colorText);
          JLabel redColor = new JLabel("");
          redColor.addMouseListener(listener);
          redColor.setBackground(Color.RED);
          redColor.setBounds(10, DRAWING_AREA_UPPER_LEFT.y+10, 50, 30);
          redColor.setOpaque(true);
          pane.add(redColor);
          JLabel blueColor = new JLabel("");
          blueColor.addMouseListener(listener);
          blueColor.setBackground(Color.blue);
          blueColor.setBounds(10, DRAWING_AREA_UPPER_LEFT.y+40, 50, 30);
          blueColor.setOpaque(true);
          pane.add(blueColor);
          JLabel whiteColor = new JLabel("");
          whiteColor.addMouseListener(listener);
          whiteColor.setBackground(Color.white);
          whiteColor.setBounds(10, DRAWING_AREA_UPPER_LEFT.y+70, 50, 30);
          whiteColor.setOpaque(true);
          pane.add(whiteColor);
          JLabel greenColor = new JLabel("");
          greenColor.addMouseListener(listener);
          greenColor.setBackground(Color.green);
          greenColor.setBounds(10, DRAWING_AREA_UPPER_LEFT.y+100, 50, 30);
          greenColor.setOpaque(true);
          pane.add(greenColor);
          JLabel blackColor = new JLabel("");
          blackColor.addMouseListener(listener);
          blackColor.setBackground(Color.black);
          blackColor.setBounds(10, DRAWING_AREA_UPPER_LEFT.y+130, 50, 30);
          blackColor.setOpaque(true);
          pane.add(blackColor);
          JLabel grayColor = new JLabel("");
          grayColor.addMouseListener(listener);
          grayColor.setBackground(Color.gray);
          grayColor.setBounds(10, DRAWING_AREA_UPPER_LEFT.y+160, 50, 30);
          grayColor.setOpaque(true);
          pane.add(grayColor);
          JLabel yellowColor = new JLabel("");
          yellowColor.addMouseListener(listener);
          yellowColor.setBackground(Color.yellow);
          yellowColor.setBounds(10, DRAWING_AREA_UPPER_LEFT.y+190, 50, 30);
          yellowColor.setOpaque(true);
          pane.add(yellowColor);
          // Display currently chosen color
          JLabel currentColorText = new JLabel("Current");
          currentColorText.setBounds(10, DRAWING_AREA_UPPER_LEFT.y+230, 50, 25);
          currentColorText.setForeground(Color.white);
          pane.add(currentColorText);
          currentColorIndicator = new JLabel("");
          currentColorIndicator.setBounds(10, DRAWING_AREA_UPPER_LEFT.y+260, 50, 30);
          updateCurrentColorLabel();
          currentColorIndicator.setOpaque(true);
          pane.add(currentColorIndicator);
       private void updateCurrentColorLabel() {
          currentColorIndicator.setBackground(currentColor);
          currentColorIndicator.setForeground(new Color(255 - currentColor.getRed(), 255 - currentColor.getGreen(),
                255 - currentColor.getBlue()));
       // Private classes
       private class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener {
          BufferedImage image;
          public DrawingPanel() {
             addMouseMotionListener(this);
             addMouseListener(this);
          public void paintComponent(Graphics g) {
             super.paintComponent(g);
             if (image == null || image.getWidth(null) != getWidth()
                   || image.getHeight(null) != getHeight()) {
                image = (BufferedImage) createImage(getWidth(), getHeight());
                graph = (Graphics2D) image.getGraphics();
                graph.fillRect(0, 0, getWidth(), getHeight());
                graph.setColor( Color.white );
                graph.setStroke(new BasicStroke(3));
                graph.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                      RenderingHints.VALUE_ANTIALIAS_ON));
                graph.drawImage(my_gif, 0, 0, this);
                System.out.println(my_gif + " <- my_gif");
                System.out.println(my_gif.getWidth(this) + " <- my_gif.getWidth(this)");
                System.out.println(my_gif.getHeight(this) + " <- my_gif.getheight(this)");
             Rectangle r = g.getClipBounds();
             g.drawImage(image, r.x, r.y, r.x + r.width, r.y + r.height, r.x, r.y,
                   r.x + r.width, r.y + r.height, null);
             if (endPoint != null) {
                g.setColor(currentColor);
                if (shapeType == 0)
                   g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
                if (shapeType == 1)
                   g.drawOval(pn.x, pn.y, pn.width, pn.height);
                if (shapeType == 2)
                   g.drawRect(pn.x, pn.y, pn.width, pn.height);
             public void mouseDragged( MouseEvent m ) {
                if (shapeType > 3 && shapeType != 8)
                   return;
                if (endPoint == null)
                   endPoint = new Point();
                repaint(pn.x, pn.y, pn.width + 1, pn.height + 1);
                pn.x = Math.min(startPoint.x, m.getX());
                pn.y = Math.min(startPoint.y, m.getY());
                pn.width = Math.abs(m.getX() - startPoint.x);
                pn.height = Math.abs(m.getY() - startPoint.y);
                if (shapeType == 0) {
                   endPoint.setLocation(m.getPoint().getLocation());
                if (shapeType == 3) {
                   graph.setColor(currentColor);
                   graph.drawLine(startPoint.x, startPoint.y, m.getX(), m.getY());
                   startPoint = m.getPoint();
                repaint(pn.x, pn.y, pn.width + 1, pn.height + 1);
             public void mouseReleased(MouseEvent m) {
                if (endPoint != null)
                   draw();
                endPoint = null;
             public void mousePressed(MouseEvent m) {
                startPoint = m.getPoint();
             public void draw() {
                graph.setColor(currentColor);
                if (shapeType == 0)
                   graph.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
                if (shapeType == 1)
                   graph.drawOval(pn.x, pn.y, pn.width, pn.height);
                if (shapeType == 2)
                   graph.draw(pn);
                repaint(pn.x, pn.y, pn.width + 1, pn.height + 1);
             public void mouseClicked(MouseEvent arg0) {}
             public void mouseEntered(MouseEvent arg0) {         }
             public void mouseExited(MouseEvent arg0) {         }
             public void mouseMoved(MouseEvent arg0) {         }
       //Listeners
       private class ColorPickerListener extends MouseAdapter {
          public void mouseClicked(MouseEvent e) {
             JLabel color = (JLabel) e.getSource();
             currentColor = color.getBackground();
             updateCurrentColorLabel();
    }So the problem might be that the system.outs in there put out:
    sun.awt.image.ToolkitImage@1a0c10f <- my_gif
    -1 <- my_gif.getWidth(this)
    -1 <- my_gif.getheight(this)I don't know how to fix so that the height and width are set. Isn't it enough to do the:
          my_gif = getImage( getDocumentBase(), "fractal.gif" );before initializing the drawing panel?
    Thanks again!

  • Is it possible to draw shapes on an background image?

    Hi all,
    I wanna ask if it is possible for loading an jpg as background image in Canvas, and draw shapes(ovals/rectangles) on it?
    I've tried, but it seems the shapes always covered by the image. How can I due with this? I want the shapes drawn on top of the image, not underneath it.
    Please Help! Thx.

    Am I doing anything wrong? cos I've already draw the shapes after the image in the paint method.
    Here's some of the program coding:
    // init
    PictCanvas coffeeCanvas=new PictCanvas();
    //set up Picture Canvas
    coffeeCanvas.thePic=getImage(getCodeBase(),"50p.jpg");
    coffeeCanvas.setSize(200,200);
    // paint method
    public void paint(Graphics g)
    g.drawImage(thePic,0,0,this);
    g.drawOval(0,0,20,20);
    repaint();
    }

  • How do you draw a rectangle in JavaScript?

    I am developing an ASP.NET application. There is an image on one of my pages. Users need to be able to draw a rectangle on which part of the image they want to zoom-in to. Basically, when the user clicks on the image, that XY location will be one corner of the rectangle. As the user moves the mouse over the image, I want to be able to show the rectangle. Once they let go of the mouse, I need to send those coordinates to the server and perform the zoom operation.
    Does anyone have code that does this?

    How, exactly, did you decide that a forum called "New
    To Java Technology" was the place to ask this
    question??Well he is clearly new to java technology since he dosn't realize that java and javascript are two completley different things.

  • 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);
         };

  • Photoshop CC: When I'm trying to draw a rectangle I can't fill it or create a stoke because there is a message telling me; "Could not complete your request because of a program error. What can I do?

    Photoshop CC: When I'm trying to draw a rectangle I can't fill it or create a stoke because there is a message telling me; "Could not complete your request because of a program error. What can I do?

    It is a mute point now. PSE12 finally failed to the point it locked up the MAC. I had to restore the MAC OS from backup to a point before the PSE12 update.
    The MAC now operates correctly and so does PSE12. I don't think I'll be updating PSE any time soon.

  • How can I draw on top of an image?

    I'm using a JApplet with three JPanel's inside of it.
    Inside one of those JPanel's I would like to place an image and a button. When the button is clicked, I want to draw an oval on top of the image . Each subsequent time the button is clicked, the oval will move to a different location.
    So here are my questions:
    1) What should I use to draw the image? (a JLabel with an ImageIcon on it?)
    2) Could I simply use g.drawOval() in paintComponent() to directly draw on top of the image/JLabel?
    Any help will be greatly appriciated.

    Here's a sample to study;-import java.awt.*;
    import java.awt.event.*;
    public class DrawOnImage extends java.applet.Applet{
      int xPos, yPos;
      Image img;
      public void init() {
        add(new Label("Hello World") );
        Button press = new Button("press");
        add(press);
        press.addActionListener(new ActionListener(){
          public void actionPerformed(ActionEvent e){
            xPos = (int)(Math.random()*270);
            yPos = (int)(Math.random()*170)+30;
            repaint();
        img = getImage(getDocumentBase(), "anImage.JPG");
      public void paint(Graphics g){
        g.drawImage(img,0,30,this);
        if(yPos>=30)g.fillOval(xPos, yPos, 45, 45);
    }

  • 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?

Maybe you are looking for