Problem with overlapping custom componets
- From: Chanchal <chanchal.jacob@xxxxxxxxx>
- Date: Wed, 14 Nov 2007 03:30:08 -0800
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
.
- Prev by Date: Re: Coping with slow construction of GUI elements
- Next by Date: Re: Problem with overlapping custom componets
- Previous by thread: Coping with slow construction of GUI elements
- Next by thread: Re: Problem with overlapping custom componets
- Index(es):
Relevant Pages
|