Apache POI XSLFAutoShape - Shape OffSet property - java

I am drawing a pptx from scratch using ApachePOI-XSLF (ooxml implementation) and I am struggling to find good documentation and good examples. Also the library isn't easy to follow and not straight forward.
I would like to know how do I set the offset of two shapes of XSFLAutoShape connected by XSLFConnectorShape.
This is my code right now:
XMLSlideShow pptx = new XMLSlideShow();
XSLFSlide slide = pptx.createSlide();
XSLFAutoShape rectangle = slide.createAutoShape();
rectangle.setShapeType(ShapeType.RECT);
rectangle.setAnchor(new Rectangle2D.Double(100, 100, 100, 50));
rectangle.setLineColor(Color.blue);
rectangle.setFillColor(Color.lightGray);
XSLFAutoShape miniCircle = slide.createAutoShape();
miniCircle.setShapeType(ShapeType.ELLIPSE);
miniCircle.setAnchor(new Rectangle2D.Double(rectangle.getAnchor().getMaxX() + 200, rectangle.getAnchor().getCenterY()+50, 1, 1));
miniCircle.setLineColor(Color.yellow);
miniCircle.setFillColor(Color.yellow);
connect(slide, rectangle, miniCircle);
XSLFAutoShape anotherRectangle = slide.createAutoShape();
anotherRectangle.setShapeType(ShapeType.RECT);
anotherRectangle.setAnchor(new Rectangle2D.Double(miniCircle.getAnchor().getMaxX() + 200, 100, 100, 50));
anotherRectangle.setLineColor(Color.blue);
anotherRectangle.setFillColor(Color.lightGray);
connect(slide, miniCircle, anotherRectangle);
and the connect method:
public static void connect(XSLFSlide slide, XSLFAutoShape start, XSLFAutoShape end){
XSLFConnectorShape connector = slide.createConnector();
connector.setAnchor(new Rectangle2D.Double(start.getAnchor().getX() + 100, start.getAnchor().getCenterY(), 100, 1));
CTConnector ctConnector = (CTConnector)connector.getXmlObject();
ctConnector.getSpPr().getPrstGeom().setPrst(STShapeType.STRAIGHT_CONNECTOR_1);
CTNonVisualConnectorProperties cx = ctConnector.getNvCxnSpPr().getCNvCxnSpPr();
// connection start
CTConnection stCxn = cx.addNewStCxn();
stCxn.setId(start.getShapeId());
// side of the rectangle to attach the connector: left=1, bottom=2,right=3, top=4
stCxn.setIdx(3);
CTConnection endCxn = cx.addNewEndCxn();
endCxn.setId(end.getShapeId());
// side of the rectangle to attach the connector: left=1, bottom=2,right=3, top=4
endCxn.setIdx(3);
}
The output of is:
This is the output
But I want this:
This is what I want
p.s. I edit the post because someone thumbs down the post without a reason. I added more info but still complicated because the library is complicated :-/. would mind help and thumbs up again ? If you cannot help at least do not destroy my reputation. I am serious developer. Tkx.

Related

JavaCV assertion failed

I am trying to use K-means JavaCV implementation, but I have the following error:
OpenCV Error: Assertion failed (!centers.empty()) in cvKMeans2, file src\matrix.cpp, line 4233
My source code is:
IplImage src = cvLoadImage(fileName, CV_LOAD_IMAGE_COLOR);
int cluster_count = 3;
int attempts = 10;
CvTermCriteria termCriteria = new CvTermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER, 10, 1.0);
cvReshape(src, src.asCvMat(), 1, src.height() * src.width());
IplImage samples = cvCreateImage(cvGetSize(src), src.depth(), 1);
cvConvertImage(src, samples, CV_32F);
IplImage labels = cvCreateImage(new CvSize(samples.height()), 1, CV_8U);
IplImage centers = cvCreateImage(new CvSize(cluster_count), 1, CV_32F);
cvKMeans2(samples, cluster_count, labels, termCriteria, 1, new long[attempts], KMEANS_RANDOM_CENTERS, centers, new double[attempts]);
I'm beginner on JavaCV and would to know what I doing wrong in this code?

OpenCV 3.0 svm always predicts 0 (java)

I'm new to opencv's svm's. I tried a sample classifier but it only returns 0 as the predicted label. I even used the value 5 for training as well as the prediction.
I've been changing the values for about a hundred times but i just don't get what's wrong. I'm using OpenCV 3.0 with Java. Here's my code:
Mat labels = new Mat(new Size(1,4),CvType.CV_32SC1);
labels.put(0, 0, 1);
labels.put(1, 0, 1);
labels.put(2, 0, 1);
labels.put(3, 0, 0);
Mat data = new Mat(new Size(1,4),CvType.CV_32FC1);
data.put(0, 0, 5);
data.put(1, 0, 2);
data.put(2, 0, 3);
data.put(3, 0, 8);
Mat testSamples = new Mat(new Size(1,1),CvType.CV_32FC1);
testSamples.put(0,0,5);
SVM svm = SVM.create();
TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1);
svm.setKernel(SVM.LINEAR);
svm.setType(SVM.C_SVC);
svm.setGamma(0.5);
svm.setNu(0.5);
svm.setC(1);
svm.setTermCriteria(criteria);
//data is N x 64 trained data Mat , labels is N x 1 label Mat with integer values;
svm.train(data, Ml.ROW_SAMPLE, labels);
Mat results = new Mat();
int predictedClass = (int) svm.predict(testSamples, results, 0);
Even if i change the lables to 1 and 2, I still get 0.0 as a response. So something has to be absolutely wrong... I just don't know what to do. Please help! :)
I had a similar problem in C++. I'm not too sure if it's the same in Java but in C++ the predictions were saved in the results Matrix instead of returned as a float.

Java Graphics2D Path2D Simplify or call from separate class

