0

我正在一个项目中工作,我必须绘制包含箭头和节点的图形!我做了一个扩展JPanel箭头和节点的类,我做了一个JSplitPane 让菜单在左边,图表在右边!当我添加两个面板时,其中一个是可见的。

箭头面板类:

public class ArrowPanel extends JPanel{
int barb;
double phi;
double x1;
double y1;
double x2;
double y2;
double xcentre;
double ycentre;
double x3;
double y3;
double x4;
double y4;

public ArrowPanel(double x1,double y1,double x2, double y2 ,double x3,double y3 ,double x4 ,double y4){
    barb = 30;                   // barb length
    phi = Math.PI/6;             // 30 degrees barb angle
    this.x1=x1;
    this.y1=y1;
    this.x2=x2;
    this.y2=y2;
    this.x3=x3;
    this.y3=y3;
    this.x4=x4;
    this.y4=y4;
    this.setVisible(true);
    this.setOpaque(false)   ;     
        this.setLayout(new GridBagLayout());
        System.out.println(" c est marché");} protected void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    Graphics2D g3 = (Graphics2D)g;
    g2.setStroke(new BasicStroke(7));
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    g3.setStroke(new BasicStroke(7));
    g3.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    int w = getWidth();
    int h = getHeight();
    double theta, x, y;
    double a = 0;
        double b = 0;
        xcentre=(x1 + x2)/2 ;
        ycentre=(y1 + y2)/2 ;
        double z3 = x1;
        double k3 = y1;
        k3=a*z3+b;
        a=(y2-y1)/(x2-x1);
        b=y1-a*x1;
        System.out.println( "a="+ a);
    System.out.println( "b="+ b);
    double a1 = -1/a;
    double b1 = ycentre-a1*xcentre;


    g2.setPaint(Color.RED);
    g3.setPaint(Color.RED);
    g3.draw(new Line2D.Double(x3, y3, x4, y4));
    g2.draw(new Line2D.Double(x1, y1, x2, y2));
    theta = Math.atan2(y2 - y1, x2 - x1);
    double theta2=Math.atan2(y4 - y3, x4 - x3);
    drawArrow(g2, theta, x2, y2);
    drawArrow(g3, theta2, x4, y4);
    g2.setPaint(Color.GREEN);
    g2.draw(new Line2D.Double(xcentre - 5, a*(xcentre - 5)+ b, xcentre+5, a*(xcentre+5)+b));
    g2.drawString("rad1", getX(), getY());
    g2.draw(new Line2D.Double(xcentre - 7, a1*(xcentre - 7)+ b1, xcentre+7, a1*(xcentre+7)+b1));
    xcentre=(x3 + x4)/2 ;
    ycentre=(y3 + y4)/2 ;

