Adding new margin class, added tests, let colorbar width be scaled with pixel ratio

This commit is contained in:
Martin Pernollet 2022-06-02 15:10:38 +02:00
parent bd4baa7fd1
commit 4eba82fd98
25 changed files with 393 additions and 159 deletions

View File

@ -64,8 +64,8 @@ public class AWTColorbarImageGenerator extends AWTAbstractImageGenerator
/** Renders the colorbar to an image. */
public BufferedImage toImage(int width, int height, int barWidth) {
if (barWidth > width)
return null;
//if (barWidth > width)
// return null;
this.barWidth = barWidth;
@ -77,9 +77,9 @@ public class AWTColorbarImageGenerator extends AWTAbstractImageGenerator
configureText(graphic);
drawBackground(width, height, graphic);
drawBarColors(height, this.barWidth/*getScaledBarWidth()*/, graphic);
drawBarContour(height, this.barWidth/*getScaledBarWidth()*/, graphic);
drawTextAnnotations(height, this.barWidth/*getScaledBarWidth()*/, graphic);
drawBarColors(height, getScaledBarWidth(), graphic);
drawBarContour(height, getScaledBarWidth(), graphic);
drawTextAnnotations(height, getScaledBarWidth(), graphic);
return image;
}
@ -97,7 +97,7 @@ public class AWTColorbarImageGenerator extends AWTAbstractImageGenerator
protected void drawBarContour(int height, int barWidth, Graphics2D graphic) {
int finalY = 0;
int finalH = height;
int finalH = height-1; // let bottom contour appear in the image
// add little space to avoid cutting the text
// on top and bottom of colorbar
@ -120,7 +120,7 @@ public class AWTColorbarImageGenerator extends AWTAbstractImageGenerator
protected void drawBarColors(int height, int barWidth, Graphics2D graphic) {
int finalFrom = 0;
int finalTo = height;
int finalTo = height-1;
// add little space to avoid cutting the text
// on top and bottom of colorbar

View File

@ -81,7 +81,8 @@ public abstract class AWTLegend extends AWTImageViewport implements IDrawableLis
if (imageWidth != imgWidth || imageHeight != height) {
//System.out.println("AWTLegend width:" + imgWidth + " height:" + height + " mode:" + getViewportMode());
//System.out.println("AWTLegend left:" + left + " right:" + right);
//System.out.println("AWTLegend imgWidth:" + imgWidth + " width:" + width + " height:" + height + " mode:" + getViewportMode());
setImage(toImage(imgWidth, height));
}
}

View File

@ -8,6 +8,7 @@ import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.IMultiColorable;
import org.jzy3d.maths.Coord2d;
import org.jzy3d.maths.Dimension;
import org.jzy3d.maths.Margin;
import org.jzy3d.painters.Font;
import org.jzy3d.painters.IPainter;
import org.jzy3d.plot2d.primitive.AWTColorbarImageGenerator;
@ -108,7 +109,7 @@ public class AWTColorbarLegend extends AWTLegend implements IColorbarLegend {
this.minimumDimension = new Dimension(AWTColorbarImageGenerator.MIN_BAR_WIDTH,
AWTColorbarImageGenerator.MIN_BAR_HEIGHT);
this.margin.height = DEFAULT_MARGIN_HEIGHT;
this.margin.setHeight(DEFAULT_MARGIN_HEIGHT);
initImageGenerator(parent, provider, renderer);
}
@ -146,7 +147,7 @@ public class AWTColorbarLegend extends AWTLegend implements IColorbarLegend {
*
* If running on a HiDPI screen, width and height parameter will grow.
*/
protected BufferedImage toImage(int width, int height, Dimension margin, Coord2d pixelScale) {
protected BufferedImage toImage(int width, int height, Margin margin, Coord2d pixelScale) {
if (imageGenerator != null) {
setGeneratorColors();
@ -162,12 +163,12 @@ public class AWTColorbarLegend extends AWTLegend implements IColorbarLegend {
// which leads to a visually thinner bar a smaller tick labels for the colorbar
if(usePixelScale) {
choosenWidth = (int)((width - margin.width) * pixelScale.x);
choosenHeight = (int)((height - margin.height) * pixelScale.y);
choosenWidth = (int)((width - margin.getWidth()) * pixelScale.x);
choosenHeight = (int)((height - margin.getHeight()) * pixelScale.y);
}
else {
choosenWidth = (int) (width - margin.width);
choosenHeight = (int) (height - margin.height);
choosenWidth = (int) (width - margin.getWidth());
choosenHeight = (int) (height - margin.getHeight());
}
//System.out.println("AWTColorbarLegend : asked.w:" + width + " asked.h:" + height + " m.w:" + margin.width + " m.h:" + margin.height + " pixScale:" + pixelScale);
@ -190,7 +191,7 @@ public class AWTColorbarLegend extends AWTLegend implements IColorbarLegend {
* Update image according to new margin.
*/
@Override
public void setMargin(Dimension margin) {
public void setMargin(Margin margin) {
super.setMargin(margin);
if(getImageGenerator()!=null) {

View File

@ -1,11 +1,11 @@
package org.jzy3d.plot3d.rendering.legends.overlay;
import org.jzy3d.maths.Margin;
public class LegendLayout {
/** External margin : distance between legend border canvas border. */
protected int boxMarginX = 5;
/** External margin : distance between legend border canvas border. */
protected int boxMarginY = 5;
protected Margin margin = new Margin(10,10);
/** Legend position. */
protected Corner corner = Corner.TOP_LEFT;
@ -13,20 +13,12 @@ public class LegendLayout {
TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT
}
public int getBoxMarginX() {
return boxMarginX;
public Margin getMargin() {
return margin;
}
public void setBoxMarginX(int boxMarginX) {
this.boxMarginX = boxMarginX;
}
public int getBoxMarginY() {
return boxMarginY;
}
public void setBoxMarginY(int boxMarginY) {
this.boxMarginY = boxMarginY;
public void setMargin(Margin margin) {
this.margin = margin;
}
public Corner getCorner() {

View File

@ -51,18 +51,29 @@ public class OverlayLegendRenderer extends AbstractAWTRenderer2d implements AWTR
int textWidthMax = maxStringWidth(fm);
// Box dimensions
int xBoxPos = layout.boxMarginX;
int yBoxPos = layout.boxMarginY;
int boxWidth = layout.txtMarginX + textWidthMax + layout.sampleLineMargin
+ layout.sampleLineLength + layout.txtMarginX;
int boxHeight = layout.txtMarginY + (textHeight + layout.txtInterline) * (info.size() - 1)
+ textHeight + layout.txtMarginY;
// Box offset to apply margins
// horizontal left
int xBoxPos = Math.round(layout.getMargin().getLeft());
// horizontal right
if (Corner.TOP_RIGHT.equals(layout.corner) || Corner.BOTTOM_RIGHT.equals(layout.corner)) {
xBoxPos = canvasWidth - layout.boxMarginX - boxWidth;
xBoxPos = Math.round(canvasWidth - layout.getMargin().getRight() - boxWidth);
}
// vertical top
int yBoxPos = Math.round(layout.getMargin().getTop());
// vertical bottom
if (Corner.BOTTOM_LEFT.equals(layout.corner) || Corner.BOTTOM_RIGHT.equals(layout.corner)) {
yBoxPos = canvasHeight - layout.boxMarginY - boxHeight;
yBoxPos = Math.round(canvasHeight - layout.getMargin().getBottom() - boxHeight);
}
// Background

View File

@ -38,17 +38,17 @@ public class AWTImageRenderer extends AbstractAWTRenderer2d implements AWTRender
int y = 0;
if (Corner.TOP_LEFT.equals(layout.getCorner())) {
x = layout.getBoxMarginX();
y = layout.getBoxMarginY();
x = Math.round(layout.getMargin().getLeft());
y = Math.round(layout.getMargin().getTop());
} else if (Corner.TOP_RIGHT.equals(layout.getCorner())) {
x = canvasWidth - imageWidth - layout.getBoxMarginX();
y = layout.getBoxMarginY();
x = Math.round(canvasWidth - imageWidth - layout.getMargin().getRight());
y = Math.round(layout.getMargin().getTop());
} else if (Corner.BOTTOM_LEFT.equals(layout.getCorner())) {
x = layout.getBoxMarginX();
y = canvasHeight - imageHeight - layout.getBoxMarginY();
x = Math.round(layout.getMargin().getLeft());
y = Math.round(canvasHeight - imageHeight - layout.getMargin().getBottom());
} else if (Corner.BOTTOM_RIGHT.equals(layout.getCorner())) {
x = canvasWidth - imageWidth - layout.getBoxMarginX();
y = canvasHeight - imageHeight - layout.getBoxMarginY();
x = Math.round(canvasWidth - imageWidth - layout.getMargin().getRight());
y = Math.round(canvasHeight - imageHeight - layout.getMargin().getBottom());
}

View File

@ -1,10 +1,12 @@
package org.jzy3d.plot3d.rendering.view;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import org.jzy3d.maths.Coord2d;
import org.jzy3d.maths.Coord3d;
import org.jzy3d.maths.Dimension;
import org.jzy3d.maths.Margin;
import org.jzy3d.painters.IPainter;
import org.jzy3d.plot3d.rendering.image.AWTImageConvert;
@ -21,7 +23,7 @@ public class AWTImageViewport extends AbstractViewportManager implements IImageV
protected int imageHeight;
protected int imageWidth;
protected Dimension margin = new Dimension(0,0); // no margin by default
protected Margin margin = new Margin(0,0,0,0); // no margin by default
protected Coord2d pixelScale = new Coord2d(1,1); // assume default pixel scale
public AWTImageViewport() {
@ -132,11 +134,11 @@ public class AWTImageViewport extends AbstractViewportManager implements IImageV
return new Dimension(0, 0);
}
public Dimension getMargin() {
public Margin getMargin() {
return margin;
}
public void setMargin(Dimension margin) {
public void setMargin(Margin margin) {
this.margin = margin;
}
}

View File

@ -42,18 +42,19 @@ public class AWTShapeRenderer extends AbstractAWTRenderer2d implements AWTRender
int y = 0;
if (Corner.TOP_LEFT.equals(layout.getCorner())) {
x = layout.getBoxMarginX() + shapeWidth/2;
y = layout.getBoxMarginY();
x = Math.round(layout.getMargin().getLeft());
y = Math.round(layout.getMargin().getTop());
} else if (Corner.TOP_RIGHT.equals(layout.getCorner())) {
x = canvasWidth - shapeWidth/2 - layout.getBoxMarginX();
y = layout.getBoxMarginY();
x = Math.round(canvasWidth - shapeWidth/2 - layout.getMargin().getRight());
y = Math.round(layout.getMargin().getTop());
} else if (Corner.BOTTOM_LEFT.equals(layout.getCorner())) {
x = layout.getBoxMarginX() + shapeWidth/2;
y = canvasHeight - shapeHeight/2 - layout.getBoxMarginY();
x = Math.round(layout.getMargin().getLeft());
y = Math.round(canvasHeight - shapeHeight/2 - layout.getMargin().getBottom());
} else if (Corner.BOTTOM_RIGHT.equals(layout.getCorner())) {
x = canvasWidth - shapeWidth/2 - layout.getBoxMarginX();
y = canvasHeight - shapeHeight/2 - layout.getBoxMarginY();
x = Math.round(canvasWidth - shapeWidth/2 - layout.getMargin().getRight());
y = Math.round(canvasHeight - shapeHeight/2 - layout.getMargin().getBottom());
}
g2d.setColor(AWTColor.toAWT(color));
g2d.translate(x, y);

View File

@ -0,0 +1,50 @@
package org.jzy3d.plot2d.primitive;
import org.junit.Assert;
import org.junit.Test;
import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.colormaps.ColorMapRainbow;
import org.jzy3d.maths.Coord2d;
import org.jzy3d.plot3d.primitives.axis.layout.providers.ITickProvider;
import org.jzy3d.plot3d.primitives.axis.layout.providers.RegularTickProvider;
import org.jzy3d.plot3d.primitives.axis.layout.renderers.DefaultDecimalTickRenderer;
import org.jzy3d.plot3d.primitives.axis.layout.renderers.ITickRenderer;
public class TestAWTColorbarImageGenerator {
@Test
public void whenPixelScaleLargerThan1_ThenBarWidthIsScaled() {
// Given
ColorMapper mapper = new ColorMapper(new ColorMapRainbow(), -1, 1);
ITickRenderer renderer = new DefaultDecimalTickRenderer();
ITickProvider provider = new RegularTickProvider();
AWTColorbarImageGenerator gen = new AWTColorbarImageGenerator(mapper, provider, renderer);
//gen.setFont(Font.Helvetica_18);
// When
int scale = 1;
int barWidth = 30;
gen.setPixelScale(new Coord2d(scale, scale));
gen.toImage(1, 1, barWidth);
// Then
Assert.assertEquals(barWidth, gen.getScaledBarWidth());
//gen.g
// When
scale = 2;
gen.setPixelScale(new Coord2d(scale, scale));
gen.toImage(1, 1, barWidth);
// Then
Assert.assertEquals(barWidth * scale, gen.getScaledBarWidth());
}
}

View File

@ -3,8 +3,7 @@ package org.jzy3d.plot3d.rendering.legends.colorbars;
import org.junit.Assert;
import org.junit.Test;
import org.jzy3d.maths.Coord2d;
import org.jzy3d.maths.Dimension;
import org.jzy3d.plot2d.primitive.AWTColorbarImageGenerator;
import org.jzy3d.maths.Margin;
import org.jzy3d.plot3d.primitives.SampleGeom;
import org.jzy3d.plot3d.primitives.axis.layout.AxisLayout;
@ -30,9 +29,8 @@ public class TestAWTColorbarLegend {
// When setting viewport to a size and having a non 0 margin
Dimension margin = legend.getMargin();
margin.width = 2;
margin.height = 2;
Margin margin = legend.getMargin();
margin.set(2,2); // 1 pixel margin each side
int width = 300;
int height = 600;
@ -43,7 +41,7 @@ public class TestAWTColorbarLegend {
// Then actual image width is smaller according to horizontal margin
int actualImageWidth = legend.getImage().getWidth(null);
int expectWidth = (int) (width * (right - left)) - margin.width;
int expectWidth = Math.round(width * (right - left) - margin.getWidth());
Assert.assertEquals(expectWidth, actualImageWidth, DELTA);
// ------------------------

View File

@ -0,0 +1,125 @@
package org.jzy3d.maths;
import java.util.Objects;
public class Margin {
protected float left = 0;
protected float right = 0;
protected float top = 0;
protected float bottom = 0;
public Margin() {
}
public Margin(float width, float height) {
set(width, height);
}
public Margin(float left, float right, float top, float bottom) {
set(left, right, top, bottom);
}
/**
* Set left and right margin to width/2, top and bottom margin to height/2.
*/
public void set(float width, float height) {
setWidth(width);
setHeight(height);
}
public void set(float left, float right, float top, float bottom) {
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
}
/**
* Set top and bottom margin to height/2.
*/
public void setHeight(float height) {
setTop(height/2);
setBottom(height/2);
}
/**
* Set left and right margin to width/2.
*/
public void setWidth(float width) {
setLeft(width/2);
setRight(width/2);
}
/**
* Return the sum of left and right margin
*/
public float getWidth() {
return left+right;
}
/**
* Return the sum of left and right margin
*/
public float getHeight() {
return top+bottom;
}
public float getLeft() {
return left;
}
public void setLeft(float left) {
this.left = left;
}
public float getRight() {
return right;
}
public void setRight(float right) {
this.right = right;
}
public float getTop() {
return top;
}
public void setTop(float top) {
this.top = top;
}
public float getBottom() {
return bottom;
}
public void setBottom(float bottom) {
this.bottom = bottom;
}
public String toString() {
return "left:" + left + " right:" + right + " top:" + top + " bottom:" + bottom;
}
@Override
public int hashCode() {
return Objects.hash(bottom, left, right, top);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Margin other = (Margin) obj;
return Float.floatToIntBits(bottom) == Float.floatToIntBits(other.bottom)
&& Float.floatToIntBits(left) == Float.floatToIntBits(other.left)
&& Float.floatToIntBits(right) == Float.floatToIntBits(other.right)
&& Float.floatToIntBits(top) == Float.floatToIntBits(other.top);
}
}

View File

@ -35,10 +35,9 @@ public abstract class AbstractViewportManager {
protected boolean screenGridDisplayed = false;
protected ViewportMode mode = ViewportMode.RECTANGLE_NO_STRETCH;
protected float ratioWidth;
protected float ratioHeight;
protected ViewportConfiguration lastViewPort;
protected boolean apply_WindowsHiDPI_Workaround = true;
/**
@ -77,11 +76,11 @@ public abstract class AbstractViewportManager {
this.screenWidth = getSliceWidth(width, left, right);
this.screenHeight = height;
this.screenLeft = (int) (left * width);
this.screenBottom = 0;// screenLeft + screenWidth;
this.screenBottom = 0;
}
public int getSliceWidth(int width, float left, float right) {
return (int) (width * (right - left));
return Math.round(width * (right - left));
}
@ -96,7 +95,6 @@ public abstract class AbstractViewportManager {
return lastViewPort;
}
protected ViewportConfiguration lastViewPort;
/**
* Build and return a {@link ViewportConfiguration}. Uses gl to

View File

@ -1,5 +1,7 @@
package org.jzy3d.plot3d.rendering.view;
import org.apache.commons.collections4.Get;
import org.jzy3d.maths.Margin;
/**
* Allows configuring the layout of a view when the chart enters a 2D rendering mode.
@ -32,14 +34,7 @@ public class View2DLayout {
/** Distance between tick labels and axis label */
protected float yAxisLabelsDistance = 0;
/** Extra margin */
protected float marginLeft = 0;
/** Extra margin */
protected float marginRight = 0;
/** Extra margin */
protected float marginTop = 0;
/** Extra margin */
protected float marginBottom = 0;
protected Margin margin = new Margin();
public View2DLayout(View view) {
@ -67,59 +62,43 @@ public class View2DLayout {
setyAxisLabelsDistance(dist);
}
/**
* A convenient shortcut to set the same margin to left, right, bottom and right canvas borders.
*
* @see {@link #getMargin()}
*/
public void setMargin(float margin) {
setMarginHorizontal(margin);
setMarginVertical(margin);
}
/**
* Set the same margin to left and right canvas borders
* A convenient shortcut to set the same margin to left and right canvas borders.
*
* @see {@link #getMargin()}
*/
public void setMarginHorizontal(float margin) {
setMarginLeft(margin);
setMarginRight(margin);
this.margin.setWidth(margin*2);
}
/**
* Set the same margin to top and bottom canvas borders
* A convenient shortcut to set the same margin to top and bottom canvas borders.
*
* @see {@link #getMargin()}
*/
public void setMarginVertical(float margin) {
setMarginTop(margin);
setMarginBottom(margin);
this.margin.setHeight(margin*2);
}
public float getMarginLeft() {
return marginLeft;
public Margin getMargin() {
return margin;
}
public void setMarginLeft(float marginLeft) {
this.marginLeft = marginLeft;
public void setMargin(Margin margin) {
this.margin = margin;
}
public float getMarginRight() {
return marginRight;
}
public void setMarginRight(float marginRight) {
this.marginRight = marginRight;
}
public float getMarginTop() {
return marginTop;
}
public void setMarginTop(float marginTop) {
this.marginTop = marginTop;
}
public float getMarginBottom() {
return marginBottom;
}
public void setMarginBottom(float marginBottom) {
this.marginBottom = marginBottom;
}
public boolean isTextAddMargin() {
return textAddMargin;
}

View File

@ -81,11 +81,15 @@ public class View2DProcessing {
// ---------------------------------------------------
// initialize all margins according to configuration
marginLeftPx = view2DLayout.marginLeft;
marginRightPx = view2DLayout.marginRight;
marginTopPx = view2DLayout.marginTop;
marginBottomPx = view2DLayout.marginBottom;
/*marginLeftPx = view2DLayout.getMargin().getLeft() * pixelScale.x;
marginRightPx = view2DLayout.getMargin().getRight() * pixelScale.x;
marginTopPx = view2DLayout.getMargin().getTop() * pixelScale.y;
marginBottomPx = view2DLayout.getMargin().getBottom() * pixelScale.y;*/
marginLeftPx = view2DLayout.getMargin().getLeft();
marginRightPx = view2DLayout.getMargin().getRight();
marginTopPx = view2DLayout.getMargin().getTop();
marginBottomPx = view2DLayout.getMargin().getBottom();
// ---------------------------------------------------
// compute pixel occupation of ticks and axis labels

View File

@ -149,6 +149,9 @@ public class ViewAndColorbarsLayout implements IViewportLayout {
int height = canvas.getRendererHeight();
float theLeft = left + slice * (k++);
float theRight = left + slice * k;
//System.out.println("ViewAndColorbars : width:" + width + " height:" + height);
//System.out.println("ViewAndColorbars : TheLeft:" + theLeft + " TheRight:" + theRight);
legend.setFont(painter.getView().getAxis().getLayout().getFont());
legend.setViewportMode(ViewportMode.STRETCH_TO_FILL);

View File

@ -0,0 +1,36 @@
package org.jzy3d.maths;
import org.junit.Assert;
import org.junit.Test;
public class TestMargins {
@Test
public void set() {
Margin margin = new Margin();
margin.set(10, 20);
Assert.assertEquals(5, margin.getLeft(), 0);
Assert.assertEquals(5, margin.getRight(), 0);
Assert.assertEquals(10, margin.getTop(), 0);
Assert.assertEquals(10, margin.getBottom(), 0);
Assert.assertEquals(10, margin.getWidth(), 0);
Assert.assertEquals(20, margin.getHeight(), 0);
margin.setWidth(100);
Assert.assertEquals(50, margin.getLeft(), 0);
Assert.assertEquals(50, margin.getRight(), 0);
Assert.assertEquals(100, margin.getWidth(), 0);
margin.setHeight(200);
Assert.assertEquals(100, margin.getTop(), 0);
Assert.assertEquals(100, margin.getBottom(), 0);
Assert.assertEquals(200, margin.getHeight(), 0);
}
}

View File

@ -0,0 +1,14 @@
package org.jzy3d.plot3d.rendering.view;
import org.junit.Assert;
import org.junit.Test;
public class TestAbstractViewportManager {
@Test
public void testSliceWidth() {
AbstractViewportManager avm = new AbstractViewportManager() {};
// ensure we properly round the value
Assert.assertEquals(220, avm.getSliceWidth(1600, 0.8625f, 1.0f));
}
}

View File

@ -24,10 +24,13 @@ public class TestView2DProcessing {
int VIEWPORT_WIDTH = 1000;
int VIEWPORT_HEIGHT = VIEWPORT_WIDTH/2;
// scale
int pixScale = 1;
// layout
int MARGIN = 10;
int TICK_DIST = 20;
int AXIS_DIST = 30;
int MARGIN = 10; // distance to canvas border
int TICK_DIST = 20; // tick distance to axis border
int AXIS_DIST = 30; // axis label distance to tick label
String yAxisLabel = "yLabel_100px_Width";
@ -51,7 +54,6 @@ public class TestView2DProcessing {
View view = Mocks.View(Mocks.Axis(axisLayout), painter, Mocks.Canvas(true));
View2DLayout layout = new View2DLayout(view);
layout.setMargin(MARGIN);
layout.setTickLabelDistance(TICK_DIST);
@ -65,8 +67,9 @@ public class TestView2DProcessing {
View2DProcessing processing = new View2DProcessing(view);
// ----------------------------------------
// When processing margins with a vertical Y AXIS
// When processing margins with a vertical Y AXIS, pixel scale = 1
when(view.getPixelScale()).thenReturn(new Coord2d(pixScale,pixScale));
when(axisLayout.getYAxisLabelOrientation()).thenReturn(LabelOrientation.VERTICAL);
ViewportConfiguration viewport = new ViewportConfiguration(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, 0, 0);
@ -75,7 +78,7 @@ public class TestView2DProcessing {
processing.apply(viewport, bounds);
// ----------------------------------------
// Then
// Then margin appropriately processed
Coord2d ratio = new Coord2d(0.02265006, 0.049261093);
@ -89,8 +92,9 @@ public class TestView2DProcessing {
Assert.assertEquals(xpectHeight, processing.getMargin().height, 0.1);
// ----------------------------------------
// When processing margins with a horizontal Y AXIS
// When processing margins with a horizontal Y AXIS, pixel scale = 1
when(view.getPixelScale()).thenReturn(new Coord2d(pixScale,pixScale));
when(axisLayout.getYAxisLabelOrientation()).thenReturn(LabelOrientation.HORIZONTAL);
processing.apply(viewport, bounds);
@ -148,7 +152,8 @@ public class TestView2DProcessing {
// Given a view with settings
View view = Mocks.View(Mocks.Axis(axisLayout), painter, Mocks.Canvas(true));
when(view.getPixelScale()).thenReturn(new Coord2d(1,1));
View2DLayout layout = new View2DLayout(view);
layout.setMargin(MARGIN);
layout.setTickLabelDistance(TICK_DIST);
@ -217,7 +222,8 @@ public class TestView2DProcessing {
// Given a view with settings
View view = Mocks.View(Mocks.Axis(axisLayout), painter, Mocks.Canvas(true));
when(view.getPixelScale()).thenReturn(new Coord2d(1,1));
View2DLayout layout = new View2DLayout(view);
layout.setMargin(MARGIN);
layout.setTickLabelDistance(TICK_DIST);
@ -242,7 +248,7 @@ public class TestView2DProcessing {
// ----------------------------------------
// Then
System.out.println(processing.getModelToScreen());
//System.out.println(processing.getModelToScreen());
Assert.assertTrue(Float.isFinite(processing.getModelToScreen().x));
Assert.assertTrue(Float.isFinite(processing.getModelToScreen().y));

View File

@ -22,7 +22,17 @@ public class EmulGLChartFactory extends ChartFactory {
}
@Override
public Chart newChart(IChartFactory factory, Quality quality) {
public AWTChart newChart() {
return newChart(Quality.Advanced());
}
@Override
public AWTChart newChart(Quality quality) {
return newChart(getFactory(), quality);
}
@Override
public AWTChart newChart(IChartFactory factory, Quality quality) {
AWTChart chart = new AWTChart(factory, quality);
chart.getView().setBoundMode(ViewBoundMode.AUTO_FIT); // EMULGL NEEDS AUTO_FIT!!!
return chart;

View File

@ -101,10 +101,10 @@ public class EmulGLViewAndColorbarsLayout extends ViewAndColorbarsLayout {
// Processing yoffset with pixel scale
int yOffset = 0;
if (pixelScale.y == 1) {
yOffset = (int) (awtLegend.getMargin().height / 2f);
yOffset = (int) (awtLegend.getMargin().getHeight() / 2f);
} else {
yOffset =
(int) ((pixelScale.y - 1) * awtLegend.getMargin().height) / 2;
(int) ((pixelScale.y - 1) * awtLegend.getMargin().getHeight()) / 2;
}
// ---------------------------------------

View File

@ -6,7 +6,7 @@ import java.io.IOException;
import javax.imageio.ImageIO;
import org.junit.Assert;
import org.jzy3d.maths.Coord2d;
import org.jzy3d.maths.Dimension;
import org.jzy3d.maths.Margin;
import org.jzy3d.plot3d.primitives.SampleGeom;
import org.jzy3d.plot3d.primitives.axis.layout.AxisLayout;
@ -16,7 +16,7 @@ public class TrialAWTColorbarLegend {
AWTColorbarLegend colorbar = new AWTColorbarLegend(SampleGeom.surface(), new AxisLayout());
// When query image with margin and pixel scale
Dimension margin = new Dimension(20,240);
Margin margin = new Margin(20,240);
Coord2d pixelScale = new Coord2d(2,2);
int width = 300;
int height = 600;
@ -24,8 +24,8 @@ public class TrialAWTColorbarLegend {
BufferedImage i = colorbar.toImage(width, height, margin, pixelScale);
// Then image is smaller than queried, due to margin
Assert.assertEquals(width - margin.width, i.getWidth(null));
Assert.assertEquals(height - margin.height, i.getHeight(null));
Assert.assertEquals(width - margin.getWidth(), i.getWidth(null), 0);
Assert.assertEquals(height - margin.getHeight(), i.getHeight(null), 0);
ImageIO.write(i, "png", new File("target/colorbar.png"));

View File

@ -96,10 +96,10 @@ public class View2DLayout_Debug extends AbstractAWTRenderer2d implements AWTRend
int x;
int y;
// under axis label
y = height-(int)layout.getMarginBottom();
y = height-(int)layout.getMargin().getBottom();
g2d.drawLine(0, y, width, y);
x = (int)layout.getMarginLeft();
x = (int)layout.getMargin().getLeft();
String info = "Axis Label Bottom";
g2d.drawString(info, x, y);
@ -107,7 +107,7 @@ public class View2DLayout_Debug extends AbstractAWTRenderer2d implements AWTRend
// up to axis label
y = height-(int)(layout.getMarginBottom()+txtSize);
y = height-(int)(layout.getMargin().getBottom()+txtSize);
g2d.drawLine(0, y, width, y);
info = "Axis Label Top";
@ -115,7 +115,7 @@ public class View2DLayout_Debug extends AbstractAWTRenderer2d implements AWTRend
shift += fm.stringWidth(info);
// down to tick label
y = height-(int)(layout.getMarginBottom()+txtSize+layout.getxAxisLabelsDistance());
y = height-(int)(layout.getMargin().getBottom()+txtSize+layout.getxAxisLabelsDistance());
g2d.drawLine(0, y, width, y);
info = "Tick Label Bottom";
@ -123,7 +123,7 @@ public class View2DLayout_Debug extends AbstractAWTRenderer2d implements AWTRend
shift += fm.stringWidth(info);
// up to tick label
y = height-(int)(layout.getMarginBottom()+txtSize+layout.getxAxisLabelsDistance()+txtSize);
y = height-(int)(layout.getMargin().getBottom()+txtSize+layout.getxAxisLabelsDistance()+txtSize);
g2d.drawLine(0, y, width, y);
info = "Tick Label Top";
@ -131,7 +131,7 @@ public class View2DLayout_Debug extends AbstractAWTRenderer2d implements AWTRend
shift += fm.stringWidth(info);
// up to tick label margin
y = height-(int)(layout.getMarginBottom()+txtSize+layout.getxAxisLabelsDistance()+txtSize+layout.getxTickLabelsDistance());
y = height-(int)(layout.getMargin().getBottom()+txtSize+layout.getxAxisLabelsDistance()+txtSize+layout.getxTickLabelsDistance());
g2d.drawLine(0, y, width, y);
info = "Chart bottom border";
@ -139,7 +139,7 @@ public class View2DLayout_Debug extends AbstractAWTRenderer2d implements AWTRend
shift += fm.stringWidth(info);
// Horizontal top margin
y = (int)layout.getMarginTop();
y = (int)layout.getMargin().getTop();
g2d.drawLine(0, y, width, y);
info = "Chart top border";
@ -154,13 +154,13 @@ public class View2DLayout_Debug extends AbstractAWTRenderer2d implements AWTRend
int x;
// left to axis label
x = (int)layout.getMarginLeft();
x = (int)layout.getMargin().getLeft();
g2d.drawLine(x, 0, x, height);
g2d.drawString("Axis Label Left Side / Left Margin (" + x + ")", x, lineHeight*1);
// right to axis label
x = (int)(layout.getMarginLeft()+view.get2DProcessing().getAxisTextHorizontal());
x = (int)(layout.getMargin().getLeft()+view.get2DProcessing().getAxisTextHorizontal());
g2d.drawLine(x, 0, x, height);
g2d.drawString("Axis Label Right Side (" + x + ")", x, lineHeight*2);
@ -185,7 +185,7 @@ public class View2DLayout_Debug extends AbstractAWTRenderer2d implements AWTRend
g2d.drawString("Chart left border (" + x + ")", x, lineHeight*5);
x = (int)(view.getCamera().getLastViewPort().getWidth()-layout.getMarginRight());
x = (int)(view.getCamera().getLastViewPort().getWidth()-layout.getMargin().getRight());
g2d.drawLine(x, 0, x, height);

View File

@ -76,8 +76,8 @@ public class ITTest_Overlay extends ITTest {
infos.add(new Legend(line3.getName(), Color.GREEN));
OverlayLegendRenderer legend = new OverlayLegendRenderer(infos);
legend.getLayout().setBoxMarginX(10);
legend.getLayout().setBoxMarginY(10);
legend.getLayout().getMargin().setWidth(10);
legend.getLayout().getMargin().setHeight(10);
legend.getLayout().setBackgroundColor(Color.WHITE);
legend.getLayout().setFont(new java.awt.Font("Helvetica", java.awt.Font.PLAIN, 11));
chart.addRenderer(legend);

View File

@ -3,6 +3,8 @@ package org.jzy3d.tests.manual.layout;
import java.awt.image.BufferedImage;
import org.jzy3d.chart.AWTChart;
import org.jzy3d.chart.factories.AWTChartFactory;
import org.jzy3d.chart.factories.ChartFactory;
import org.jzy3d.chart.factories.EmulGLChartFactory;
import org.jzy3d.colors.Color;
import org.jzy3d.debug.View2DLayout_Debug;
import org.jzy3d.events.IViewLifecycleEventListener;
@ -38,13 +40,15 @@ public class MTest_Layout_Native_Open {
static final float ALPHA_FACTOR = 0.55f;// .61f;
public static void main(String[] args) throws InterruptedException {
new MTest_Layout_Native_Open().go();
AWTChartFactory factory = new AWTChartFactory();
//EmulGLChartFactory factory = new EmulGLChartFactory();
new MTest_Layout_Native_Open().go(factory);
}
private void go() throws InterruptedException {
AWTChartFactory factory = new AWTChartFactory();
private void go(ChartFactory factory) throws InterruptedException {
Quality q = Quality.Advanced();
q.setAnimated(false);
q.setHiDPIEnabled(true);
@ -55,10 +59,10 @@ public class MTest_Layout_Native_Open {
Shape surface = SampleGeom.surface();
AWTChart chart = factory.newChart(q);
AWTChart chart = (AWTChart)factory.newChart(q);
AWTView view = chart.getView();
((ViewAndColorbarsLayout) chart.getView().getLayout()).setShrinkColorbar(true);
//((ViewAndColorbarsLayout) chart.getView().getLayout()).setShrinkColorbar(true);
AxisLayout layout = chart.getAxisLayout();
// layout.setFont(new Font("Apple Chancery", 20));
@ -107,15 +111,10 @@ public class MTest_Layout_Native_Open {
//view.addRenderer2d(new View2DLayout_Debug(Color.GREEN));
view.addRenderer2d(new View2DLayout_Debug(Color.GREEN));
view.getCamera().setScreenGridDisplayed(true);
colorbar.setScreenGridDisplayed(true);
/*chart.getView().addViewEventListener(()->{
});*/
view.addViewLifecycleChangedListener(new IViewLifecycleEventListener() {
@Override
@ -123,7 +122,6 @@ public class MTest_Layout_Native_Open {
}
@Override
public void viewHasInit(ViewLifecycleEvent e) {
//info(chart, view, colorbar);
}
@Override
public void viewHasRendered(ViewLifecycleEvent e) {
@ -146,15 +144,20 @@ public class MTest_Layout_Native_Open {
//System.out.println("Canvas.bounds : " + ((Component)chart.getCanvas()).getBounds());
System.out.println("---------------------------------");
System.out.println("Canvas.dims : " + chart.getCanvas().getDimension());
System.out.println("---");
System.out.println("View.cam.viewport : " + view.getCamera().getLastViewPort());
System.out.println("View.scale : " + view.getPixelScale());
System.out.println("---");
BufferedImage i = colorbar.getImage();
System.out.println("Colorbar.image : " + i.getWidth(null) + " x " + i.getHeight(null));
System.out.println("Colorbar.margins : " + colorbar.getMargin() + "add left/right, top/bottom");
System.out.println("Colorbar.askedDim : " + colorbar.getWidth() + " x " + colorbar.getHeight());
System.out.println("Colorbar.viewport : " + colorbar.getLastViewPort());
System.out.println("Colorbar.margins : " + colorbar.getMargin());
System.out.println("Colorbar.image : " + i.getWidth(null) + " x " + i.getHeight(null));
System.out.println("Colorbar.minDim : " + colorbar.getMinimumDimension());
System.out.println("---");
AWTColorbarImageGenerator gen = colorbar.getImageGenerator();
System.out.println("Colorbar.gen.scale : " + gen.getPixelScale());

View File

@ -49,8 +49,8 @@ public class Line2D_DemoAWT {
OverlayLegendRenderer legend = new OverlayLegendRenderer(infos);
LineLegendLayout layout = legend.getLayout();
layout.setBoxMarginX(10);
layout.setBoxMarginY(10);
layout.getMargin().setWidth(10);
layout.getMargin().setHeight(10);
layout.setBackgroundColor(Color.WHITE);
layout.setFont(new java.awt.Font("Helvetica", java.awt.Font.PLAIN, 11));