Creating ellips and connecting them
Hi here codes that draws on screen ellipses and connecting them together with lines..
Code:
package paint;
import javax.swing.JFrame;
/**
*
* @author Meko
*/
public class Main {
public Main() {
MainPanel selectablePanel = new MainPanel();
ShapeControl control = new ShapeControl(selectablePanel);
selectablePanel.addMouseListener(control);
selectablePanel.addMouseMotionListener(control);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(selectablePanel);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
public class MainPanel extends JPanel implements ActionListener {
private JButton XvalueButton = new JButton("Circle");
private JButton YvalueButton = new JButton("Draw");
private JButton ZvalueButton = new JButton("Z");
private ShapeControl shape = new ShapeControl(this);
public boolean circleSelected, drawlineSelected, clicked;
double LineCenter;
double a, b;
public MainPanel() {
JPanel buttonPanel=new JPanel();
circleSelected = false;
drawlineSelected = false;
XvalueButton.addActionListener(this);
YvalueButton.addActionListener(this);
ZvalueButton.addActionListener(this);
setLayout(new BorderLayout());
add(buttonPanel,BorderLayout.NORTH);
buttonPanel.add(XvalueButton);
buttonPanel.add(YvalueButton);
buttonPanel.add(ZvalueButton);
addMouseMotionListener(shape);
addMouseListener(shape);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == XvalueButton) {
circleSelected = true;
clicked = true;
for (int i = 0; i < shape.ellipsList.size(); i++) {
MyEllips circle = (MyEllips) shape.ellipsList.get(i);
circle.repaint();
}
}
if (e.getSource() == YvalueButton) {
drawlineSelected = true;
}
}
}
public class MyEllips extends JPanel {
Point c ;
int posX;
int posY;
int width =50;
int height =50;
public MyEllips(Point center) {
c = center;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int posX = c.x ;
int posY = c.y;
g.setColor(Color.BLUE);
g.fillRect(posX, posY, width, height);
g.setColor(Color.BLUE);
g.drawRect(posX, posY, width, height);
}
}
public class ShapeControl extends MouseInputAdapter {
MainPanel selectablePanel;
public boolean dragging, ChoosedBooth, firstClicked, secondClicked, lineSelected;
Point selectedPoint;
Line2D current_line;
MyEllips selectedEllips;
List lineList;
List ellipsList;
List pointList;
Point mainPoint, a, b;
public ShapeControl(MainPanel mp) {
pointList = new ArrayList();
ellipsList = new ArrayList();
lineList = new ArrayList();
selectablePanel = mp;
dragging = false;
firstClicked = false;
secondClicked = false;
}
public void initLine(Point a, Point b) {
lineList.add(new Line2D.Double(a, b));
pointList.add(a);
pointList.add(b);
}
public void initCircle(Point c) {
MyEllips myellips = new MyEllips(c);
// myellips.setPreferredSize(new Dimension(50,50));
myellips.setLayout(new BoxLayout(myellips,BoxLayout.X_AXIS));
myellips.setBackground(Color.red);
ellipsList.add(myellips);
selectablePanel.add(myellips);
myellips.repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
Point p = e.getPoint();
if (selectablePanel.circleSelected) {
if (selectablePanel.clicked) {
Point center = new Point(e.getX(), e.getY());
pointList.add(center);
initCircle(center);
selectablePanel.clicked = false;
}
}
if (selectablePanel.drawlineSelected) {
for (int j = 0; j < ellipsList.size(); j++) {
MyEllips circle = (MyEllips) ellipsList.get(j);
if (e.getClickCount() == 2) {
firstClicked = true;
if (firstClicked) {
Point point = (Point) pointList.get(j);
if (circle.contains(p)) {
a = point;
firstClicked = false;
secondClicked = true;
}
}
} else if (secondClicked) {
Point point = (Point) pointList.get(j);
if (circle.contains(p)) {
b = point;
firstClicked = true;
secondClicked = false;
initLine(a, b);
for (int i = 0; i < lineList.size(); i++) {
Line2D.Double line = (Line2D.Double) lineList.get(i);
current_line = line;
}
}
}
}
}
}
int downX, downY;
Point movingPoint = null;
public void mousePressed(MouseEvent e) {
for (int j = 0; j < pointList.size(); j++) {
Point point = (Point) pointList.get(j);
if (e.getX() <= point.x + 25 && e.getX()>= point.x - 25 && e.getY() <= point.y + 25 &&e.getY()>= point.y - 25) {
System.out.println("Found");
downX = e.getX();
downY = e.getY();
movingPoint = point;
}
}
}
public void mouseReleased(MouseEvent e) {
if (movingPoint != null) {
movingPoint.x += e.getX() - downX;
movingPoint.y += e.getY() - downY;
movingPoint = null;
}
selectablePanel.repaint();
}
}
My problem is It creates RedSquare and I can move it but When I resize screen IT draws all screnn red..It means MyEllips object is holl screen.And When I try to create new object with clickin button and screen It doenst show.I think it is behind of first created Jpanel ..What must I do?