2012-09-02 52 views
0

我正在一个项目中工作,在这个项目中,我必须绘制包含箭头和节点的图形女巫! 我做了一个类,它扩展了箭头的JPanel,节点也是这样,我做了一个JSplitPane来制作左边的菜单和右边的图形!当我添加两个面板时,其中一个只能看到。同时显示两个面板和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); 
} 
+0

您的代码是很长的时间才能够正确分析。你必须做出更简单的方法。因为这是不可读的。 – fonZ

回答

4

JSplitPane不以这种方式工作。它只允许一个组件在每个拆分部分中可见。

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

JPanel rightPanel = new JPanel(new GridLayout(2, 1)); 
rightPanel.add(monMenuCirculaire); 
rightPanel.add(monArrowPanel); 
jSplitPane1.add(rightPanel, JSplitPane.RIGHT); 
+0

我需要将面板放在彼此之上,这样箭头才能链接到节点! – Rad1

+0

不知道你想要什么,但我已经改变了这里的控股面板的布局 – Reimeus

+0

以及将两个面板放在一个面板中,然后将它添加到Jsplitpane中是一个好主意,但问题是一个面板包含箭头和另一个包含节点,所以我必须重叠它们才能得到完整的图形(同时显示arows和节点)!这是布局问题吗? – Rad1

相关问题