Results 1 to 9 of 9
  1. #1
    smitharavi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default How to get the focus of other Panel

    Hi All,

    I have 2 panels with 2 images on it.I am drawing rectangle on 1 image for marking purpose,same marking(rectangle) should appear on another image also.
    Following is the program for mouse activity.Problem here is if i draw rect on one image,it is drawing on other,but rect appears on 2nd panel only if i click on image.
    Can any one help how to write rect simultaneously on both image.

    //Mouse and MouseMotion Activity Start
    private int mousex = 0;
    private int mousey = 0;
    private int Orx = 0;
    private int Ory = 0;
    private int OrWidth = 0;
    private int OrHeight = 0;
    private int drawX = 0;
    private int drawY = 0;
    private boolean initialRect = true;
    public void rectOperation(MouseEvent e){
    Graphics2D drawOnImage1 = (Graphics2D)firstImage.getGraphics();
    Graphics2D drawOnImage2 = (Graphics2D)secImage.getGraphics();
    if(drawOnImage1!=null)
    {
    drawOnImage1.setStroke(new BasicStroke(lineThickness));
    drawOnImage2.setStroke(new BasicStroke(lineThickness));
    }

    if(newColor!=null)
    {
    drawOnImage1.setColor(newColor);
    drawOnImage2.setColor(newColor);
    }
    if (initialRect)
    {
    setGraphicalDefaults(e);
    initialRect = false;
    }

    if (mouseHasMoved(e))
    {
    if(rectOrOval)
    {
    drawOnImage1.setXORMode(Color.white);
    drawOnImage1.drawRect(drawX, drawY, OrWidth, OrHeight);
    //e.setSource(secImage);
    MainPanel.toSetFocus();
    drawOnImage2.setXORMode(Color.white);
    drawOnImage2.drawRect(drawX, drawY, OrWidth, OrHeight);
    /* Update new mouse coordinates */
    mousex = e.getX();
    mousey = e.getY();

    /* Check new mouse coordinates for negative errors */
    setActualBoundry();

    /* Draw rectangle shadow */
    drawOnImage1.drawRect(drawX,drawY,OrWidth,OrHeight );
    // e.setSource(secImage);
    MainPanel.toSetFocus();
    drawOnImage2.drawRect(drawX,drawY,OrWidth,OrHeight );

    }
    else
    {
    drawOnImage1.setXORMode(Color.white);
    drawOnImage1.drawOval(drawX, drawY, OrWidth, OrHeight);
    e.setSource(secImage);
    drawOnImage2.setXORMode(Color.white);
    drawOnImage2.drawOval(drawX, drawY, OrWidth, OrHeight);
    /* Update new mouse coordinates */
    mousex = e.getX();
    mousey = e.getY();

    /* Check new mouse coordinates for negative errors */
    setActualBoundry();
    /* Draw oval shadow */
    // e.setSource(drawOnImage2);
    drawOnImage1.drawOval(drawX, drawY, OrWidth, OrHeight);
    drawOnImage2.drawOval(drawX, drawY, OrWidth, OrHeight);

    }
    }
    repaint();

    }
    public boolean mouseHasMoved(MouseEvent e) {
    return (mousex != e.getX() || mousey != e.getY());
    }
    public void setActualBoundry()
    {
    /*
    If the any of the current mouse coordinates
    are smaller than the origin coordinates, meaning
    if drag occured in a negative manner, where either
    the x-shift occured from right and/or y-shift occured
    from bottom to top.
    */
    if (mousex < Orx || mousey < Ory)
    {
    /*
    if the current mouse x coordinate is smaller
    than the origin x coordinate,
    equate the drawX to be the difference between
    the current width and the origin x coordinate.
    */
    if (mousex < Orx)
    {
    OrWidth = Orx - mousex;
    drawX = Orx - OrWidth;
    } else
    {
    drawX = Orx;
    OrWidth = mousex - Orx;
    }
    /*
    if the current mouse y coordinate is smaller
    than the origin y coordinate,
    equate the drawY to be the difference between
    the current height and the origin y coordinate.
    */
    if (mousey < Ory)
    {
    OrHeight = Ory - mousey;
    drawY = Ory - OrHeight;
    } else
    {
    drawY = Ory;
    OrHeight = mousey - Ory;
    }
    }
    /*
    Else if drag was done in a positive manner meaning
    x-shift occured from left to right and or y-shift occured
    from top to bottom
    */
    else
    {
    drawX = Orx;
    drawY = Ory;
    OrWidth = mousex - Orx;
    OrHeight = mousey - Ory;
    }
    }
    /*
    Method sets all the drawing varibles to the default
    state which is the current position of the mouse cursor
    Also the height and width varibles are zeroed off.
    */
    public void setGraphicalDefaults(MouseEvent e) {
    mousex = e.getX();
    mousey = e.getY();
    Orx = e.getX();
    Ory = e.getY();
    drawX = e.getX();
    drawY = e.getY();
    OrWidth = 0;
    OrHeight = 0;
    }
    /*
    Method will be activated when mouse is being dragged.
    depending on what operation is the opstatus, the switch
    statement will call the relevent operation
    */
    public void mouseDragged(MouseEvent e) {
    System.out.println("In mousedragged");
    rectOperation(e);
    repaint();
    }
    public void mouseReleased(MouseEvent e) {
    System.out.println("in mouseReleased()");
    releasedRect(e);
    repaint();
    }
    public void mouseEntered(MouseEvent e) { }

    public void releasedRect(MouseEvent e) {
    System.out.println("in releasedRect()");
    Graphics2D drawOnImage1 = (Graphics2D)firstImage.getGraphics();
    Graphics2D drawOnImage2 = (Graphics2D)secImage.getGraphics();
    drawOnImage1.setStroke(new BasicStroke(lineThickness));
    drawOnImage2.setStroke(new BasicStroke(lineThickness));
    if(newColor!=null)
    {
    drawOnImage1.setColor(newColor);
    drawOnImage2.setColor(newColor);
    }
    initialRect = true;
    if(rectOrOval)
    {
    drawOnImage1.drawRect(drawX,drawY,OrWidth,OrHeight );
    //e.setSource(secImage);
    MainPanel.toSetFocus();
    drawOnImage2.drawRect(drawX,drawY,OrWidth,OrHeight );
    }
    else
    {
    drawOnImage1.drawOval(drawX,drawY,OrWidth,OrHeight );
    //e.setSource(getFocusListeners());
    MainPanel.toSetFocus();
    drawOnImage2.drawOval(drawX,drawY,OrWidth,OrHeight );
    }
    repaint();
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    even after some corrections i couldn't compile the code. example: where is newColor declared? please read the sscce and post your problem again.

  3. #3
    smitharavi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default I have 4 connected program,i can send all to run?

    Hi,

    I have 4 connected program, can I send all to run?

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by smitharavi View Post
    Hi,

    I have 4 connected program, can I send all to run?

    what do you mean with "connected program" and can you be more specific what you mean with "send all to run?". thanks.

  5. #5
    smitharavi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default Complete program to run

    Hi,

    Find an attachment of all 4 related program to run.

    Problems:

    1.I have 2 panels with 2 images on it.I am drawing rectangle on 1 image for marking purpose,same marking(rectangle) should appear on another image also.
    Following is the program for mouse activity.Problem here is if i draw rect on one image,it is drawing on other,but rect appears on 2nd panel only if i click on image.
    Can any one help how to write rect simultaneously on both image.

    2.After drawing Rectangle,on right click,i should get Textfield to enter user comments.
    Attached Files Attached Files
    Last edited by smitharavi; 12-19-2010 at 03:22 AM. Reason: to remove attachment &include SSCCE

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    ok, i could import and run your code. the question i have: suppose you have an image of 200x200px on the left panel and an other image of 100x100px on the right panel. now, if you paint a rectangle at position 150, 150 with width of 30 and height of 30 in the left image this rectangle will be placed outside in the right image. is this what you want?

  7. #7
    smitharavi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Kindly load same image on both panels

    suppose you have an image of 200x200px on the left panel and an other image of 200x200px on the right panel. now, if you paint a rectangle at any position of 30 width and 30 height on left panel,at same position it should appear on right panel also,(synchronisation).

    It should draw on both panels same size rectangle,at same position,and at same time.
    while drawing on left panel,i should be able to see drawing rectangle even on right panel.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Java Code:
    leftPanel = new JPanel() {
    
       @Override
       public void paintComponent(Graphics g) {
          // custom painting code
       }
    
       @Override
       public void repaint() {
          super.repaint();
          rightPanel.repaint();
       }
    }
    
    rightPanel = new JPanel() {
    
       @Override {
       public void paintComponent(Graphics g) {
          super.paintComponent(g); // not needed if both panels are identically the same size
          leftPanel.paintComponent(g);
       }
    }
    Typed here, may have typos.

    rightPanel will always show the content of leftPanel.

    db

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by smitharavi View Post
    Hi,

    Find an attachment of all 4 related program to run my tool.
    I've downloaded and reviewed your files, and I have to say, that's a h&#101;ll of a lot of code.


    Can you please help me as early as possible
    Most would be glad to, but to do so, we would have to simplify the problem to understand it, which means we would have to create our own SSCCE. So why are you asking us to quickly solve your problem and create our own SSCCE when you won't put in the effort to make one for us? I'll be glad to help you, and quickly, but I'll wait for your SSCCE first.

Similar Threads

  1. Add panel to parent panel
    By LovJava in forum AWT / Swing
    Replies: 10
    Last Post: 09-02-2010, 08:43 PM
  2. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 05:56 AM
  3. Window Focus
    By collin389 in forum New To Java
    Replies: 4
    Last Post: 12-29-2009, 03:03 AM
  4. AWT Focus Problem using idk 1.6.0
    By javakash in forum AWT / Swing
    Replies: 2
    Last Post: 09-05-2009, 02:00 AM
  5. Focus
    By Marty in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 02:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •