我有一个允许用户在JPanel中释放绘制形状的程序,绘制的形状存储在一般类型的数组列表中(一个类是形状类型扩展的类)。 不过,我需要允许用户与形状交互。 阅读:事件处理程序。 任何想法如何工作那一个?
目前我如何绘制自己的形状的代码,尽管它是无关的:
import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.GeneralPath; import java.util.ArrayList; import java.util.LinkedList; import javax.swing.JFrame; import javax.swing.JPanel; public class DrawPanel extends JPanel { private ArrayList <shape> shapes; public enum ShapeType{LINE,OVAL}; public ShapeType shapeType; public shape currentShape; public Color currentColor=Color.BLACK; public DrawPanel(){ shapes= new ArrayList<shape>(); currentShape=null; setBackground(Color.WHITE); MouseHandler mouseHandler = new MouseHandler(); addMouseListener(mouseHandler); addMouseMotionListener(mouseHandler); } @Override public void paintComponent(Graphics g){ super.paintComponent(g); for(int i=0;i<shapes.size();i++) { shapes.get(i).draw(g); } if(currentShape!=null) { currentShape.draw(g); } } public void clearDraw(){ shapes.clear(); repaint(); } public void setToOval(){ setShapeType(ShapeType.OVAL); } public void setToLine(){ setShapeType(ShapeType.LINE); } public void setShapeType(ShapeType shape){ shapeType=shape; } private class MouseHandler extends MouseAdapter implements MouseMotionListener{ public void mousePressed(MouseEvent event){ if(shapeType!=null){ switch(shapeType){ case LINE: currentShape = new line(event.getX(),event.getX(),event.getY(),event.getY(),currentColor); break; case OVAL:{ currentShape=new oval(event.getX(),event.getX(),event.getY(),event.getY(),currentColor); } break; } } } public void mouseReleased(MouseEvent event){ if(currentShape!=null) { currentShape.setMyColor(currentColor); currentShape.setX2(event.getX()); currentShape.setY2(event.getY()); shapes.add(currentShape); currentShape=null; validate(); repaint(); } } public void mouseDragged(MouseEvent event){ if(currentShape!=null) { currentShape.setMyColor(currentColor); currentShape.setX2(event.getX()); currentShape.setY2(event.getY()); validate(); repaint(); } } } public void setCurrentColor(Color color){ this.currentColor=color; } }I have a program that allows the user to free draw shapes inside a JPanel, the drawn shapes are stored in an array-list of a general type (a class that is the class the shape types extend). However I need to allow the user to interact with the shapes. Read: event handlers. Any idea how to work that one?
Current code of how I draw my shapes, although it's unrelated:
import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.GeneralPath; import java.util.ArrayList; import java.util.LinkedList; import javax.swing.JFrame; import javax.swing.JPanel; public class DrawPanel extends JPanel { private ArrayList <shape> shapes; public enum ShapeType{LINE,OVAL}; public ShapeType shapeType; public shape currentShape; public Color currentColor=Color.BLACK; public DrawPanel(){ shapes= new ArrayList<shape>(); currentShape=null; setBackground(Color.WHITE); MouseHandler mouseHandler = new MouseHandler(); addMouseListener(mouseHandler); addMouseMotionListener(mouseHandler); } @Override public void paintComponent(Graphics g){ super.paintComponent(g); for(int i=0;i<shapes.size();i++) { shapes.get(i).draw(g); } if(currentShape!=null) { currentShape.draw(g); } } public void clearDraw(){ shapes.clear(); repaint(); } public void setToOval(){ setShapeType(ShapeType.OVAL); } public void setToLine(){ setShapeType(ShapeType.LINE); } public void setShapeType(ShapeType shape){ shapeType=shape; } private class MouseHandler extends MouseAdapter implements MouseMotionListener{ public void mousePressed(MouseEvent event){ if(shapeType!=null){ switch(shapeType){ case LINE: currentShape = new line(event.getX(),event.getX(),event.getY(),event.getY(),currentColor); break; case OVAL:{ currentShape=new oval(event.getX(),event.getX(),event.getY(),event.getY(),currentColor); } break; } } } public void mouseReleased(MouseEvent event){ if(currentShape!=null) { currentShape.setMyColor(currentColor); currentShape.setX2(event.getX()); currentShape.setY2(event.getY()); shapes.add(currentShape); currentShape=null; validate(); repaint(); } } public void mouseDragged(MouseEvent event){ if(currentShape!=null) { currentShape.setMyColor(currentColor); currentShape.setX2(event.getX()); currentShape.setY2(event.getY()); validate(); repaint(); } } } public void setCurrentColor(Color color){ this.currentColor=color; } }最满意答案
形状是您在表面上绘制的元素。 你不能直接处理它们上的事件。 一个选项是在你的JPanel中处理鼠标事件,并遍历你绘制的形状列表。 您可以检查一个形状是否位于当前鼠标位置,并根据该结果将currentShape变量设置为此形状(或将currentShape设置为NULL,并考虑可能在其他处理程序中未选择任何形状...)
Shapes are elements which you just draw on your surface. You cannot directly handle events on them. An option is to handle mouse events in your JPanel, and iterate through your list of painted shapes. You can check if a shape is located at your current mouse location, and based on that result, set your currentShape variable to this shape (or set currentShape to NULL and consider that probably NO shape is selected in your other handlers...)
更多推荐
发布评论