从两个不同的类绘制Jframe

时间:2015-02-28 22:04:54

标签: java swing jframe jpanel

我正在尝试创建一个记录鼠标点击的程序,在这些点之间绘制线条,并在一些计算后通过按钮调用显示一些圆圈。我的问题是我可以显示线条或圆圈,但不能同时显示两者。

我知道有些东西重叠了,但我对Java很新,我不知道如何修复它。这是代码:

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();


        }
    });
}
}

绘制线条的类:

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);
    }



   }

   }

绘制圆圈的课程:

       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);

    }
   }

   }

1 个答案:

答案 0 :(得分:0)

这是我在createmesh()中添加点的解决方案。我没有修改你从鼠标位置得到的点的任何处理。我只是试图重构和清理你的代码。 现在DrawLine和DrawPoint不再是JPanel,它们只是保存你给它们的数据并绘制到你在public void draw(Graphics g)方法中传递给它们的Graphics-Object,它在DrawPanel-Class中的paint上调用。 DrawPanel继承自JPanel并覆盖void PaintComponent(Graphics g);来实现这一点(感谢MadProgrammer;)。 DrawPanel还使用MouseEvents将鼠标位置放入DrawLines-和DrawPoints-对象中。 MainWindow继承自JFrame并且是MainWindow。它创建所有GUI元素,并在必要时调用DrawPanel。

主窗口:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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


public class MainWindow extends JFrame {

    private static final long serialVersionUID = 6755417048009930291L;

// begin gui components
    JButton clearButton = null;
    JButton dcomp = null;
    JPanel buttonsPanel = null;
    DrawPanel drawPanel = null;
// end gui components


    public MainWindow(){

        // Add ButtonPanel
        add(getDrawPanel(), BorderLayout.CENTER);
        add(getButtonPanel(), BorderLayout.SOUTH);

        // Add Buttons
        getButtonPanel().add(getDcomp());
        getButtonPanel().add(getClearButton());

        addMouseListener(getDrawPanel());
    }


// begin getters and setters for gui components
    private DrawPanel getDrawPanel(){
        if(drawPanel == null){
            drawPanel = new DrawPanel();
            drawPanel.setVisible(true);
        }

        return drawPanel;
    }

    private JPanel getButtonPanel() {
        if(buttonsPanel == null){
            buttonsPanel = new JPanel();
            buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.black));
            buttonsPanel.setPreferredSize(new Dimension(500, 100));
        }

        return buttonsPanel;
    }

    private JButton getClearButton() {
        if(clearButton == null){
            clearButton = new JButton("Clear");
            clearButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    getDrawPanel().clearLines();
                }
            });
        }

        return clearButton;
    }

    private JButton getDcomp() {
        if(dcomp == null){
            dcomp = new JButton("Complete");
            dcomp.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    getDrawPanel().createmesh();
                    getDrawPanel().repaint();
                }
            });
        }
        return dcomp;
    }
// end begin getters and setters for gui components



    //as always program entry-point
    public static void main(String[] args) {
        MainWindow wnd = new MainWindow();

        wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        wnd.setPreferredSize(new Dimension(500, 600));
        wnd.setLocationRelativeTo(null);
        wnd.pack();
        wnd.setVisible(true);
    }

}

DrawPanel:

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DrawPanel extends JPanel implements MouseListener {

    private static final long serialVersionUID = -7726303639184194659L;

// begin members
    private DrawLines dlines;
    private DrawPoints dpoints;

    // dont know what these members do
    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;
// end members

    public DrawPanel() {
        dlines = new DrawLines();
        dpoints = new DrawPoints();

        addMouseListener(this);
    }

// begin class logic
    public void clearLines(){
        dlines.clearLines();
        repaint();
    }


    // dont know what this does
    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;

                    dpoints.addPoint(xm1, ym1);

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

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

                mdi++;

            }
        }
    }
// end class logic

// begin MouseListener implementation
    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @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) {
                dlines.addLine(Xpoint[c1][c2], Ypoint[c1][c2],
                        Xpoint[c1 - 1][c2], Ypoint[c1 - 1][c2]);
            }

            c1++;
            drawing = true;

            repaint();
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }
// end MouseListener implementation

// beging PAINTING
    @Override
    protected void paintComponent(Graphics g) {
        System.out.println("paintComponent");
        super.paintComponent(g);

        dlines.draw(g);
        dpoints.draw(g);
    }
// end PAINTING
}

DrawPoints:

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

public class DrawPoints {

    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));
    }

    public void draw(Graphics g){
        for (Point point : points) {
            g.drawOval(point.x, point.y, 5, 5);
        }
    }

}

DrawLines:

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

public class DrawLines{

    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));
    }

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

    public void draw(Graphics g){
        for (Line line : lines) {
            g.drawLine(line.x1, line.y1, line.x2, line.y2);
        }
    }
}

我觉得处理这样的事情是一个好主意。你想在Panel上绘制一些东西,然后将它传递给保存数据的对象,让它们绘制到它们的公共 canvas ,就像我在DrawPoints-和DrawLines中使用void draw(Graphics g)方法一样-class。

我希望这会对你有所帮助。 如果有什么不清楚,可以随意询问。