问题描述
我有一个名为 Shape 的类,它继承自JPanel。
一些子类反过来又扩展了 Shape 类,每种形状都有一个。
每个形状都有自己的覆盖 paint()方法,绘制相应的形状。
我想要点击任何形状,我现在试图实现这个逻辑。请注意,每个形状都已添加到arrayList中。
但是,即使我已经清楚地点击了形状,contains语句总是返回false。
任何想法?
不要覆盖 paint() JPanel 而不是 paintComponent(..)
我不太确定我明白了,但我做了一个很简单的例子,我希望会有所帮助。
基本上它是一个简单的 JFrame 与一个 DrawingPanel (我自己的类扩展 JPanel ,并绘制形状)。此面板将创建形状(仅2个测试)将它们添加到 ArrayList 中,并将它们绘制到 JPanel code> paintComponent(..)和一个为循环,它还有一个 MouseAdapter 检查 JPanel 上的用户 mouseClicked(..) evnets。当点击时,我们遍历 ArrayList 中的每个 Shape ,然后检查 Shape 包含该点,如果是,则打印其类名,并使用 实例来检查 Shape 被点击并打印相应的消息:
输出(点击两个形状后):
ShapeClicker.java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ShapeClicker {
public ShapeClicker(){
JFrame frame = new JFrame();
frame.setTitle(Shape Clicker);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
initComponents(frame);
frame.pack();
frame.setVisible(true);
}
public static void main(String [] args){
//在EDT上创建框架和组件
SwingUtilities.invokeLater(new Runnable (){
@Override
public void run(){
new ShapeClicker();
}
});
}
private void initComponents(JFrame frame){
frame.add(new ShapePanel());
}
}
//定制面板
class ShapePanel扩展JPanel {
private Shape rect = new Rectangle2D.Double(50, 100,200,100);
private Shape cirlce = new Ellipse2D.Double(260,100,100,100);
private Dimension dim = new Dimension(450,300);
private final ArrayList< Shape>形状;
public ShapePanel(){
shapes = new ArrayList<>();
shapes.add(rect);
shapes.add(cirlce);
addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent me){
super.mouseClicked(me);
for(Shape s:shapes ){
if(s.contains(me.getPoint())){//检查鼠标是否在形状内点击
//我们可以打印出对象类名称
System.out.println(clicked a+ s.getClass()。getName());
//或检查使用实例处理的形状类如果
if(s instanceof Rectangle2D){
System.out.println(Clicked a rectangle);
} else if(s instanceof Ellipse2D){
系统.out.println(点击圈子);
}
}
}
}
});
}
@Override
protected void paintComponent(Graphics grphcs){
super.paintComponent(grphcs);
Graphics2D g2d =(Graphics2D)grphcs; (形状s:形状)
{
g2d.draw(s);
}
}
@Override
public Dimension getPreferredSize(){
return dim;
}
}
I've got a class called Shape which inherits from JPanel.
A number of sub-classes in turn extend the Shape classes, one for each type of shape.
Each shape has its own overriden paint() method, which draws the respective shape.
I would like to be able to click on any shape, and am trying to implement this logic for now. Please note that each shape has been added to an arrayList.
However, the contains statement always returns false, even when I have clearly clicked inside the shape.
Any ideas?
Never override paint() in JPanel rather paintComponent(..)
Im not quite sure I understand however I made a short example which I hope will help.Basically it is a simple JFrame with a DrawingPanel (my own class which extends JPanel and the shapes are drawn on). This panel will create shapes (only 2 for testing) add them to an ArrayList and draw them to the JPanel via paintComponent(..) and a for loop, it also has a MouseAdapter to check for user mouseClicked(..) evnets on the JPanel. When a click is made we iterate through each Shape in the ArrayList and check whether the Shape contains the point or not, and if so prints its class name and uses instance of to check what type of Shape is clicked and prints appropriate message:
Output (after clicking both shapes):
ShapeClicker.java:
import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class ShapeClicker { public ShapeClicker() { JFrame frame = new JFrame(); frame.setTitle("Shape Clicker"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); initComponents(frame); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //create frame and components on EDT SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new ShapeClicker(); } }); } private void initComponents(JFrame frame) { frame.add(new ShapePanel()); } } //custom panel class ShapePanel extends JPanel { private Shape rect = new Rectangle2D.Double(50, 100, 200, 100); private Shape cirlce = new Ellipse2D.Double(260, 100, 100, 100); private Dimension dim = new Dimension(450, 300); private final ArrayList<Shape> shapes; public ShapePanel() { shapes = new ArrayList<>(); shapes.add(rect); shapes.add(cirlce); addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent me) { super.mouseClicked(me); for (Shape s : shapes) { if (s.contains(me.getPoint())) {//check if mouse is clicked within shape //we can either just print out the object class name System.out.println("Clicked a "+s.getClass().getName()); //or check the shape class we are dealing with using instance of with nested if if (s instanceof Rectangle2D) { System.out.println("Clicked a rectangle"); } else if (s instanceof Ellipse2D) { System.out.println("Clicked a circle"); } } } } }); } @Override protected void paintComponent(Graphics grphcs) { super.paintComponent(grphcs); Graphics2D g2d = (Graphics2D) grphcs; for (Shape s : shapes) { g2d.draw(s); } } @Override public Dimension getPreferredSize() { return dim; } }
这篇关于点击绘制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!