الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

[تنبيه - عنوان غير مناسب : ]مساعدة للرسم في jpanel باستخدام netbeans

بدأه THE C-zar في 6 مايو 2010 · 1 رد · 699 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

بسم الله الرحمن الرحيم

السلام عليكم ورحمة الله وبركاته

اخواني اعضاء المنتدى جزاكم الله الخير على هذا الموقع الرائع

اود ان اطلب منكم مساعدة في مشروع رسم مخططات شبيهه بمخططات UML باستخدام Netbeans

فانا لا استطيع عمل رسم او اضافة اي عنصر على JPanel منشأة في البرنامج باستخدام NetBeans

بعد بحث مضن على الانترنت وجدت المثال التالي ولكن مشكلتي تطبيق هذا المثال على الـ NetBeans خصوصا عند انشاء زر والضغط على هذا الزر لانشاء عنصر يمثل صف على سبيل المثال

فارجو منكم المساعدة في اسرع وقت ممكن

ولكم جزيل الشكر


* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.MouseInputAdapter;


public class SelectableLinks implements ActionListener {
SelectableComponentPanel componentPanel;


public void actionPerformed(ActionEvent e) {
componentPanel.removeSelectedLink();
}

private JPanel getContent() {
componentPanel = new SelectableComponentPanel();
componentPanel.setLayout(null);
Color co1 = Color.WHITE;
componentPanel.setBackground(co1);
int numComponents = 5;
Rectangle r = new Rectangle(75, 75, 100, 50);
for(int j = 0; j < numComponents; j++) {
r.x = ((j+1)%2 == 0) ? 250 : 50;
addLabel(componentPanel, r, "Class " + (j+1));
if(j%2 == 1)
r.y += r.height + 75;

}
componentPanel.initialize();
SelectableComponentMover labelMover =
new SelectableComponentMover(componentPanel);
componentPanel.addMouseListener(labelMover);
componentPanel.addMouseMotionListener(labelMover);
return componentPanel;
}

private void addLabel(Container c, Rectangle bounds, String id) {
JLabel label = new JLabel(id, JLabel.CENTER);
c.add(label);
label.setBounds(bounds);
label.setBorder(BorderFactory.createEtchedBorder());
Color co = Color.BLUE;
label.setBackground(co);
// Border raisedbevel;
// raisedbevel = BorderFactory.createRaisedBevelBorder();
//Titled borders
TitledBorder title;

title = BorderFactory.createTitledBorder("Abstract");
title.setTitleJustification(TitledBorder.CENTER);
label.setBorder(title);
}

private JPanel getLast() {
JButton button = new JButton("remove link");
JLabel lbl = new JLabel ("Hi There");
button.addActionListener(this);
JPanel panel = new JPanel();
panel.add(lbl);
panel.add(button);
return panel;
}

public static void main(String[] args) {
SelectableLinks test = new SelectableLinks();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test.getContent());
f.getContentPane().add(test.getLast(), "Last");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}

