按钮的MouseEvent与单击UI的点不同

按钮MouseEvent中指定的点与单击UI的点不同。

这是“绘图面板程序”。如果用户单击面板,则将绘制正方形点。 如果有两个点,将画出与两个点相交的线。关闭多边形按钮将连接多边形的第一个点和最后一个点,然后将该多边形分离到另一个对象Polyline中。

我要使顶点可以通过用户的鼠标拖动来校正。因此,我在线条的顶点处单击按钮以选择顶点作为对象。很好,但是问题是我期望的位置是我单击的位置。我希望在拖动鼠标后绘制填充的矩形,但是当我拖动按钮时,正方形开始在顶部的左侧绘制。我不知道原因

package stu;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

public class MyDrawPanel extends JPanel {
    private Polyline    mPolyline;
    private Point point;
    public void setPolyLine(Polyline pline) {

        mPolyline = pline;

    }

    public void setPoint(Point p)

    {
        point = p;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.gray);
        g.fillRect(0,this.getWidth(),this.getHeight());
        g.setColor(Color.black);


        Graphics2D  g2 = (Graphics2D)g;
        g2.setStroke(new BasicStroke(10));




        this.addmouseListener(mPolyline);





    }
}


package stu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.JButton;

public class PolylineEditor  {

    Polyline polyline;
    MyDrawPanel mypanel;
    PolygonList polylist;
    DefaultListModel listModel;

