When i execute the code bellow and add in mySimpleXYPlot.getGraphWidget().getBorderPaint().setColor(Color.WHITE);
the app crashes when i launch it on my device.
what i'm trying to accomplish is getting rid of the black border around the entire graph. it is about 2cm thick on located on the top, left and bottom sides of the chart. Is there any way i can get rid of this black border?
public class MainActivity extends Activity {
private XYPlot mySimpleXYPlot;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a couple arrays of y-values to plot:
Number[] days = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
Number[] values = { 380, 1433, 1965, 3200, 3651, 3215, 3217 };
// initialize our XYPlot reference:
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
mySimpleXYPlot.getBackgroundPaint().setColor(Color.WHITE);
mySimpleXYPlot.setBorderStyle(XYPlot.BorderStyle.NONE, null, null);
mySimpleXYPlot.getGraphWidget().getBackgroundPaint().setColor(Color.WHITE);
mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE);
// Domain
mySimpleXYPlot.getGraphWidget().setDomainLabelPaint(null);
mySimpleXYPlot.getGraphWidget().setDomainOriginLinePaint(null);
mySimpleXYPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, days.length);
mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("0"));
//Range
mySimpleXYPlot.getGraphWidget().setRangeOriginLinePaint(null);
mySimpleXYPlot.setRangeStep(XYStepMode.SUBDIVIDE, values.length);
mySimpleXYPlot.setRangeValueFormat(new DecimalFormat("0"));
//Remove legend
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getLegendWidget());
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getDomainLabelWidget());
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getRangeLabelWidget());
mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getTitleWidget());
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(days),
Arrays.asList(values),
"Series1"); // Set the display title of the series
// Create a formatter to use for drawing a series using LineAndPointRenderer:
LineAndPointFormatter series1Format = new LineAndPointFormatter(
Color.rgb(0, 200, 0), // line color
Color.rgb(0, 100, 0), // point color
Color.CYAN); // fill color
// setup our line fill paint to be a slightly transparent gradient:
Paint lineFill = new Paint();
lineFill.setAlpha(200);
lineFill.setShader(new LinearGradient(0, 0, 0, 250, Color.WHITE, Color.GREEN, Shader.TileMode.MIRROR));
series1Format.setFillPaint(lineFill);
// add a new series' to the xyplot:
mySimpleXYPlot.addSeries(series1, series1Format);
// by default, AndroidPlot displays developer guides to aid in laying out your plot.
// To get rid of them call disableAllMarkup():
mySimpleXYPlot.disableAllMarkup();
}
}
If you want to get rid of all of the color behind your graph, you will need the following three methods. Each of which gets rid of different parts.
//This gets rid of the gray grid
mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.TRANSPARENT);
//This gets rid of the black border (up to the graph) there is no black border around the labels
mysimpleXYPlot.getBackgroundPaint().setColor(Color.TRANSPARENT);
//This gets rid of the black behind the graph
mySimpleXYPlot.getGraphWidget().getBackgroundPaint().setColor(Color.TRANSPARENT);
//With a new release of AndroidPlot you have to also set the border paint
plot.getBorderPaint().setColor(Color.TRANSPARENT);
Hope this helps.
Border line can be hidden by this method:
plot.setBorderPaint(null);
plot.setPlotMargins(0, 0, 0, 0);
I was able to figure out how to fix the graph with this information but I had to pull together the information from several of these posts. There appears to be 4 different background areas:
* grid (and axis labels)
* area around the graph
* border around the graph.
* margin around the border
The background color for the grid and range/domain labels are set using
plot.getGraphWidget().getBackgroundPaint().setColor(background_color);
The area round the grid can get set by:
plot.getBackgroundPaint().setColor(background_color);
This still leaves a border that is drawn around the graph. You can either get rid of the border:
plot.setBorderPaint(null);
or set the background color
plot.getBorderPaint().setColor(background_color);
This leaves the margin around the entire plot. This can be removed using
plot.setPlotMargins(0, 0, 0, 0);
There should be a way to change the color for the margin instead of just removing it but didn't bother to figure that out.
Related
I am a little bit new to OpenGL. I am trying to draw 3D dynamic trail for aircraft using Java OpenGL and WorldWind Java I can draw it by using glDrawArrays. Since the trail of the aircraft increases in every frame(25fps) I put new vertice values to verticeBuffer. I also use rightFloatBuffer and leftFloatBuffer to draw GL_LINE_STRIP to the both sides of the trail as you may see in the attached firstpicture. Since the trail gets longer and longer as the aircraft flies I thought that I need to create a large FloatBuffer for the triangles (verticeBuffer) and 2 large FloatBuffers for the left and right lines.
My first question: What is the most efficient way to draw to many triangles? Based on my code I think after 5 hours of flight the FloatBuffers will be full. If I try to update values with for loop in each frame and if I have, say 50-75 aircraft at the same time, this will reduce the performance. And because of that, I update one triangle at each frame.
Second question: I want to draw a trail like in the second picture. As you see trail gets more transparent as it gets closer to aircraft. And when the aircraft turns color the bottom side of the trail seems different. How can I do it?
Third question: I use gl.DepthMask(false) and draw line_strip and gl.DepthMask(true) to draw smooth lines without a gap between the lines. But this time aircraft trail which is added to the scene first always seems on the top no matter if it is under another trail. What can I do to overcome this? Or what can I do to draw smooth lines without gaps considering the amount of the vertices?
My code to draw the trail is below:
private final FloatBuffer verticeBuffer = GLBuffers.newDirectFloatBuffer(3000000);
private final FloatBuffer rightFloatBuffer = GLBuffers.newDirectFloatBuffer(1500000);
private final FloatBuffer leftFloatBuffer = GLBuffers.newDirectFloatBuffer(1500000);
protected void drawTrail() {
gl.glPushAttrib(GL2.GL_CURRENT_BIT | GL2.GL_COLOR_BUFFER_BIT | GL2.GL_LINE_BIT | GL2.GL_ENABLE_BIT
| GL2.GL_DEPTH_BUFFER_BIT);
try {
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
doDrawTrail(dc);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
gl.glDisable(GL.GL_BLEND);
} finally {
gl.glPopAttrib();
}
}
protected void doDrawTrail() {
updateTrailVertices();
float[] colors = new float[]{trailColor.getRed() / 255.f, trailColor.getGreen() / 255.f, trailColor.getBlue() / 255.f};
gl.glColor4f(colors[0], colors[1], colors[2], 0.6f);
gl.glEnable(GL2.GL_LINE_SMOOTH);
gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL2.GL_NICEST);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, verticeBuffer.rewind());
gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, verticeBuffer.limit() / 3);
gl.glColor3f(colors[0], colors[1], colors[2]);
gl.glLineWidth(3f);
//To draw smooth lines
gl.glDepthMask(false);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, rightFloatBuffer.rewind());
gl.glDrawArrays(GL.GL_LINE_STRIP, 0, rightFloatBuffer.limit() / 3);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, leftFloatBuffer.rewind());
gl.glDrawArrays(GL.GL_LINE_STRIP, 0, leftFloatBuffer.limit() / 3);
gl.glDepthMask(true);
}
protected void updateTrailVertices() {
// In each frame when the aircraft position changes this function updates the last vertices
if (positionChange) {
positionChange = false;
//I need to set the position and the limit of the buffers to draw only updated parts
verticeBuffer.position(lastIndex * 2);
rightFloatBuffer.position(lastIndex);
leftFloatBuffer.position(lastIndex);
verticeBuffer.limit((lastIndex * 2) + 6);
rightFloatBuffer.limit(lastIndex + 3);
leftFloatBuffer.limit(lastIndex + 3);
List<Vec4> pointEdges = computeVec4(this.currentPosition, this.currentHeading, this.currentRoll, this.span);
verticeBuffer.put((float) pointEdges.get(0).x).put((float) pointEdges.get(0).y).put((float) pointEdges.get(0).z);
verticeBuffer.put((float) pointEdges.get(1).x).put((float) pointEdges.get(1).y).put((float) pointEdges.get(1).z);
rightFloatBuffer.put((float) pointEdges.get(0).x).put((float) pointEdges.get(0).y).put((float) pointEdges.get(0).z);
leftFloatBuffer.put((float) pointEdges.get(1).x).put((float) pointEdges.get(1).y).put((float) pointEdges.get(1).z);
lastIndex = rightFloatBuffer.position();
}
}
If you can use geometry shaders, the most efficient way to display the flight track is to have one vertexbuffer and render it as a line strip. The vertexbuffer contains the earlier locations and a normal vector (plane up direction). With these two values the the geometry shader you can transform it into quads. These quads should contain texture coordinates, which can be used in the fragment shader to display the borders.
You need only one draw call and reduce the data stored on the gpu to the absolute minimum.
The fading of the flight track can be done by using a uniform with the plane coordinates. One of your shades can calculate the distance to the plane and with that a alpha value for the pixel.
Using FX2D.
Hi, I'm currently drawing a line at every frame using the curveVertex() function.
Every line stays on the sketch voluntarily by not resetting the background at every step,
which leaves a trail effect.
But I would like to make these trails fade away over time. I tried resetting the background in the draw() function by giving it a small opacity value but it clears all the trails at once every time.
Right now I can only have either all trails or no trails.
//background(0, 0, 0, 10); // Reset background every time
stroke(255, 255, 255, 10); // Draw Line between all nodes
curveVertex(nodes.get(fixId(i)).position.x, nodes.get(fixId(i)).position.y);
with trails
background() does not just set the background color, it clear the entire window. It is not intended to use background within beginShape() / endShape() sequences.
What what to do can be achieved by drawing a rect() with blendMode(DIFFERENCE) on the entire window, at the begin of the frame. e.g.:
void draw() {
// "fade" the entire view
blendMode(DIFFERENCE);
fill(1, 1, 1, 255);
rect(0, 0, width, height);
blendMode(ADD);
// draw shape
// [...]
}
See also the answer to Processing - rendering shapes.
I have a canvas and a simple bitmap for background image, fills the whole screen. I created a rect painted black and set it's alpha to 250 in order to make a "dark" effect on the background image. My aim to make a simple circle object that reveals the place it's hovering above. I tried thinking in many ways how to excecute it and failed.
I think the best way is to create a simple circle that manages to decrease the darkness alpha on the position it hovers above, but I have no idea how to do it.
The relevant part of my code:
private ColorFilter filter = new LightingColorFilter(Color.BLACK, 1);
private Paint darkPaint = new Paint(Color.BLACK), paint = new Paint(), paint2 = new Paint();//The style of the text and dark.
public DarkRoomView(Context context) {
super(context);
myChild = this;
darkPaint.setColorFilter(filter);
darkPaint.setAlpha(250);
paint2.setAlpha(10);
paint.setAlpha(50);
}
private void loadGFX() {//Loads all of this view GFX file.
backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.darkroomscreen);
lightImage = BitmapFactory.decodeResource(getResources(), R.drawable.light);
}
private void drawGFX(Canvas canvas) {
canvas.drawBitmap(backgroundImage, 0, 0, paint2);//The backgeound image.
canvas.drawRect(0, 0, WIDTH, HEIGHT, darkPaint);//The darkness.
canvas.drawBitmap(lightImage, 50, 50, paint);//A spotlight.
}
Any ideas how I should get it done?
Thanks!
For the spotlight, you could draw a circle of the original image over the darkness. You'd simply need to find the correct rectangle of the original image (based on where your finger is), and then draw a circle of that particular rectangle over the darkness. Trying to look "through" the darkness won't really get you anywhere; you need to place something over it.
By the time you draw the "spotlight", you've already darkened the image with the rectangle. It would be difficult to recover information lost during that draw.
A more flexible approach would be to draw a dark rectangle with a spotlight in a separate image (that is, compose the "darkness" and spotlight alpha and color mask image first), and then draw that mask image on top of the background as a separate step. This would also let you easily do things like e.g. give the spotlight fuzzy borders.
I have a libgdx game I am working, and in this game, when you hit the "Help" screen, my Help_Manager class uses my "Room's" stage object to draw a label. The room's camera is 27 wide, and 18 tall. The stage uses the same camera, and set's it's viewport to the same WIDTH, and HEIGHT.
If I were to add a text button to the stage with bounds(4,4,1,1), the button draws correctly, but when I add a Label with bounds(0,0,16,4) the labels "S" alone takes up half the screen (shown above)(it's supposed to say "Swipe finger from ship to \"Fling\" ship."). No matter what I set the bounds width and height to, the label is still the same size. Changing the scale doesn't have an effect either. Changing the font-scale(of the label, not the font) does have an effect, but for some reason the text is all messed up:
Heres bits and peices of code:
Room.java:
Stage stage = new Stage();
camera = new OrthographicCamera(WIDTH,HEIGHT);
camera.position.set(WIDTH/2,HEIGHT/2,0);
camera.update();
stage.setCamera(camera);
stage.setViewport(WIDTH, HEIGHT, false);
public void resize(int width, int height){
ppux = width/WIDTH;
ppuy = height/HEIGHT;
stage.setViewport(Room.WIDTH,Room.HEIGHT,false);
}
public void render(float delta){
stage.act(delta);
stage.draw();
}
Help_Manager.java
BitmapFont font = new BitmapFont();
LabelStyle style = new LabelStyle();
Label text;
style.font = font;
text = new Label(instructions[0],style);
text.setText(instructions[stage]);
text.setBounds(0,0,16,4);
//text.setFontScale(0.1f);
room.stage.addActor(text);
So how do I get my label to cooperate? Thanks in advance for any help!
Well, I discovered that fonts by default use integers for positions. When using a small camera size and stage viewport, means that the letters need to be placed more acurately, and so I had to change the font.setUseIntegerPosistions(false); I also discovered, that when you set the bonds for a Label, this simply defines the space that can be used, but it is still being filled with the same font, and size of text. Your putting the same stuff in a bigger/smaller box. (This explains why the text moves up as you increase the height of your Label, your raising the top bound, and the text starts at the top). To actually change the size of the font youse text.setFontScale();
I am writing an Eclipse RCP-based application and am trying to draw a rectangle on top of a ViewPart. However, the rectangle seems to take up the whole screen even when specifiying the bounds. The following is my code.
public void createPartControl(Composite parent) {
Shell shell = parent.getShell();
Canvas canvas = new Canvas(parent, SWT.NONE);
LightweightSystem lws = new LightweightSystem(canvas);
RectangleFigure rectangle = new RectangleFigure();
rectangle.setBounds(new Rectangle(0, 0, 10, 10));
rectangle.setBackgroundColor(ColorConstants.green);
lws.setContents(rectangle);
}
I haven't used Draw2D, but I tried modifying your example by creating another rectangle figure and adding it to the first one, and that one shows up. I.e.
// from your code
rectangle.setBackgroundColor(ColorConstants.green);
// new code
RectangleFigure r2 = new RectangleFigure();
r2.setBounds(new Rectangle(0,0,10,10));
r2.setBackgroundColor(ColorConstants.blue);
rectangle.add(r2);
// back to your code
lws.setContents(rectangle);
It looks fine to me - there's a little blue rectangle in the top left corner of the totally green canvas. I guess that the figure you use as the contents of the canvas, by default (and probably by necessity), takes up the whole canvas.