class SelectableComponentPanel extends JPanel {
Line2D.Double[] lines;
GeneralPath[] arrowHeads;
final int BARB = 12;
final double BARB_ANGLE = 30.0;
Rectangle[] rects;
boolean[] visibility;
final int SIDE = 4;
Rectangle seeker = new Rectangle(SIDE, SIDE);
int selectedIndex = -1;


public SelectableComponentPanel() {
addMouseListener(ml);
}

public void initialize() {
initLines();
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for(int j = 0; j < lines.length; j++) {
if(visibility[j]) {
Color color = Color.black;
if(j == selectedIndex)
color = Color.red;
g2.setPaint(color);
g2.draw(lines[j]);
g2.draw(arrowHeads[j]);
//Label the lines with their index in the lines array.
//double x = lines[j].getX1() +
// (lines[j].getX2() - lines[j].getX1())/3 + 5;
//double y = lines[j].getY1() +
// (lines[j].getY2() - lines[j].getY1())/3 + 5;
//g2.drawString(String.valueOf(j), (float)x, (float)y);
}
}
}

private void initLines() {
Component[] c = getComponents();
int n = c.length;
// The number of lines is the combination of
// n components taken two at a time.
int numLines = factorial(n)/(factorial(2)*factorial(n-2));
arrowHeads = new GeneralPath[numLines];
lines = new Line2D.Double[numLines];
rects = new Rectangle[c.length];
for(int j = 0, count = 0; j < c.length; j++) {
Rectangle r1 = c[j].getBounds();
rects[j] = r1;
for(int k = j+1; k < c.length; k++) {
Rectangle r2 = c[k].getBounds();
double theta = getAngle(r1, r2);
Point2D.Double p1 = getIntersectingPoint(theta, r1);
theta = getAngle(r2, r1);
Point2D.Double p2 = getIntersectingPoint(theta, r2);
arrowHeads[count] = getArrowHead(theta, p1, p2);
lines[count++] = new Line2D.Double(p1, p2);
}
}
visibility = new boolean[numLines];
java.util.Arrays.fill(visibility, true);
}

private GeneralPath getArrowHead(double theta, Point2D.Double p1,
Point2D.Double p2) {
double phi = Math.toRadians(BARB_ANGLE);
float x = (float)(p1.x - BARB*Math.cos(theta+phi));
float y = (float)(p1.y - BARB*Math.sin(theta+phi));
GeneralPath path = new GeneralPath();
path.moveTo(x, y);
path.lineTo((float)p1.x, (float)p1.y);
x = (float)(p1.x - BARB*Math.cos(theta-phi));
y = (float)(p1.y - BARB*Math.sin(theta-phi));
path.lineTo(x, y);
theta += Math.PI;
x = (float)(p2.x - BARB*Math.cos(theta+phi));
y = (float)(p2.y - BARB*Math.sin(theta+phi));
path.moveTo(x, y);
path.lineTo((float)p2.x, (float)p2.y);
x = (float)(p2.x - BARB*Math.cos(theta-phi));
y = (float)(p2.y - BARB*Math.sin(theta-phi));
path.lineTo(x, y);
return path;
}

private double getAngle(Rectangle r1, Rectangle r2) {
double cy1 = r1.getCenterY();
double cx1 = r1.getCenterX();
double cy2 = r2.getCenterY();
double cx2 = r2.getCenterX();
double dy = cy2 - cy1;
double dx = cx2 - cx1;
return Math.atan2(dy, dx);
}

private Point2D.Double getIntersectingPoint(double theta, Rectangle r) {
double cx = r.getCenterX();
double cy = r.getCenterY();
double W = r.getWidth()/2;
double H = r.getHeight()/2;
double R = Point2D.distance(cx, cy, cx+W, cy+H);
double x = cx + R * Math.cos(theta);
double y = cy + R * Math.sin(theta);
Point2D.Double p = new Point2D.Double();
int outcode = r.outcode(x, y);
switch(outcode) {
case Rectangle.OUT_TOP:
p.x = cx - H * ((x - cx)/(y - cy));
p.y = cy - H;
break;
case Rectangle.OUT_LEFT:
p.x = cx - W;
p.y = cy - W * ((y - cy)/(x - cx));
break;
case Rectangle.OUT_BOTTOM:
p.x = cx + H * ((x - cx)/(y - cy));
p.y = cy + H;
break;
case Rectangle.OUT_RIGHT:
p.x = cx + W;
p.y = cy + W * ((y - cy)/(x - cx));
break;
}
return p;
}

private static int factorial(int n) {
return n == 0 ? 1 : n*factorial(n-1);
}

public void setLinesFor(Rectangle r, int index) {
// Update the rect that is being dragged.
rects[index].setRect(r);
// Find the indices of the lines connected to
// the dragged rect - these need to be reset.
int[] indices = getLineIndices(index);
// Reset the indicated lines.
for(int j = 0, k = 0; j < rects.length; j++) {
if(j == index) continue;
Rectangle r2 = rects[j];
double theta = getAngle(r, r2);
Point2D.Double p1 = getIntersectingPoint(theta, r);
theta = getAngle(r2, r);
Point2D.Double p2 = getIntersectingPoint(theta, r2);
arrowHeads[indices[k]] = getArrowHead(theta, p1, p2);
lines[indices[k++]].setLine(p1, p2);
}
}

private int[] getLineIndices(int index) {
// Find indices of lines that need to be changed.
int[] indices = new int[rects.length-1];
int lineIndex = 0;
for(int j = 0, count = 0; j < index+1; j++) {
for(int k = 0; k < rects.length; k++) {
if((j < index & k == index) || (j == index && k > index))
indices[count++] = lineIndex;
if(k > j)
lineIndex++;
}
}
return indices;
}

public void removeSelectedLink() {
if(selectedIndex != -1) {
visibility[selectedIndex] = false;
repaint();
}
}

private MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
// Position seeker over Point p.
seeker.setFrameFromCenter(p.x, p.y, p.x+seeker.width/2,
p.y+seeker.height/2);
// Do any lines intersect seeker?
for(int j = 0; j < lines.length; j++) {
if(lines[j].intersects(seeker)) {
// Change the selection or toggle selection off.
if(selectedIndex == -1 || selectedIndex != j)
selectedIndex = j;
else
selectedIndex = -1;
repaint();
break;
}
}
}
};
}

class SelectableComponentMover extends MouseInputAdapter {
SelectableComponentPanel componentPanel;
Point offset = new Point();
int selectedIndex;
boolean dragging = false;

public SelectableComponentMover(SelectableComponentPanel panel) {
componentPanel = panel;
}

public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
Component[] c = componentPanel.getComponents();
for(int j = 0; j < c.length; j++) {
Rectangle r = c[j].getBounds();
if(r.contains(p)) {
offset.x = p.x - r.x;
offset.y = p.y - r.y;
selectedIndex = j;
dragging = true;
break;
}
}
}

public void mouseReleased(MouseEvent e) {
dragging = false;
}

public void mouseDragged(MouseEvent e) {
if(dragging) {
int x = e.getX() - offset.x;
int y = e.getY() - offset.y;
// Update the component location.
Component c = componentPanel.getComponent(selectedIndex);
c.setLocation(x, y);
// Update lines that terminate at component.
componentPanel.setLinesFor(c.getBounds(), selectedIndex);
componentPanel.repaint();
}
}
}

/*

#2

الأخ الكريم/الأخت الكريمة

السلام عليكم ورحمة الله وبركاته

مرحباً بكم في منتدى الفريق العربي للبرمجة

نود تنبيهك أن العنوان غير مناسب.

الرجاء اختيار عنوان مناسب أكثر في مواضيعك القادمة، وهذا بمثابة إشعار للأهمية.

وفي حال التكرار سيتعرض موضوعك للإغلاق والحذف!!!

قواعد المشاركة

/index.php?showtopic=29343

شاكرين لكم حُسن تعاونكم

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…