    int count;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PolylineEditor gui = new PolylineEditor();
        gui.go();
    }

    public void makePanel()
    {
        mypanel = new MyDrawPanel();
    }
    public void makeNewPolyline()
    {

        polylist.add(polyline);
        polylist.setName("Polygon"+ count);


    }
    public void go() {

        //frame 
        JFrame frame = new JFrame("Polyline Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //button 
        JButton button1 = new JButton ("clear");

        JButton button2 = new JButton ("Close Operation");

        JButton button3 = new JButton("save");

        JButton button4 = new JButton("Checking");
        //panel and  polyline
        makePanel();

        polyline = new Polyline();
        mypanel.setPolyLine(polyline);


        //PolygonList
         polylist = new PolygonList();


         //JList


        listModel = new DefaultListModel();


        JList list = new JList();
        list = new JList(listModel);

        JScrollPane scroll = new JScrollPane(list);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        frame.getcontentPane().add(scroll,BorderLayout.EAST);
        frame.getcontentPane().add(mypanel,BorderLayout.CENTER);



        //container and  button
        Container c = new Container();
        c.setLayout(new FlowLayout());


        frame.getcontentPane().add(BorderLayout.SOUTH,c);
        c.add(button1);
        c.add(button2);
        c.add(button3);
        c.add(button4);


          //button for clear

        button1.addactionListener(new actionListener()
        {
            public void actionPerformed(actionEvent event)
            {

                polyline.clear();
                mypanel.repaint();




            }
        });





        //button for closing Polygon
        button2.addactionListener(new actionListener()
        {
            public void actionPerformed(actionEvent event)
            {
                try {

//this count variable is used to make the name of the polygon object.
                    count++;
 /* connects first and the last point,then separates from other polygons then the polygon is added into
PolygonList*/          
                polyline.ClosingPolygon();


                makeNewPolyline();
// make the listmodel to hold polygon object
// but I made to add String ... after now I will connect string to obejct.
                listModel.addElement(polylist.getName(polylist.size()-1));

// clear polyline's points
                polyline.clear();


                System.out.println("polygon is seperated");



                }

                catch(java.lang.IndexOutOfBoundsException e)
                {
                    System.out.println("There is no polygon to close");
                }




            }

        });

        // ...
        // ...

// button for save
        button3.addactionListener(new actionListener()
        {
            public void actionPerformed(actionEvent event)
            {
                try {
                    polyline.MakeSerial();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });





        BorderLayout    layout = (BorderLayout)frame.getcontentPane().getLayout();
        layout.setHgap(10);
        layout.setVgap(10);

        frame.setSize(800,800);
        frame.setVisible(true);

    }





}

package stu;

import java.util.*;

import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
public class PolygonList  {

    private ArrayList<Polyline> PolygonList;

    // Object to hold polyline in the ArrayList
    private ArrayList<String> Name;


    PolygonList()
    {
        PolygonList = new ArrayList<Polyline>();
        Name = new ArrayList<String>();
    }

    public void setName(String n)
    {
        Name.add(n);
    }

    public String getName(int index)
    {
        return Name.get(index);
    }
    public void add(Polyline polyline)
    {


        PolygonList.add(polyline);


    }

    public Polyline get(int num)
    {
        return PolygonList.get(num);
    }

    public void remove(int num)
    {
        PolygonList.remove(num);
    }

    public int size()
    {
        return PolygonList.size();
    }
}

package stu;

//Point included in Polyline. The point contains x,y coordinates.

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
public class Pointimplements Serializable {
    private double  posX,posY;
    public PolygonList list;
    public Point(double x,double y) {
        posX = x; posY = y;
    }
    public double getX() { return posX; }
    public double getY() { return posY; }
    public void setX(double x) { posX = x; }
    public void setY(double y) { posY = y; }


}


// Listener for Panel to draw lines,points and make polygons.
package stu;

import java.awt.*;
import java.awt.Color;

import java.awt.Graphics;
import java.awt.event.actionEvent;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.io.*;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Polyline extends MouseAdapter implements Serializable {
    private String name;
    private ArrayList<Point> mPts = new ArrayList<Point>();
    private JPanel panel;

    private Graphics g;

    public PolygonList list;

    //set name for close polygon object
    public void setName(String n,int index)
    {
        name = n + Integer.toString(index);
        System.out.print(name);
    }
    public void clear() {

        mPts.removeAll(mPts);





        /*Clear all data in the ArrayList*/
    }

    public void ClosingPolygon()
    {
//connect first and last points
        int posX1 = (int)mPts.get(0).getX();
        int posY1 = (int)mPts.get(0).getY();
        int posX2 = (int)mPts.get(mPts.size()-1).getX();
        int posY2 = (int)mPts.get(mPts.size()-1).getY();
        g.drawLine(posX1,posY1,posX2,posY2 ); 
    }

    public int getNumPts() {

        return mPts.size();
    }

    public Point getPoint(int i) {
        return mPts.get(i);
    }

    @Override
    public void mousepressed(MouseEvent e) {
// if mouse clicked on panel.
        Point p = new Point(e.getX(),e.getY());

        int px = e.getX();
        int py = e.getY();
        mPts.add(p);


        ((MyDrawPanel)(e.getsource())).setPolyLine(this);
        // set polyline to panel.

         panel = ((MyDrawPanel)(e.getsource()));
         g = panel.getGraphics();


        g.fillRect(px,py,10,10);
        //draw vertex at touched point
        JButton Ankle = new JButton();
//Ankle means vertex
        ((MyDrawPanel)(e.getsource())).setLayout(null);
        ((MyDrawPanel)(e.getsource())).add(Ankle);
        Ankle.setbounds(px,10);
// make ankle at touched points to acces the vertex
        Ankle.addmouseMotionListener(new MouseAdapter()
        {
//when user dragged the button,the points should be moved where the user dragged
            public void mouseDragged(MouseEvent event)
            {




            int newX = event.getX();
            int newY= event.getY();

//draw rectangle to show where 

//make sure the points moved by the cursor to be shown as naturally
            g.setColor(Color.black);
            int newX = event.getX();
            int newY= event.getY();


            p.setX(event.getX());
            p.setY(event.getY());
            g.fillRect(event.getX(),event.getY(),10);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            g.setColor(Color.GRAY);
            p.setX(event.getX());
            p.setY(event.getY());
            g.fillRect(event.getX(),10);





            }
        });


//Draws Line only if when the points are more than two.
        if(mPts.size() >1)
        g.drawLine((int)getPoint(mPts.size()-2).getX(),(int)getPoint(mPts.size()-2).getY(),(int)getPoint(mPts.size()-1).getX(),(int)getPoint(mPts.size()-1).getY());

        /*if using for loop,the line will be overlaid*/
    }









    public void MakeSerial () throws IOException
    {
        int num = 1;
        try {
            FileOutputStream fileStream = new FileOutputStream("Polyline" + num);
            ObjectOutputStream os = new ObjectOutputStream(fileStream);

            os.writeObject(mPts);

            os.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

程序中存在一些问题,例如剩余按钮,JList,保存 但是绘制矩形最难解决。.

有什么想法吗?另一种方法也可以

bonaxi 回答:按钮的MouseEvent与单击UI的点不同

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3010359.html

大家都在问