         z3 = x3;
         k3 = y3;
        k3=a*z3+b;
        a=(y4-y3)/(x4-x3);
        b=y3-a*x3;
        System.out.println( "a="+ a);
    System.out.println( "b="+ b);
    a1 = -1/a;
    b1 = ycentre-a1*xcentre;
    g3.draw(new Line2D.Double(xcentre - 5, a*(xcentre - 5)+ b, xcentre+5, a*(xcentre+5)+b));
    g3.draw(new Line2D.Double(xcentre - 7, a1*(xcentre - 7)+ b1, xcentre+7, a1*(xcentre+7)+b1));
        System.out.println("bahamas");

}
private void drawArrow(Graphics2D g2, double theta, double x0, double y0)
{
    double x = x0 - barb * Math.cos(theta + phi);
    double y = y0 - barb * Math.sin(theta + phi);
    g2.draw(new Line2D.Double(x0, y0, x, y));
    x = x0 - barb * Math.cos(theta - phi);
    y = y0 - barb * Math.sin(theta - phi);
    g2.draw(new Line2D.Double(x0, y0, x, y));
}}`

这是元素类:

public class ElementMenu extends JPanel {private static final long serialVersionUID = 1L;private int largeur;
private Color couleur;
private Color couleurActif;
private Color couleurNormal;
private String texte;
private String Ident;
public ElementMenu(String texte,String Ident, Color couleurNormal, Color couleurActif, int taille) {
    this.couleur=couleurNormal;
    this.couleurNormal=couleurNormal;
    this.couleurActif=couleurActif;
    this.texte=texte;
    this.Ident=Ident ;
    largeur=taille;
    this.setSize(new Dimension(largeur, largeur ));
    this.setOpaque(false);
    this.setLayout(new BorderLayout());
    ajouterListener();
}
private void ajouterListener(){
    addMouseListener(new MouseListener() {public void mouseClicked(MouseEvent e) {}

        public void mouseReleased(MouseEvent e) {}

        public void mousePressed(MouseEvent e) {}

        public void mouseExited(MouseEvent e) {
            couleur=couleurNormal;
            repaint();
        }

        public void mouseEntered(MouseEvent e) {
            couleur=couleurActif;
            setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            repaint();
        }

    });}public void paint(Graphics arg0) {
    super.paint(arg0);
    Graphics2D g2d = (Graphics2D) arg0;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(couleur);
    g2d.setStroke(new BasicStroke(3));
    GradientPaint gradient = new GradientPaint(largeur / 2, 4, couleur, (largeur) / 2, 4 + largeur, new Color(255,255,255,200));
    g2d.setPaint(gradient);
    g2d.fillOval(4, 4, largeur, largeur);
    gradient = new GradientPaint(largeur / 2, 4, Color.RED, largeur / 2, 4 + largeur / 2, new Color(couleur.getRed(), couleur.getGreen(), couleur.getBlue(), 0));
    g2d.setPaint(gradient);
    g2d.fillOval(4+largeur/5, 4, 5*largeur/8, largeur/3);
    g2d.setColor(Color.WHITE);
    g2d.setFont(new Font("Calibri", Font.BOLD, 14));
    FontMetrics fm = g2d.getFontMetrics();
    int x = 10;
    int y = (fm.getAscent() + (this.getHeight() -
            (fm.getAscent() + fm.getDescent())) / 2 );
    int x0 = (this.getWidth() - fm.stringWidth(Ident)) / 4 +25;
    int y0 = (fm.getAscent() + (this.getHeight() -
            (fm.getAscent() + fm.getDescent())) / 2 -55);
    g2d.drawString(texte, x+15, y);
    g2d.drawString(Ident, x0, y0);}}`

这是主要的:

public static void main(String[] args) {
    Fenetre splitPaneTest1 = new Fenetre();
    splitPaneTest1.setSize(1000,600);
    splitPaneTest1.show();
    List<ElementMenu> items = new ArrayList<ElementMenu>();
    items.add(new ElementMenu("rad1","ACTION", new Color(19, 73, 140), new Color(162, 198, 232), 150));
    items.add(new ElementMenu("rad2","GOAL",new Color(19, 73, 140), new Color(162, 198, 232), 150));
    items.add(new ElementMenu("rad3","KNOWLEDGE", new Color(19, 73, 140), new Color(162, 198, 232), 150));
    splitPaneTest1.getContentPane().add(jSplitPane1, BorderLayout.CENTER);
    jSplitPane1.setOpaque(false);
    jSplitPane1.add(new MenuCirculaire(Color.blue, 800, 600, 200, 150,items),JSplitPane.RIGHT);
    jSplitPane1.add(new ArrowPanel(363.5,335,460.5,168,540,171,635.5,335),JSplitPane.RIGHT);
    jSplitPane1.add(new MenuGauche(),JSplitPane.LEFT);
}
4

1 回答 1

4

JSplitPane不能这样工作。它只允许一个组件在每个拆分部分中可见。

您将需要创建另一个面板,其中包含的右侧组件。这看起来像:

JPanel rightPanel = new JPanel(new GridLayout(2, 1));
rightPanel.add(monMenuCirculaire); 
rightPanel.add(monArrowPanel);
jSplitPane1.add(rightPanel, JSplitPane.RIGHT);
于 2012-09-02T10:57:31.550 回答