Problem with overlapping custom componets



Hi,

i'm trying to write a line component which can be dragged around and
which can have context menus. The code is


<code>
import java.awt.Shape;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Color;
import javax.swing.JLayeredPane;
import javax.swing.JComponent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Line2D.Double;
import java.awt.Cursor;


public class Line extends JComponent implements MouseListener,
MouseMotionListener {

JLayeredPane canvas;
BasicStroke stroke = new BasicStroke(10);
Line2D line;
boolean isSelected = false;
private Color color;
public Line(int init_x, int init_y, int last_x, int last_y,
JLayeredPane canvas, Color color) {
super();
this.color = color;
line = new Line2D.Double(init_x,init_y,last_x,last_y);
addMouseListener(this);
addMouseMotionListener(this);
this.canvas = canvas;
this.setOpaque(false);
this.setRequestFocusEnabled(true);
canvas.add(this);
updateUI();
}

protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D)g;
if(this.isSelected)
g2D.setColor(Color.BLACK);
else
g2D.setColor(color);
g2D.setStroke(new BasicStroke(5.0f));
g2D.draw(this.line);
}


public void mouseClicked(MouseEvent arg0) {
if(isContain(arg0.getPoint()));
canvas.repaint();
}


public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent m) {
}
public void mouseReleased(MouseEvent arg0) {
}
public void mouseDragged(MouseEvent m) {
}

public void mouseMoved(MouseEvent e) {
this.isContain(e.getPoint());
repaint();
if(isSelected){
Cursor c = new Cursor(Cursor.MOVE_CURSOR);
setCursor(c);
}else{
Cursor c = new Cursor(Cursor.DEFAULT_CURSOR);
setCursor(c);
}
}


/* Checks whether a Point cuts the Line */
public boolean isContain(Point point) {
Shape outline = stroke.createStrokedShape(line);
if (outline.contains(point)) {
isSelected = true;
this.canvas.moveToFront(this);
return true;
} else
this.canvas.moveToBack(this);
isSelected = false;
return false;
}
}


<code>

And following is a test program
<code>

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.GroupLayout;

public class Test extends JFrame {

public Test(){

GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}

public static void main(String args[]) {
Test test = new Test();
test.showLines();
test.setSize(400,300);
test.setVisible(true);

}

private void showLines(){
JLayeredPane panel = new JLayeredPane();

Line line1 = new Line(0,0,75,75,panel, Color.ORANGE);
line1.setBounds(new Rectangle(10,10,75,75));
line1.setBorder(new LineBorder(Color.GREEN));
add(line1);

repaint();

Line line2 = new Line(0,0,90,90,panel,Color.BLUE);
line2.setBounds(new Rectangle(20,50,90,90));
line2.setBorder(new LineBorder(Color.RED));
add(line2);

repaint();
}

}

</code>

When the program is run, two lines are drawn. First one, Orange in
color and has a green border. Sencond line is a blue one with red
border. the borders are opverlapping.

My issue is that when i move mouse pointer over the part of the blue
line which is overlapping with the green border, the events are not
getting fired.

Kindly advice on how to overcome this problem

Thanks in advance

Chanchal

.



Relevant Pages

  • How to unit test a Servlet
    ... The servlet to test... ... public void testDoGetSaysHello() throws Exception { ... public boolean containsHeader(String arg0) { ... public String encodeRedirectURL{ ...
    (comp.lang.java.programmer)
  • Re: Resizing JTextArea issue
    ... > because there is no room for growing, unless you make the top-level JFrame ... >> public void actionPerformed(ActionEvent arg0) { ... How is getWindowAncesterdiffererent from getParent()? ...
    (comp.lang.java.gui)
  • events of combobox
    ... public class TestForm extends JFrame ... implements PopupMenuListener, ActionListener, MouseListener, AncestorListener, MouseMotionListener { ... public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) { ...
    (comp.lang.java.programmer)
  • Re: JScrollPane doesnt resize properly
    ... send you also a data file which I'm not allowed to. ... public class PanelScroller extends JPanel implements MouseListener { ... public void mouseClicked{ ... public void mousePressed(MouseEvent arg0) ...
    (comp.lang.java.help)
  • closing a specific JFrame of several
    ... I have a JFrame that, when I click "new" in the menu bar, creates ... public void windowOpened(WindowEvent arg0) ... I'd just like to close only that specific window in which the user ...
    (comp.lang.java.gui)