在JPanel中处理生成的形状(Handling an generated shapes in a JPanel)

编程入门 行业动态 更新时间:2024-10-28 00:14:04
在JPanel中处理生成的形状(Handling an generated shapes in a JPanel)

我有一个允许用户在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...)

更多推荐

本文发布于:2023-04-28 01:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:形状   JPanel   Handling   shapes   generated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!