0

我正在尝试构建一个自定义三角形组件,它具有与 JComponent 相同的功能(例如每个 JButton)。

该程序的 porpoise 将在鼠标单击时在鼠标所在的位置添加三角形,并通过突出显示形状的 bg 来处理鼠标悬停事件。

我让默认布局(或 null),因为在使用其他布局时,应用程序不会将三角形放置在我想要的位置......

现在我的主要问题是如何调整与表格大小成正比的三角形的大小?因此,如果我将框架尺寸减小 50%,所有组件也会降低该值。

另一个问题是 JComponent 需要一个矩形区域来处理事件,因为我已经看到没有办法解决这个问题,所以如果我尝试单击受影响的区域,它将忽略它,而不是在那里创建一个新的三角形。

还有一个问题是,有时当从底部移出三角形时,它仍然是绿色的。

谢谢!

这是SSCCE:

// TriangleCustom.java
package TriangleCustom;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TriangleCustom {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame f = new JFrame("Triangle");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(1200, 800);
        Panel p = new Panel();
        f.add(p);
        f.setVisible(true);
    }

}

class Panel extends JPanel {
    // the offsets are the area (rect border) to contain the triangle shape
    private final int xOFFSET = 25;
    private final int yOFFSET = 50;

    ArrayList<TriangleShape> triangleAL = new ArrayList<TriangleShape>();

    public Panel() {
        setBounds(0, 0, 800, 400);
        // setBorder(BorderFactory.createLineBorder(Color.black,2));
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                addTriangle(new Point(e.getX(), e.getY()), new Point(e.getX()
                        - xOFFSET, e.getY() + yOFFSET), new Point(e.getX()
                                + xOFFSET, e.getY() + yOFFSET));
            }

        });
    }

    private void addTriangle(Point topCorner, Point leftCorner,
            Point rightCorner) {
        final TriangleDTO tdto = new TriangleDTO(new Point(25, 0), new Point(0,
                50), new Point(50, 50));
        TriangleShape ts = new TriangleShape(tdto);
        ts.setBorderColor(Color.BLACK);
        ts.setFillColor(Color.RED);
        ts.setBounds((int) (topCorner.getX() - 25), (int) topCorner.getY(), 51,
                51);
        triangleAL.add(ts);
        this.add(ts);
        repaint();

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.draw(new Rectangle2D.Double(0, 0, 799, 399));
    }
}

// the custom component in a shape of a triangle
class TriangleShape extends JComponent {

    private GeneralPath triangle = new GeneralPath();
    private TriangleDTO tdto = new TriangleDTO();
    private Color borderColor = new Color(0);
    private Color fillColor = new Color(0);

    // Constructor
    public TriangleShape(TriangleDTO tdto) {
        this.tdto = tdto;
        triangle.moveTo(tdto.getTopCorner().getX(), tdto.getTopCorner().getY());
        triangle.lineTo(tdto.getLeftCorner().getX(), tdto.getLeftCorner()
                .getY());
        triangle.lineTo(tdto.getRightCorner().getX(), tdto.getRightCorner()
                .getY());
        triangle.closePath();

        addMouseMotionListener(new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                // there are some issues when going out of the triangle from
                // bottom
                if (triangle.contains((Point2D) e.getPoint())) {
                    setFillColor(Color.GREEN);
                    repaint();
                } else {
                    setFillColor(Color.RED);
                    repaint();
                }
            }
        });

    }

    public void setBorderColor(Color borderColor) {
        this.borderColor = borderColor;
    }

    public void setFillColor(Color fillColor) {
        this.fillColor = fillColor;
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setPaint(fillColor);
        g2d.fill(triangle);
        g2d.setPaint(borderColor);
        g2d.draw(triangle);
    }

}

// just a plain DTO for the triangle points
class TriangleDTO {

    private Point topCorner = new Point();
    private Point leftCorner = new Point();
    private Point rightCorner = new Point();

    // Constructors
    public TriangleDTO() {
    }

    public TriangleDTO(Point topCorner, Point leftCorner, Point rightCorner) {
        super();
        this.topCorner = topCorner;
        this.leftCorner = leftCorner;
        this.rightCorner = rightCorner;
    }

    // Getters and Setters
    public Point getTopCorner() {
        return topCorner;
    }

    public void setTopCorner(Point topCorner) {
        this.topCorner = topCorner;
    }

    public Point getLeftCorner() {
        return leftCorner;
    }

    public void setLeftCorner(Point leftCorner) {
        this.leftCorner = leftCorner;
    }

    public Point getRightCorner() {
        return rightCorner;
    }

    public void setRightCorner(Point rightCorner) {
        this.rightCorner = rightCorner;
    }

}
4

0 回答 0