I'm wondering if I can clean up my code somehow using a list or call it from another class because I have a lot of exact Path2D coordinates just cluttering my paintComponent.
public void paintComponent(Graphics g)
{ Graphics2D g2=(Graphics2D)g;
super.paintComponent(g2);
//Background
Rectangle background = new Rectangle(0,0,getWidth(),getHeight());
Color skyBlue = new Color(135,206,235);
g2.setColor(skyBlue);
g2.fill (background);
Path2D bunny = new Path2D.Float();
bunny.moveTo(486.63,530.25);
bunny.lineTo(483.13,532.25);
bunny.lineTo(483.13,532.25);
bunny.lineTo(483.13,532.25);
bunny.lineTo(477.38,534.38);
bunny.lineTo(477.38,534.38);
bunny.lineTo(477.38,534.38);
bunny.lineTo(469.88,535.50);
bunny.lineTo(469.88,535.50);
bunny.lineTo(469.88,535.50);
bunny.lineTo(453.50,536.38);
bunny.lineTo(453.50,536.38);
bunny.lineTo(453.50,536.38);
bunny.lineTo(437.63,537.75);
bunny.lineTo(437.63,537.75);
bunny.lineTo(437.63,537.75);
bunny.lineTo(418.75,539.25);
bunny.lineTo(418.75,539.25);
bunny.lineTo(418.75,539.25);
bunny.lineTo(405.00,539.88);
bunny.lineTo(405.00,539.88);
bunny.lineTo(405.00,539.88);
bunny.lineTo(392.25,539.63);
bunny.lineTo(392.25,539.63);
bunny.lineTo(392.25,539.63);
bunny.lineTo(385.50,539.13);
bunny.lineTo(385.50,539.13);
bunny.lineTo(385.50,539.13);
bunny.lineTo(381.00,538.63);
bunny.lineTo(381.00,538.63);
bunny.lineTo(381.00,538.63);
bunny.lineTo(375.50,537.88);
bunny.lineTo(375.50,537.88);
bunny.lineTo(375.50,537.88);
bunny.lineTo(370.13,537.75);
bunny.lineTo(370.13,537.75);
bunny.lineTo(370.13,537.75);
bunny.lineTo(364.88,537.13);
bunny.lineTo(364.88,537.13);
bunny.lineTo(364.88,537.13);
bunny.lineTo(357.75,536.63);
bunny.lineTo(357.75,536.63);
bunny.lineTo(357.75,536.63);
bunny.lineTo(353.50,536.25);
bunny.lineTo(353.50,536.25);
bunny.lineTo(353.50,536.25);
bunny.lineTo(347.88,535.75);
bunny.lineTo(347.88,535.75);
bunny.lineTo(347.88,535.75);
bunny.lineTo(341.75,535.38);
bunny.lineTo(341.75,535.38);
bunny.lineTo(341.75,535.38);
bunny.lineTo(336.25,534.38);
bunny.lineTo(336.25,534.38);
bunny.lineTo(336.25,534.38);
bunny.lineTo(328.25,532.88);
bunny.lineTo(328.25,532.88);
bunny.lineTo(328.25,532.88);
bunny.lineTo(323.00,530.63);
bunny.lineTo(323.00,530.63);
bunny.lineTo(323.00,530.63);
bunny.lineTo(318.50,528.13);
bunny.lineTo(318.50,528.13);
bunny.lineTo(318.50,528.13);
bunny.lineTo(313.50,524.63);
bunny.lineTo(313.50,524.63);
bunny.lineTo(313.50,524.63);
bunny.lineTo(311.63,521.25);
bunny.lineTo(311.63,521.25);
bunny.lineTo(311.63,521.25);
bunny.lineTo(310.38,516.25);
bunny.lineTo(310.38,516.25);
bunny.lineTo(310.38,516.25);
bunny.lineTo(309.88,513.00);
bunny.lineTo(309.88,513.00);
bunny.lineTo(309.88,513.00);
bunny.lineTo(311.13,508.38);
bunny.lineTo(311.13,508.38);
bunny.lineTo(311.13,508.38);
bunny.lineTo(312.25,505.38);
bunny.lineTo(312.25,505.38);
bunny.lineTo(312.25,505.38);
bunny.lineTo(313.00,502.63);
bunny.lineTo(313.00,502.63);
bunny.lineTo(313.00,502.63);
bunny.lineTo(314.75,500.00);
bunny.lineTo(314.75,500.00);
bunny.lineTo(314.75,500.00);
bunny.lineTo(316.38,497.63);
bunny.lineTo(316.38,497.63);
bunny.lineTo(316.38,497.63);
bunny.lineTo(316.25,494.00);
bunny.lineTo(316.25,494.00);
bunny.lineTo(316.25,494.00);
bunny.lineTo(314.88,491.00);
bunny.lineTo(314.88,491.00);
bunny.lineTo(314.88,491.00);
bunny.lineTo(312.88,486.38);
bunny.lineTo(312.88,486.38);
bunny.lineTo(312.88,486.38);
bunny.lineTo(310.13,480.00);
bunny.lineTo(310.13,480.00);
bunny.lineTo(310.13,480.00);
bunny.lineTo(308.50,474.75);
bunny.lineTo(308.50,474.75);
bunny.lineTo(308.50,474.75);
bunny.lineTo(306.63,468.75);
bunny.lineTo(306.63,468.75);
bunny.lineTo(306.63,468.75);
bunny.lineTo(305.25,462.75);
bunny.lineTo(305.25,462.75);
bunny.lineTo(305.25,462.75);
bunny.lineTo(304.88,456.63);
bunny.lineTo(304.88,456.63);
bunny.lineTo(304.88,456.63);
bunny.lineTo(304.88,449.88);
bunny.lineTo(304.88,449.88);
bunny.lineTo(304.88,449.88);
bunny.lineTo(305.00,446.38);
bunny.lineTo(305.00,446.38);
bunny.lineTo(305.00,446.38);
bunny.lineTo(305.88,442.75);
bunny.lineTo(305.88,442.75);
bunny.lineTo(305.88,442.75);
bunny.lineTo(307.63,437.88);
bunny.lineTo(307.63,437.88);
bunny.lineTo(307.63,437.88);
bunny.lineTo(309.25,434.50);
bunny.lineTo(309.25,434.50);
bunny.lineTo(309.25,434.50);
bunny.lineTo(310.50,432.25);
bunny.lineTo(310.50,432.25);
bunny.lineTo(310.50,432.25);
bunny.lineTo(311.63,430.25);
bunny.lineTo(311.63,430.25);
bunny.lineTo(311.63,430.25);
bunny.lineTo(312.50,427.25);
bunny.lineTo(312.50,427.25);
bunny.lineTo(312.50,427.25);
bunny.lineTo(312.63,424.00);
bunny.lineTo(312.63,424.00);
bunny.lineTo(312.63,424.00);
bunny.lineTo(311.75,421.25);
bunny.lineTo(311.75,421.25);
bunny.lineTo(311.75,421.25);
bunny.lineTo(309.38,417.88);
bunny.lineTo(309.38,417.88);
bunny.lineTo(309.38,417.88);
bunny.lineTo(307.00,415.00);
bunny.lineTo(307.00,415.00);
bunny.lineTo(307.00,415.00);
bunny.lineTo(304.50,412.25);
bunny.lineTo(304.50,412.25);
bunny.lineTo(304.50,412.25);
bunny.lineTo(302.63,409.38);
bunny.lineTo(302.63,409.38);
bunny.lineTo(302.63,409.38);
bunny.lineTo(300.13,406.63);
bunny.lineTo(300.13,406.63);
bunny.lineTo(300.13,406.63);
bunny.lineTo(298.00,402.88);
bunny.lineTo(298.00,402.88);
bunny.lineTo(298.00,402.88);
bunny.lineTo(296.63,400.50);
bunny.lineTo(296.63,400.50);
bunny.lineTo(296.63,400.50);
bunny.lineTo(295.38,397.88);
bunny.lineTo(295.38,397.88);
bunny.lineTo(295.38,397.88);
bunny.lineTo(294.50,395.63);
bunny.lineTo(294.50,395.63);
bunny.lineTo(294.50,395.63);
bunny.lineTo(294.13,392.88);
bunny.lineTo(294.13,392.88);
bunny.lineTo(294.13,392.88);
bunny.lineTo(293.13,389.88);
bunny.lineTo(293.13,389.88);
bunny.lineTo(293.13,389.88);
bunny.lineTo(292.38,385.75);
bunny.lineTo(292.38,385.75);
bunny.lineTo(292.38,385.75);
bunny.lineTo(291.75,381.13);
bunny.lineTo(291.75,381.13);
bunny.lineTo(291.75,381.13);
bunny.lineTo(290.88,371.75);
bunny.lineTo(290.88,371.75);
bunny.lineTo(290.88,371.75);
bunny.lineTo(291.13,366.88);
bunny.lineTo(291.13,366.88);
bunny.lineTo(291.13,366.88);
bunny.lineTo(293.25,362.50);
bunny.lineTo(293.25,362.50);
bunny.lineTo(293.25,362.50);
bunny.lineTo(295.38,358.88);
bunny.lineTo(295.38,358.88);
bunny.lineTo(295.38,358.88);
bunny.lineTo(296.75,356.38);
bunny.lineTo(296.75,356.38);
bunny.lineTo(296.75,356.38);
bunny.lineTo(300.00,350.00);
bunny.lineTo(300.00,350.00);
bunny.lineTo(300.00,350.00);
bunny.lineTo(305.63,342.75);
bunny.lineTo(305.63,342.75);
bunny.lineTo(305.63,342.75);
bunny.lineTo(309.88,337.00);
bunny.lineTo(309.88,337.00);
bunny.lineTo(309.88,337.00);
bunny.lineTo(313.50,333.13);
bunny.lineTo(313.50,333.13);
bunny.lineTo(313.50,333.13);
bunny.lineTo(319.25,327.63);
bunny.lineTo(319.25,327.63);
bunny.lineTo(319.25,327.63);
bunny.lineTo(323.38,324.00);
bunny.lineTo(323.38,324.00);
bunny.lineTo(323.38,324.00);
bunny.lineTo(328.38,321.25);
bunny.lineTo(328.38,321.25);
bunny.lineTo(328.38,321.25);
bunny.lineTo(333.75,318.88);
bunny.lineTo(333.75,318.88);
bunny.lineTo(333.75,318.88);
bunny.lineTo(338.50,316.38);
bunny.lineTo(338.50,316.38);
bunny.lineTo(338.50,316.38);
bunny.lineTo(341.63,315.50);
bunny.lineTo(341.63,315.50);
bunny.lineTo(341.63,315.50);
bunny.lineTo(348.50,313.25);
bunny.lineTo(348.50,313.25);
bunny.lineTo(348.50,313.25);
bunny.lineTo(355.25,312.00);
bunny.lineTo(355.25,312.00);
bunny.lineTo(355.25,312.00);
bunny.lineTo(361.00,310.88);
bunny.lineTo(361.00,310.88);
bunny.lineTo(361.00,310.88);
bunny.lineTo(364.50,309.75);
bunny.lineTo(364.50,309.75);
bunny.lineTo(364.50,309.75);
bunny.lineTo(367.75,307.50);
bunny.lineTo(367.75,307.50);
bunny.lineTo(367.75,307.50);
bunny.lineTo(373.25,302.50);
bunny.lineTo(373.25,302.50);
bunny.lineTo(373.25,302.50);
bunny.lineTo(379.13,296.13);
bunny.lineTo(379.13,296.13);
bunny.lineTo(379.13,296.13);
bunny.lineTo(384.25,290.13);
bunny.lineTo(384.25,290.13);
bunny.lineTo(384.25,290.13);
bunny.lineTo(389.25,283.88);
bunny.lineTo(389.25,283.88);
bunny.lineTo(389.25,283.88);
bunny.lineTo(393.25,279.50);
bunny.lineTo(393.25,279.50);
bunny.lineTo(393.25,279.50);
bunny.lineTo(396.63,275.50);
bunny.lineTo(396.63,275.50);
bunny.lineTo(396.63,275.50);
bunny.lineTo(400.50,271.38);
bunny.lineTo(400.50,271.38);
bunny.lineTo(400.50,271.38);
bunny.lineTo(406.25,266.75);
bunny.lineTo(406.25,266.75);
bunny.lineTo(406.25,266.75);
bunny.lineTo(411.50,262.13);
bunny.lineTo(411.50,262.13);
bunny.lineTo(411.50,262.13);
bunny.lineTo(414.88,259.75);
bunny.lineTo(414.88,259.75);
bunny.lineTo(414.88,259.75);
bunny.lineTo(420.25,256.63);
bunny.lineTo(420.25,256.63);
bunny.lineTo(420.25,256.63);
bunny.lineTo(424.63,253.88);
bunny.lineTo(424.63,253.88);
bunny.lineTo(424.63,253.88);
bunny.lineTo(427.50,252.88);
bunny.lineTo(427.50,252.88);
bunny.lineTo(427.50,252.88);
bunny.lineTo(431.13,251.50);
bunny.lineTo(431.13,251.50);
bunny.lineTo(431.13,251.50);
bunny.lineTo(434.25,251.13);
bunny.lineTo(434.25,251.13);
bunny.lineTo(434.25,251.13);
bunny.lineTo(437.63,251.00);
bunny.lineTo(437.63,251.00);
bunny.lineTo(437.63,251.00);
bunny.lineTo(442.50,252.25);
bunny.lineTo(442.50,252.25);
bunny.lineTo(442.50,252.25);
bunny.lineTo(445.50,253.75);
bunny.lineTo(445.50,253.75);
bunny.lineTo(445.50,253.75);
bunny.lineTo(447.88,255.25);
bunny.lineTo(447.88,255.25);
bunny.lineTo(447.88,255.25);
bunny.lineTo(450.50,258.25);
bunny.lineTo(450.50,258.25);
bunny.lineTo(450.50,258.25);
bunny.lineTo(451.88,260.75);
bunny.lineTo(451.88,260.75);
bunny.lineTo(451.88,260.75);
bunny.lineTo(453.38,264.13);
bunny.lineTo(453.38,264.13);
bunny.lineTo(453.38,264.13);
bunny.lineTo(454.25,266.88);
bunny.lineTo(454.25,266.88);
bunny.lineTo(454.25,266.88);
bunny.lineTo(455.50,271.13);
bunny.lineTo(455.50,271.13);
bunny.lineTo(455.50,271.13);
bunny.lineTo(455.63,274.75);
bunny.lineTo(455.63,274.75);
bunny.lineTo(455.63,274.75);
bunny.lineTo(456.75,276.50);
bunny.lineTo(456.75,276.50);
bunny.lineTo(456.75,276.50);
bunny.lineTo(458.63,278.00);
bunny.lineTo(458.63,278.00);
bunny.lineTo(458.63,278.00);
bunny.lineTo(460.00,278.88);
bunny.lineTo(460.00,278.88);
bunny.lineTo(460.00,278.88);
bunny.lineTo(461.00,280.75);
bunny.lineTo(461.00,280.75);
bunny.lineTo(461.00,280.75);
bunny.lineTo(462.75,281.00);
bunny.lineTo(462.75,281.00);
bunny.lineTo(462.75,281.00);
bunny.lineTo(464.88,281.88);
bunny.lineTo(464.88,281.88);
bunny.lineTo(464.88,281.88);
bunny.lineTo(467.38,283.00);
bunny.lineTo(467.38,283.00);
bunny.lineTo(467.38,283.00);
bunny.lineTo(470.13,284.50);
bunny.lineTo(470.13,284.50);
bunny.lineTo(470.13,284.50);
bunny.lineTo(472.00,286.13);
bunny.lineTo(472.00,286.13);
bunny.lineTo(473.63,287.50);
bunny.lineTo(477.13,290.13);
bunny.lineTo(477.13,290.13);
bunny.lineTo(477.13,290.13);
bunny.lineTo(479.50,292.25);
bunny.lineTo(479.50,292.25);
bunny.lineTo(479.50,292.25);
bunny.lineTo(481.50,295.00);
bunny.lineTo(481.50,295.00);
bunny.lineTo(481.50,295.00);
bunny.lineTo(483.38,299.13);
bunny.lineTo(483.38,299.13);
bunny.lineTo(483.38,299.13);
bunny.lineTo(483.38,303.63);
bunny.lineTo(483.38,303.63);
bunny.lineTo(483.38,303.63);
bunny.lineTo(482.63,308.00);
bunny.lineTo(482.63,308.00);
bunny.lineTo(482.63,308.00);
bunny.lineTo(480.75,311.75);
bunny.lineTo(480.75,311.75);
bunny.lineTo(480.75,311.75);
bunny.lineTo(469.25,325.00);
bunny.lineTo(469.25,325.00);
bunny.lineTo(469.25,325.00);
bunny.lineTo(464.00,330.63);
bunny.lineTo(464.00,330.63);
bunny.lineTo(464.00,330.63);
bunny.lineTo(458.50,337.13);
bunny.lineTo(458.50,337.13);
bunny.lineTo(458.50,337.13);
bunny.lineTo(451.75,345.13);
bunny.lineTo(451.75,345.13);
bunny.lineTo(451.75,345.13);
bunny.lineTo(448.38,349.63);
bunny.lineTo(448.38,349.63);
bunny.lineTo(448.38,349.63);
bunny.lineTo(446.00,353.13);
bunny.lineTo(446.00,353.13);
bunny.lineTo(446.00,353.13);
bunny.lineTo(443.38,357.75);
bunny.lineTo(443.38,357.75);
bunny.lineTo(443.38,357.75);
bunny.lineTo(442.13,361.13);
bunny.lineTo(442.13,361.13);
bunny.lineTo(442.13,361.13);
bunny.lineTo(441.88,367.38);
bunny.lineTo(441.88,367.38);
bunny.lineTo(441.88,367.38);
bunny.lineTo(441.75,371.75);
bunny.lineTo(441.75,371.75);
bunny.lineTo(441.75,371.75);
bunny.lineTo(442.13,375.63);
bunny.lineTo(442.13,375.63);
bunny.lineTo(442.13,375.63);
bunny.lineTo(443.00,381.88);
bunny.lineTo(443.00,381.88);
bunny.lineTo(443.00,381.88);
bunny.lineTo(444.38,385.75);
bunny.lineTo(444.38,385.75);
bunny.lineTo(444.38,385.75);
bunny.lineTo(446.50,390.75);
bunny.lineTo(446.50,390.75);
bunny.lineTo(446.50,390.75);
bunny.lineTo(450.00,396.38);
bunny.lineTo(450.00,396.38);
bunny.lineTo(450.00,396.38);
bunny.lineTo(452.88,399.50);
bunny.lineTo(452.88,399.50);
bunny.lineTo(452.88,399.50);
bunny.lineTo(457.25,404.25);
bunny.lineTo(457.25,404.25);
bunny.lineTo(457.25,404.25);
bunny.lineTo(462.38,409.50);
bunny.lineTo(462.38,409.50);
bunny.lineTo(462.38,409.50);
bunny.lineTo(467.25,414.50);
bunny.lineTo(467.25,414.50);
bunny.lineTo(467.25,414.50);
bunny.lineTo(471.25,418.63);
bunny.lineTo(471.25,418.63);
bunny.lineTo(471.25,418.63);
bunny.lineTo(473.25,421.38);
bunny.lineTo(473.25,421.38);
bunny.lineTo(473.25,421.38);
bunny.lineTo(476.63,426.13);
bunny.lineTo(476.63,426.13);
bunny.lineTo(476.63,426.13);
bunny.lineTo(480.75,431.13);
bunny.lineTo(480.75,431.13);
bunny.lineTo(480.75,431.13);
bunny.lineTo(483.50,435.13);
bunny.lineTo(483.50,435.13);
bunny.lineTo(483.50,435.13);
bunny.lineTo(485.25,439.63);
bunny.lineTo(485.25,439.63);
bunny.lineTo(485.25,439.63);
bunny.lineTo(487.50,444.75);
bunny.lineTo(487.50,444.75);
bunny.lineTo(487.50,444.75);
bunny.lineTo(489.13,448.88);
bunny.lineTo(489.13,448.88);
bunny.lineTo(489.13,448.88);
bunny.lineTo(489.75,453.88);
bunny.lineTo(489.75,453.88);
bunny.lineTo(489.75,453.88);
bunny.lineTo(490.63,459.38);
bunny.lineTo(490.63,459.38);
bunny.lineTo(490.63,459.38);
bunny.lineTo(492.38,466.25);
bunny.lineTo(492.38,466.25);
bunny.lineTo(492.38,466.25);
bunny.lineTo(493.38,472.88);
bunny.lineTo(493.38,472.88);
bunny.lineTo(493.38,472.88);
bunny.lineTo(493.88,477.13);
bunny.lineTo(493.88,477.13);
bunny.lineTo(493.88,477.13);
bunny.lineTo(494.38,480.63);
bunny.lineTo(494.38,480.63);
bunny.lineTo(494.38,480.63);
bunny.lineTo(495.50,483.38);
bunny.lineTo(495.50,483.38);
bunny.lineTo(495.50,483.38);
bunny.lineTo(498.00,485.25);
bunny.lineTo(498.00,485.25);
bunny.lineTo(498.00,485.25);
bunny.lineTo(499.50,488.00);
bunny.lineTo(499.50,488.00);
bunny.lineTo(499.50,488.00);
bunny.lineTo(500.63,491.50);
bunny.lineTo(500.63,491.50);
bunny.lineTo(500.63,491.50);
bunny.lineTo(501.25,497.13);
bunny.lineTo(501.25,497.13);
bunny.lineTo(501.25,497.13);
bunny.lineTo(500.13,501.13);
bunny.lineTo(500.13,501.13);
bunny.lineTo(500.13,501.13);
bunny.lineTo(499.13,505.63);
bunny.lineTo(499.13,505.63);
bunny.lineTo(499.13,505.63);
bunny.lineTo(497.88,507.88);
bunny.lineTo(497.88,507.88);
bunny.lineTo(497.88,507.88);
bunny.lineTo(495.88,512.25);
bunny.lineTo(495.88,512.25);
bunny.lineTo(495.88,512.25);
bunny.lineTo(494.38,516.25);
bunny.lineTo(494.38,516.25);
bunny.lineTo(494.38,516.25);
bunny.lineTo(493.25,518.63);
bunny.lineTo(493.25,518.63);
bunny.lineTo(493.25,518.63);
bunny.lineTo(491.88,521.88);
bunny.lineTo(491.88,521.88);
bunny.lineTo(491.88,521.88);
bunny.lineTo(489.38,524.00);
bunny.lineTo(489.38,524.00);
bunny.lineTo(489.38,524.00);
bunny.lineTo(489.13,526.63);
bunny.lineTo(489.13,526.63);
bunny.lineTo(489.13,526.63);
bunny.lineTo(488.13,528.75);
bunny.lineTo(488.13,528.75);
bunny.closePath();
g2.draw(bunny);
Color gold = new Color(255,215,0);
g2.setColor(gold);
g2.fill(bunny);
Path2D chocoears = new Path2D.Float();
chocoears.moveTo(473.63, 287.50);
chocoears.lineTo(473.63,287.50);
chocoears.lineTo(477.13,290.13);
chocoears.lineTo(477.13,290.13);
chocoears.lineTo(477.13,290.13);
chocoears.lineTo(479.50,292.25);
chocoears.lineTo(479.50,292.25);
chocoears.lineTo(479.50,292.25);
chocoears.lineTo(481.50,295.00);
chocoears.lineTo(481.50,295.00);
chocoears.lineTo(481.50,295.00);
chocoears.lineTo(483.38,299.13);
chocoears.lineTo(483.38,299.13);
chocoears.lineTo(483.38,299.13);
chocoears.lineTo(483.38,303.63);
chocoears.lineTo(483.38,303.63);
chocoears.lineTo(483.38,303.63);
chocoears.lineTo(482.63,308.00);
chocoears.lineTo(482.63,308.00);
chocoears.lineTo(482.63,308.00);
chocoears.lineTo(480.75,311.75);
chocoears.lineTo(480.75,311.75);
chocoears.lineTo(480.75,311.75);
chocoears.lineTo(469.25,325.00);
chocoears.lineTo(469.25,325.00);
chocoears.lineTo(469.25,325.00);
chocoears.lineTo(464.00,330.63);
chocoears.lineTo(464.00,330.63);
chocoears.lineTo(464.00,330.63);
chocoears.lineTo(458.50,337.13);
chocoears.lineTo(458.50,337.13);
chocoears.lineTo(458.50,337.13);
chocoears.lineTo(451.75,345.13);
chocoears.lineTo(451.75,345.13);
chocoears.lineTo(451.75,345.13);
chocoears.lineTo(442.33,351.00);
chocoears.lineTo(442.33,351.00);
chocoears.lineTo(442.33,351.00);
chocoears.lineTo(435.33,351.00);
chocoears.lineTo(435.33,351.00);
chocoears.lineTo(435.33,351.00);
chocoears.lineTo(432.00,349.00);
chocoears.lineTo(432.00,349.00);
chocoears.lineTo(432.00,349.00);
chocoears.lineTo(427.00,348.67);
chocoears.lineTo(427.00,348.67);
chocoears.lineTo(427.00,348.67);
chocoears.lineTo(420.00,342.67);
chocoears.lineTo(420.00,342.67);
chocoears.lineTo(420.00,342.67);
chocoears.lineTo(415.33,339.33);
chocoears.lineTo(415.33,339.33);
chocoears.lineTo(415.33,339.33);
chocoears.lineTo(412.00,341.00);
chocoears.lineTo(412.00,341.00);
chocoears.lineTo(412.00,341.00);
chocoears.lineTo(408.33,337.00);
chocoears.lineTo(408.33,337.00);
chocoears.lineTo(408.33,337.00);
chocoears.lineTo(407.33,333.33);
chocoears.lineTo(407.33,333.33);
chocoears.lineTo(407.33,333.33);
chocoears.lineTo(403.67,329.67);
chocoears.lineTo(403.67,329.67);
chocoears.lineTo(403.67,329.67);
chocoears.lineTo(399.00,331.67);
chocoears.lineTo(399.00,331.67);
chocoears.lineTo(399.00,331.67);
chocoears.lineTo(396.33,329.33);
chocoears.lineTo(396.33,329.33);
chocoears.lineTo(396.33,329.33);
chocoears.lineTo(394.33,326.67);
chocoears.lineTo(394.33,326.67);
chocoears.lineTo(394.33,326.67);
chocoears.lineTo(386.67,326.33);
chocoears.lineTo(386.67,326.33);
chocoears.lineTo(386.67,326.33);
chocoears.lineTo(382.33,320.67);
chocoears.lineTo(382.33,320.67);
chocoears.lineTo(382.33,320.67);
chocoears.lineTo(378.00,322.00);
chocoears.lineTo(378.00,322.00);
chocoears.lineTo(378.00,322.00);
chocoears.lineTo(372.00,320.00);
chocoears.lineTo(372.00,320.00);
chocoears.lineTo(372.00,320.00);
chocoears.lineTo(373.33,318.00);
chocoears.lineTo(373.33,318.00);
chocoears.lineTo(373.33,318.00);
chocoears.lineTo(371.33,315.67);
chocoears.lineTo(371.33,315.67);
chocoears.lineTo(371.33,315.67);
chocoears.lineTo(365.33,316.00);
chocoears.lineTo(365.33,316.00);
chocoears.lineTo(365.33,316.00);
chocoears.lineTo(355.25,312.00);
chocoears.lineTo(355.25,312.00);
chocoears.lineTo(355.25,312.00);
chocoears.lineTo(361.00,310.88);
chocoears.lineTo(361.00,310.88);
chocoears.lineTo(361.00,310.88);
chocoears.lineTo(364.50,309.75);
chocoears.lineTo(364.50,309.75);
chocoears.lineTo(364.50,309.75);
chocoears.lineTo(367.75,307.50);
chocoears.lineTo(367.75,307.50);
chocoears.lineTo(367.75,307.50);
chocoears.lineTo(373.25,302.50);
chocoears.lineTo(373.25,302.50);
chocoears.lineTo(373.25,302.50);
chocoears.lineTo(379.13,296.13);
chocoears.lineTo(379.13,296.13);
chocoears.lineTo(379.13,296.13);
chocoears.lineTo(384.25,290.13);
chocoears.lineTo(384.25,290.13);
chocoears.lineTo(384.25,290.13);
chocoears.lineTo(389.25,283.88);
chocoears.lineTo(389.25,283.88);
chocoears.lineTo(389.25,283.88);
chocoears.lineTo(393.25,279.50);
chocoears.lineTo(393.25,279.50);
chocoears.lineTo(393.25,279.50);
chocoears.lineTo(396.63,275.50);
chocoears.lineTo(396.63,275.50);
chocoears.lineTo(396.63,275.50);
chocoears.lineTo(400.50,271.38);
chocoears.lineTo(400.50,271.38);
chocoears.lineTo(400.50,271.38);
chocoears.lineTo(406.25,266.75);
chocoears.lineTo(406.25,266.75);
chocoears.lineTo(406.25,266.75);
chocoears.lineTo(411.50,262.13);
chocoears.lineTo(411.50,262.13);
chocoears.lineTo(411.50,262.13);
chocoears.lineTo(414.88,259.75);
chocoears.lineTo(414.88,259.75);
chocoears.lineTo(414.88,259.75);
chocoears.lineTo(420.25,256.63);
chocoears.lineTo(420.25,256.63);
chocoears.lineTo(420.25,256.63);
chocoears.lineTo(424.63,253.88);
chocoears.lineTo(424.63,253.88);
chocoears.lineTo(424.63,253.88);
chocoears.lineTo(427.50,252.88);
chocoears.lineTo(427.50,252.88);
chocoears.lineTo(427.50,252.88);
chocoears.lineTo(431.13,251.50);
chocoears.lineTo(431.13,251.50);
chocoears.lineTo(431.13,251.50);
chocoears.lineTo(434.25,251.13);
chocoears.lineTo(434.25,251.13);
chocoears.lineTo(434.25,251.13);
chocoears.lineTo(437.63,251.00);
chocoears.lineTo(437.63,251.00);
chocoears.lineTo(437.63,251.00);
chocoears.lineTo(442.50,252.25);
chocoears.lineTo(442.50,252.25);
chocoears.lineTo(442.50,252.25);
chocoears.lineTo(445.50,253.75);
chocoears.lineTo(445.50,253.75);
chocoears.lineTo(445.50,253.75);
chocoears.lineTo(447.88,255.25);
chocoears.lineTo(447.88,255.25);
chocoears.lineTo(447.88,255.25);
chocoears.lineTo(450.50,258.25);
chocoears.lineTo(450.50,258.25);
chocoears.lineTo(450.50,258.25);
chocoears.lineTo(451.88,260.75);
chocoears.lineTo(451.88,260.75);
chocoears.lineTo(451.88,260.75);
chocoears.lineTo(453.38,264.13);
chocoears.lineTo(453.38,264.13);
chocoears.lineTo(453.38,264.13);
chocoears.lineTo(454.25,266.88);
chocoears.lineTo(454.25,266.88);
chocoears.lineTo(454.25,266.88);
chocoears.lineTo(455.50,271.13);
chocoears.lineTo(455.50,271.13);
chocoears.lineTo(455.50,271.13);
chocoears.lineTo(455.63,274.75);
chocoears.lineTo(455.63,274.75);
chocoears.lineTo(455.63,274.75);
chocoears.lineTo(456.75,276.50);
chocoears.lineTo(456.75,276.50);
chocoears.lineTo(456.75,276.50);
chocoears.lineTo(458.63,278.00);
chocoears.lineTo(458.63,278.00);
chocoears.lineTo(458.63,278.00);
chocoears.lineTo(460.00,278.88);
chocoears.lineTo(460.00,278.88);
chocoears.lineTo(460.00,278.88);
chocoears.lineTo(461.00,280.75);
chocoears.lineTo(461.00,280.75);
chocoears.lineTo(461.00,280.75);
chocoears.lineTo(462.75,281.00);
chocoears.lineTo(462.75,281.00);
chocoears.lineTo(462.75,281.00);
chocoears.lineTo(464.88,281.88);
chocoears.lineTo(464.88,281.88);
chocoears.lineTo(464.88,281.88);
chocoears.lineTo(467.38,283.00);
chocoears.lineTo(467.38,283.00);
chocoears.lineTo(467.38,283.00);
chocoears.lineTo(470.13,284.50);
chocoears.lineTo(470.13,284.50);
chocoears.lineTo(470.13,284.50);
chocoears.lineTo(472.00,286.13);
chocoears.lineTo(472.00,286.13);
chocoears.closePath();
g2.draw(chocoears);
Color milkChocolate = new Color(111,68,51);
g2.setColor(milkChocolate);
g2.fill(chocoears);
//Grass
Rectangle grass = new Rectangle(0,525,getWidth(),100);
Color lawnGreen = new Color(124,252,0);
g2.setColor(lawnGreen);
g2.fill(grass);
List<Arc2D> blades = new ArrayList<Arc2D>();
for (int x = 0; x < getWidth(); x += 10) {
blades.add(new Arc2D.Double(x, 500, 10, 35, 105, 180, Arc2D.OPEN));
blades.add(new Arc2D.Double(x - 5, 510, 10, 35, 105, 180, Arc2D.OPEN));
blades.add(new Arc2D.Double(x, 520, 10, 35, 105, 180, Arc2D.OPEN));
}
Color yellowGreen = new Color(107, 142, 35);
g2.setColor(yellowGreen);
for (Shape blade : blades) {
g2.draw(blade);
}
Ellipse2D.Double circle = new Ellipse2D.Double(60,100,25,25);
g2.setColor(Color.RED);
g2.fill(circle);
Rectangle box = new Rectangle(150,100,20,80);
g2.setColor(Color.YELLOW);
g2.fill(box);
}
}
Whatever you do I would
create the Path2D objects outside of the paintComponent() method.
add each Path2D object to an ArrayList
in the paintComponent() method you can iterate through the ArrayList to paint each Path2D
Or another approach is to just have a method that:
creates each Path2D object when the class is created
then you paint the Path2D object to a BufferedImage
now that you have a BufferedImage you can create an ImageIcon and Just use a JLabel in you app and you don't need the custom panel.
Heres 2 options I would consider for cleaning up the pathing.
Use SVG, one library I know is Batik found at: http://xmlgraphics.apache.org/batik/
Once I did a project where the paths I needed would be created in photoshop then exported to Illustrator. The file format was each action was line delineated, each line was space delineated and consisted of a set of points with the last entry the command name. Here was a sample input.
10 10 m
15 15 l
10 10 12 12 v
As you can see you just read the file, create your path and do an if test for the last letter on the line and use the appropriate command with for the points. Of course you can make up your own format or procedures but it sure was nice using photoshop for me :)

