samedi 28 février 2015

Drawing on a Jframe from two different classes



I am trying to create a program that record mouse clicks, draw lines between those points and after some calculations display some circles through a button call. My problem is that I can display the lines or the circles, but not both.


I know there is something overlapping something else, but I am very new to Java and I don't know how to fix it. Here is the code:



package fempack;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import java.lang.Math;



public class MainFem extends JPanel {



final DrawPoints Npoints = new DrawPoints();
final DrawLines Nline = new DrawLines();


private static final long serialVersionUID = 1L;
private MouseHandler mouseHandler = new MouseHandler();
private Point p1 = new Point(100, 100);
public int Xpoint[][] = new int[500][30];
public int Ypoint[][] = new int[500][30];
private double Xmpoint[][] = new double[500][1000]; // [i γραμμή][συντεταγμένη Χ]
private double Ympoint[][] = new double[500][1000];

private double Vec[][][] = new double[500][2][500]; // [i γραμμή][0,1][0α 1β]


private double dist[] = new double[10000];
private boolean drawing;
private int c1;
private int c2;

public MainFem() {

this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);

}


// -------------- Draw by clicking -----------------------

private class MouseHandler extends MouseAdapter {

@Override
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e)){
drawing=false;
c2++;

}
if(SwingUtilities.isLeftMouseButton(e)){

p1 = e.getPoint();
Xpoint[c1][c2] = p1.x;
Ypoint[c1][c2] = p1.y;




if (c1 > 3) {

for (int j = 0; j<c2+1; j++){
for (int i = 0; i<c1; i++){


if ((Math.abs(Xpoint[i][j]-Xpoint[c1][c2]) < 10) && (Math.abs(Ypoint[i][j]-Ypoint[c1][c2]) < 10)) {


Xpoint[c1][c2] = Xpoint[i][j];
Ypoint[c1][c2] = Ypoint[i][j];
System.out.println(Xpoint[i][j]);

}
}
}
}

if (drawing == true){
Nline.addLine(Xpoint[c1][c2], Ypoint[c1][c2], Xpoint[c1-1][c2], Ypoint[c1-1][c2]);
}



c1++;
drawing = true;

}
}


}


// ---------------- Create Mesh Points --------------------------

public void createmesh() {
int mdi = 0;


for (int j = 0; j<=c2; j++){
for (int i = 0; i<c1-1; i++){

// Υπολογισμός a και b συνιστωσών της εξίσωσης της γραμμής


Vec[i][0][mdi] = (Ypoint[i+1][j] - Ypoint[i][j])/(Xpoint[i+1][j] - Xpoint[i][j]);
Vec[i][1][mdi] = Ypoint[i][j] - Xpoint[i][j]*Vec[i][1][mdi];


// Υπολογισμός μέτρου διανύσματος

dist[mdi] = Math.sqrt(Math.pow(Xpoint[i][j] - Xpoint[i+1][j], 2) + Math.pow(Ypoint[i][j] - Ypoint[i+1][j], 2) );



// Υπολογισμός ενδιάμεσον σημείων

int nkom = 3;
double xa = Xpoint[i][j];
double ya = Ypoint[i][j];

for (int ii = 0; ii <nkom; ii++) {
double a = Vec[i][0][mdi];
double b = Vec[i][1][mdi];

Xmpoint[i][ii] = (-((2*a)*(b - ya) - 2*xa) + Math.sqrt(Math.abs(Math.pow(((2*a)*(b - ya) - 2*xa), 2) - 4*(1 + a*a)*(xa*xa + Math.pow((b - ya),2) - Math.pow(dist[mdi]/nkom,2)))))/(2 + 2*a*a);


Ympoint[i][ii] = a*Xmpoint[i][ii] + b;


double xm11 = Xmpoint[i][ii];
double ym11 = Ympoint[i][ii];
int xm1 = (int) xm11;
int ym1 = (int) ym11;

Npoints.addPoint(xm1, ym1);


System.out.println("i:" + ym11 + "...ii:" + ym1 );

xa = Xmpoint[i][ii];
ya = Ympoint[i][ii];
}





mdi++;


}
}
}



//------------------------- Display ---------------------------





private void display() {
JFrame f = new JFrame("LinePanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setPreferredSize(new Dimension(500, 600));

f.setLocationRelativeTo(null);




f.add(Npoints);
f.add(Nline);





JPanel buttonsPanel = new JPanel();



//-----------------Complete---------------

JButton dcomp = new JButton("Complete");
buttonsPanel.add(dcomp);


dcomp.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

createmesh();

}
});


//------------------Clean-------------------

JButton clearButton = new JButton("Clear");
buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
buttonsPanel.setPreferredSize(new Dimension(500, 100));
f.add(buttonsPanel, BorderLayout.SOUTH);
buttonsPanel.add(clearButton);


clearButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Nline.clearLines();
}
});
f.pack();
f.setVisible(true);
f.add(this);

}




//---------------------------------------------------------

public static void main(String[] args) {



EventQueue.invokeLater(new Runnable() {

@Override
public void run() {

new MainFem().display();


}
});
}
}


Class to draw lines:



package fempack;


import java.awt.Graphics;
import java.util.LinkedList;

import javax.swing.JPanel;


public class DrawLines extends JPanel {


/**
*
*/
private static final long serialVersionUID = 1L;

public DrawLines() {

}

private static class Line{
final int x1;
final int y1;
final int x2;
final int y2;


public Line(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;

}



}

private final LinkedList<Line> lines = new LinkedList<Line>();



public void addLine(int x1, int x2, int x3, int x4) {
lines.add(new Line(x1,x2,x3,x4));

repaint();
}



public void clearLines() {
lines.clear();
repaint();

}


@Override
protected void paintComponent(Graphics g) {

super.paintComponent(g);

for (Line line : lines) {
// System.out.println(line);

g.drawLine(line.x1, line.y1, line.x2, line.y2);
}



}

}


And class to draw Circles:



package fempack;

import java.awt.Graphics;

import javax.swing.JPanel;

import java.util.LinkedList;


public class DrawPoints extends JPanel {


private static final long serialVersionUID = 1L;

public DrawPoints() {

}




private static class Point {
final int x;
final int y;


public Point(int x, int y) {
this.x = x;
this.y = y;
}
}

private final LinkedList<Point> points = new LinkedList<Point>();


public void addPoint(int x, int y) {
points.add(new Point(x,y));
// System.out.println(x);
repaint();
}




@Override
protected void paintComponent(Graphics g) {

super.paintComponent(g);

for (Point point : points) {
// System.out.println(point);

g.drawOval(point.x, point.y, 5, 5);

}
}

}



Aucun commentaire:

Enregistrer un commentaire