LineBreakMeasurer produces result differ from MS Word / LibreOffice

In a swing application, I need to foresee text wrapping of a string like when putting it in a word processor program such as MS Word or LibreOffice. Providing the same width of the displayable area, the same font (face and size) and the same string as following:
displayable area width: 179mm (in a .doc file, setup an A4 portrait page - width = 210mm, margin left = 20mm, right = 11mm; the paragraph is formatted with zero margins)
Font Times New Roman, size 14
Test string: Tadf fdas fdas daebjnbvx dasf opqwe dsa: dfa fdsa ewqnbcmv caqw vstrt vsip d asfd eacc
And the result:
On both MS Word and LibreOffice, that test string is displayed on single line, no text wrapping occurs.
My bellow program report a text wrapping occurs, 2 lines
Line 1: Tadf fdas fdas daebjnbvx dasf opqwe dsa: dfa fdsa ewqnbcmv caqw vstrt vsip d asfd
Line 2: eacc
Is it possible to achieve the same text wrapping effect like MS Word in swing? What could be wrong in the code?
Bellow the my program
public static List<String> wrapText(String text, float maxWidth,
Graphics2D g, Font displayFont) {
// Normalize the graphics context so that 1 point is exactly
// 1/72 inch and thus fonts will display at the correct sizes:
GraphicsConfiguration gc = g.getDeviceConfiguration();
g.transform(gc.getNormalizingTransform());
AttributedCharacterIterator paragraph = new AttributedString(text).getIterator();
Font backupFont = g.getFont();
g.setFont(displayFont);
LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(
paragraph, BreakIterator.getWordInstance(), g.getFontRenderContext());
// Set position to the index of the first character in the paragraph.
lineMeasurer.setPosition(paragraph.getBeginIndex());
List<String> lines = new ArrayList<String>();
int beginIndex = 0;
// Get lines until the entire paragraph has been displayed.
while (lineMeasurer.getPosition() < paragraph.getEndIndex()) {
lineMeasurer.nextLayout(maxWidth);
lines.add(text.substring(beginIndex, lineMeasurer.getPosition()));
beginIndex = lineMeasurer.getPosition();
}
g.setFont(backupFont);
return lines;
}
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane txtp = new JTextPane();
frame.add(txtp);
frame.setSize(200,200);
frame.setVisible(true);
Font displayFont = new Font("Times New Roman", Font.PLAIN, 14);
float textWith = (179 * 0.0393701f) // from Millimeter to Inch
* 72f; // From Inch to Pixel (User space)
List<String> lines = wrapText(
"Tadf fdas fdas daebjnbvx dasf opqwe dsa: dfa fdsa ewqnbcmv caqw vstrt vsip d asfd eacc",
textWith,
(Graphics2D) txtp.getGraphics(),
displayFont);
for (int i = 0; i < lines.size(); i++) {
System.out.print("Line " + (i + 1) + ": ");
System.out.println(lines.get(i));
}
frame.dispose();
}
+1 for the question
From my experience with text editors it's not possible to achieve exactly the same measuring.
You can try to play with DPI there is default DPI=72 and 96 on windows.
Also you can try to play with all the rendering hints of the Graphics - text antialiasing etc.

Motion detection on IP video input stream - Android/Java

As described in post title, I'm looking for a way to detect motion/movement on the input stream from CCTV camera (IP/WiFi). Anyone know best way how I can connect to IP video stream and monitor for motion?
this is the opencv code in python, java is simiar, you need use opencv for the image operation
import cv2, time, pandas
from datetime import datetime
first_frame = None
status_list = [None,None]
times = []
df=pandas.DataFrame(columns=["Start","End"])
video = cv2.VideoCapture('rtsp://admin:Paxton10#10.199.27.128:554')
while True:
check, frame = video.read()
status = 0
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(21,21),0)
if first_frame is None:
first_frame=gray
continue
delta_frame=cv2.absdiff(first_frame,gray)
thresh_frame=cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
thresh_frame=cv2.dilate(thresh_frame, None, iterations=2)
(cnts,_)=cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
if cv2.contourArea(contour) < 200000:
continue
status=1
(x, y, w, h)=cv2.boundingRect(contour)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0), 3)
status_list.append(status)
status_list=status_list[-2:]
if status_list[-1]==1 and status_list[-2]==0:
times.append(datetime.now())
if status_list[-1]==0 and status_list[-2]==1:
times.append(datetime.now())
#cv2.imshow("Gray Frame",gray)
#cv2.imshow("Delta Frame",delta_frame)
imS = cv2.resize(thresh_frame, (640, 480))
cv2.imshow("Threshold Frame",imS)
imS = cv2.resize(frame, (640, 480))
cv2.imshow("Color Frame",imS)
#cv2.imshow("Color Frame",frame)
key=cv2.waitKey(1)
if key == ord('q'):
if status == 1:
times.append(datetime.now())
break
print(status_list)
print(times)
for i in range(0, len(times), 2):
df = df.append({"Start": times[i],"End": times[i+1]}, ignore_index=True)
df.to_csv("Times.csv")
video.release()
cv2.destroyAllWindows

Categories

Resources