My chart did show anything in android - java

I did some code but still im on the road to complete the whole chart.but at this moment I just want to show a CIRCLE in the emulator,just to make sure that im on the right path.but none of them is appear.the requestWindowFeature( Window.FEATURE_NO_TITLE ); is working but the rest is not.Help me.
package com.Sabry.yesbmi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
public class Chart extends Activity {
Paint paint;
#Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
requestWindowFeature( Window.FEATURE_NO_TITLE );
setContentView( R.layout.chart );
initializeView();
}
private void initializeView() {
paint = new Paint();
paint.setColor( Color.BLACK );
paint.setStrokeWidth( 2 );
paint.setTextSize( 20 );
paint.setStyle(Paint.Style.STROKE);
setContentView( new Panel( this ) );
}
class Panel extends View {
public Panel( Context context ) {
super( context );
}
}
public void onDraw( Canvas canvas ) {
int originX = 10, originY = 800;
canvas.drawColor( Color.WHITE ); // Background color
canvas.drawCircle( 300, 80, 20, paint );
}
}

I edited your question and formatted the code. It is now very obvious that you have onDraw() outside the Panel class. As you have it, it is a method of your Activity.
Here, I've removed the extra braces.
class Panel extends View {
public Panel( Conext context ) {
super( context );
}
public void onDraw( Canvas canvas ) {
int originX = 10, originY = 800;
canvas.drawColor( Color.WHITE ); // Background color
canvas.drawCircle( 300, 80, 20, paint );
}
}
It is not required, but is good practice, to decorate overridden methods with the #Override annotation. If you had done this, the compiler would have given you an error since Acitivty does not have an onDraw method.

Related

Curving text in Android

I'm creating a text editing app that allows the user to edit text using different fonts, colors, sizes, etc. Right now I'm working on a feature that is supposed to curve the text. I already tried asking once HERE but the answer that was posted wasn't really what I was looking for (or maybe it was and I just don't understand exactly what they meant) so I figured I'd ask again with an ACTUAL reference to what I'm trying to accomplish. So as you can see, I'm trying to have the text curve based on what the valued of the slider (seekbar in my case) is, but I just don't know how to do it. I was trying to mimic what is done HERE specifically the Along a path part, but again I'm not sure how to make it work with a seekbar as shown in the image.
Thank you
EDIT
Here's the code that I've been working with. Like I stated earlier, this my be exactly what I'm looking for and I'm just stupid, but here it is anyways.
import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import android.view.*;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Painting extends Activity
{
public static int y = 0;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View
{
private Paint mPaint;
private float mX;
private float[] mPos;
private Path mPath;
private Paint mPathPaint;
private static final int DY = 30;
private static final String TEXTONPATH = "Along a path";
private static void makePath(Path p)
{
p.moveTo(10, 0);
p.cubicTo(00, 00, 00, 00, 900, 00);
}
public SampleView(Context context)
{
super(context);
setFocusable(true);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(90);
mPaint.setTypeface(Typeface.SERIF);
mPath = new Path();
makePath(mPath);
mPathPaint = new Paint();
mPathPaint.setAntiAlias(true);
mPathPaint.setColor(0x800000FF);
mPathPaint.setStyle(Paint.Style.STROKE);
}
#Override
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.WHITE);
Paint p = mPaint;
float x = mX;
float y = 0;
float[] pos = mPos;
p.setColor(0x80FF000);
canvas.drawLine(x, y, x, y+DY*3, p);
p.setColor(Color.BLACK);
canvas.translate(0, DY*10);
canvas.drawPath(mPath, mPathPaint);
p.setTextAlign(Paint.Align.LEFT);
canvas.drawTextOnPath(TEXTONPATH, mPath, 0, 0, p);
}
}
}
You are not going to be able to do this with a TextView. Instead you will have to write you own class extending View.
You will have to override onDraw and draw the text onto the Canvas there.
To make it work with a SeekBar you can use SeekBar#setOnSeekBarChangeListener. There in onProgressChanged you can tell your View to change its appearance.

How to set Z-Order of elements on apache poi?

I'm using apache poi library to make a ppt file in my java program.
To insert elements, I added elements to a Document object.
the add order is below.
Picture1 -> rectangle1 -> Picture2 -> rectangle2
But, All Pictures is over all rectangles in output ppt file.
How to set z-order of elements such as the order of add?
Well, here is what you can do. The order of adding is, as you see, Rectangle1, Picture, Rectangle2. And the z-order is respected (we can see all Rectangle2, but a Rectangle1 is partly hidden behind the image):
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.model.PPGraphics2D;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.ShapeGroup;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
public class PowerPointTest {
public static void main( String[] args ) {
SlideShow slideShow = new SlideShow();
Slide slide = slideShow.createSlide();
try {
// Rectangle1 (partly hidden)
fillRectangle( slide, Color.blue, 20, 20, 300, 300 );
// Image
int index = slideShow.addPicture(new File("IMG_8499.jpg"), Picture.JPEG);
Picture picture = new Picture(index);
picture.setAnchor(new Rectangle( 50, 50, 300, 200 ));
slide.addShape(picture);
// Rectangle2 (all visible)
fillRectangle( slide, Color.yellow, 250, 150, 50, 10 );
FileOutputStream out = new FileOutputStream( "z-order.ppt" );
slideShow.write( out );
out.close();
} catch ( FileNotFoundException e ) {
e.printStackTrace();
} catch ( IOException e ) {
e.printStackTrace();
}
}
private static void fillRectangle( Slide slide, Color color, int x, int y, int width, int height ) {
// Objects are drawn into a shape group, so we need to create one
ShapeGroup group = new ShapeGroup();
// Define position of the drawing inside the slide
Rectangle bounds = new Rectangle(x, y, width, height);
group.setAnchor(bounds);
slide.addShape(group);
// Drawing a rectangle
Graphics2D graphics = new PPGraphics2D(group);
graphics.setColor(color);
graphics.fillRect(x, y, width, height);
}
}
Take a look at this tutorial.
If you need not just to a draw a rectangle using lines, but, for example, adding a table with text cells, see examples here. Hope it helps!

Exception in thread "LWJGL Application" java.lang.NullPointerException

I am following a tutorial on creating a game with LibGdx from some ebook. the tutorial has steps for creating a game called "Canyon Bunny". Its a simple 2D game. but i keep getting this annoying error! (i also used to get the error on a different tutorial of the same genre)
i am in the early stages of the development for this game. and i am doing some test (of which i follow to the letter from the tutorial). I use a MAC and a i have tried many solutions with no luck at all.
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.Adel.CanyonBunny.game.WorldUpdater.updateTestObjects(WorldUpdater.java:83)
at com.Adel.CanyonBunny.game.WorldUpdater.update(WorldUpdater.java:76)
at com.Adel.CanyonBunny.CanyonBunnyMain.render(CanyonBunnyMain.java:39)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
It is truly one of the most frustrating things a striving programmer can face.
ill get the code of all the classes in case that's related somehow...
This is CanyonBunnyMain in the general program:
package com.Adel.CanyonBunny;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.Adel.CanyonBunny.game.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.Application;
public class CanyonBunnyMain implements ApplicationListener {
private static final String TAG = CanyonBunnyMain.class.getName();
private WorldUpdater worldUpdater;
private WorldRenderer worldRenderer ;
private boolean paused ;
public void create() {
//I'll set the log to debug for the developing process
Gdx.app.setLogLevel(Application.LOG_DEBUG) ;
worldUpdater = new WorldUpdater();
worldRenderer = new WorldRenderer() ;
// since, upon creation, the game is not paused, then:
paused = false ;
}
public void render() {
if (paused = true) {
//update the game by the time passed since the last update
worldUpdater.update(Gdx.graphics.getDeltaTime()) ;
}
//sets the screen color to: CornFlower Blue
Gdx.gl.glClearColor(0x64 / 255.0f, 0x95 / 255.0f, 0xed / 255.0f, 0xff / 255.0f);
//clears the screen to prevent flickering
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT) ;
//Render the game to the screen
worldRenderer.render();
}
public void resize (int w, int h) {
worldRenderer.resize(w, h) ;
}
public void pause () {
paused = true ;
}
public void resume() {
paused = false ;
}
public void dispose() {
worldRenderer.dispose() ;
} }
this is the WorldRenderer (general program too) :
package com.Adel.CanyonBunny.game;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
public class WorldRenderer {
private OrthographicCamera cam;
private SpriteBatch batch ;
private WorldUpdater updater;
public void WorldRenderer(WorldUpdater worldUpdater) { }
public void init() { }
public void render() { }
public void resize(int w, int h) { }
public void dispose() { }
}
this is the main class (from the desktop project: the one that i run on my MAC) :
package com.Adel.CanyonBunny;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "CanyonBunny";
cfg.useGL20 = false;
cfg.width = 800;
cfg.height = 480;
new LwjglApplication(new CanyonBunnyMain(), cfg);
}
}
Any help will be wonderful.
tell me should you need extra data
this is the WorldUpdater class for those who asked:
package com.Adel.CanyonBunny.game;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
public class WorldUpdater {
private final String TAG = WorldUpdater.class.getName();
public Sprite[] testSprites;
public int selectedSprite;
public WorldRenderer worldRenderer;
public void worldUpdater() {
init() ;
}
public void init() {
initTestObjects() ;
}
private void initTestObjects() {
// create new array of 5 sprites
testSprites = new Sprite[5] ;
// Create empty POT-sized Pixmap with 8 bit RGBA pixel data
int w = 32;
int h = 32;
Pixmap pixmap = createProceduralPixmap(w, h) ;
//create a new texture from Pixmap data
Texture texture = new Texture(pixmap) ;
//create sprites using the just created texture
for (int i = 0; i < testSprites.length; i++) {
Sprite spr = new Sprite(texture) ;
spr.setSize(1,1) ;
//set origin to sprite's center
spr.setOrigin(spr.getWidth() / 2.0f, spr.getHeight() / 2.0f) ;
float randomX = MathUtils.random(-2.0f, 2.0f) ;
float randomY = MathUtils.random(-2.0f, 2.0f) ;
spr.setPosition(randomX, randomY) ;
//put new sprite into array
testSprites[i] = spr ;
}
//set first sprite as the selected one
selectedSprite = 0 ;
}
private Pixmap createProceduralPixmap(int width, int height) {
Pixmap pixmap = new Pixmap(width, height , Format.RGBA8888) ;
//fill the square with red color at 50% opacity
pixmap.setColor(1, 0, 0, 0.5f) ;
pixmap.fill() ;
//draw a yellow X in the pixmap
pixmap.setColor(1, 1, 0 , 1) ;
pixmap.drawLine(0, 0, width, height) ;
pixmap.drawLine(width, 0, 0, height);
//draw a cyan-colored border around the square
pixmap.setColor(0, 1, 1, 1) ;
pixmap.drawRectangle(0, 0, width, height) ;
return pixmap;
}
public void update(float deltaTime) {
updateTestObjects(deltaTime);
}
private void updateTestObjects(float deltaTime) {
//get current rotation from the selected sprite
float rotation = testSprites[selectedSprite].getRotation();
//rotate sprite by 90 degrees per second
rotation += 90 * deltaTime;
//wrap around at 360 degrees
rotation %= 360 ;
testSprites[selectedSprite].setRotation(rotation);
}
}
Also, when i check this line out in Debugging mode:
testSprites = new Sprite[5] ;
"testSprites" keeps showing null.
i hope this clears up some details!
thanks again.
The problem is with your "constructors", mainly in the updater (as the renderer does nothing):
public void worldUpdater() { ... }
Constructors should not specify return types - that's part of how the compiler recognizes them as constructors. As it is in your code, it's just a method you could call on an existing object instance. Change it like so:
public WorldUpdater() { ... }
Note the lack of a return type and the uppercase W.
You can change the renderer the same way. (But then you will have to pass the updater to its constructor in the main class.)
Also, Nine Magics is right that the way you store renderer and updater references in each other doesn't make much sense, even if it's not related to this problem. I see no reason why an updater class would need to know about its renderer, I'd remove that field.
In your WorldRenderer you specify this:
public void WorldRenderer(WorldUpdater worldUpdater) { }
And WorldRendere also carries an instance of an worldUpdater?
private WorldUpdater updater;
But on your main file you create an instance of both renderer and updater?
worldUpdater = new WorldUpdater();
worldRenderer = new WorldRenderer() ;
I don't know, I might have tired eyes or something but this seems too complex. Can it be that you are refering to a wrong instance of WorldUpdater? Might edit this if I can wrap my head around it better.

Drawing & creating the canvas on the android screen

The code will run through an array. If the character 'X' is seen in the array, it will drawRect at the respective spot on the screen.
The problem is that the color isn't actually being drawn on the screen. Some of the android websites talked about how you need a canvas, bitmap, etc. etc.
My question: What do I need to do to get the colours from the drawRect method to actually show up on the screen?
package com.example.routedrawingtest;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.graphics.Paint;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
float scale_x;
float scale_y;
public void main(String[] args){
#SuppressWarnings("unused")
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
int screen_width = size.x;
int screen_height = size.y;
scale_x = screen_width/125;
scale_y = screen_height/100;
}
public void onDraw(Canvas canvas) {
int i;
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
int j;
for (i=0; i<125; i++)
for (j=0; j<100; j++)
if (charMap[i][j]=='X')
canvas.drawRect(i*scale_x, j*scale_y, scale_x,scale_y, paint);
}
}
The method onDraw is not called by an Activity to do what you want, you need to extend the class View and implement the method there. Until you learn more when you add the view to a parent, you have to give it an explicit size like fill_parent or it will not show up on the screen.
For example:
public class Drawer extends View
{
Paint paint = new Paint();
public Drawer()
{
paint.setStyle(Style.STROKE);
paint.setStrokeColor(Color.BlUE);
// etc...
}
public void onDraw(Canvas canvas)
{
for (i=0; i<125; i++)
for (j=0; j<100; j++)
if (charMap[i][j]=='X')
canvas.drawRect(i*scale_x, j*scale_y, scale_x,scale_y, paint);
}
}
Add the view to your code via XML or via code like using it's fully qualified name:
XML:
<com.example.comp.Drawer
android:layout_width="fill_parent"<<<< specify a size
etc...
/>

Two simultaneous RotationTransitions on the same node

I've been playing with animations while getting acquainted with JavaFX 2.0, and I wrote a small test program that was intended to rotate a rectangle along its X and Y axes. Here is the test program:
import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
public class ParallelTransitionTest extends Application
{
public static void main( String[] args )
{
launch( args );
}
#Override
public void start( Stage primaryStage ) throws Exception
{
init( primaryStage );
primaryStage.show();
}
private void init( Stage primaryStage )
{
primaryStage.setTitle( "Parallel Transition" );
primaryStage.setResizable( true );
// Create the scene
BorderPane root = new BorderPane();
Scene scene = new Scene( root, 800, 600, true );
scene.setFill( Color.BLACK );
primaryStage.setScene( scene );
Rectangle rect = RectangleBuilder.create()
.width( 100 ).height( 100 )
.x( 350 ).y( 250 )
.fill( Color.BLUE )
.build();
RotateTransition rotationY = new RotateTransition();
rotationY.setAxis( Rotate.Y_AXIS );
rotationY.setDuration( Duration.seconds( 5 ) );
rotationY.setByAngle( 360 );
rotationY.setNode( rect );
rotationY.setAutoReverse( true );
rotationY.setCycleCount( Animation.INDEFINITE );
RotateTransition rotationX = new RotateTransition();
rotationX.setAxis( Rotate.X_AXIS );
rotationX.setDuration( Duration.seconds( 5 ) );
rotationX.setByAngle( 360 );
rotationX.setNode( rect );
rotationX.setAutoReverse( true );
rotationX.setCycleCount( Animation.INDEFINITE );
FadeTransition fade = new FadeTransition();
fade.setDuration( Duration.seconds( 5 ) );
fade.setToValue( 0.2 );
fade.setNode( rect );
fade.setAutoReverse( true );
fade.setCycleCount( Animation.INDEFINITE );
ParallelTransition transition = new ParallelTransition( rect,
rotationX, rotationY, fade );
transition.setAutoReverse( true );
transition.play();
root.getChildren().add( rect );
}
}
Unfortunately, the rotation is only happening for one of the axes. My assumption is that both RotationTransitions are running, but that one is overwriting the rotation applied by the other. Is this the intended behavior of RotationTransition?
Also, if the following three lines are commented:
rotationY.setNode( rect );
...
rotationX.setNode( rect );
...
fade.setNode( rect );
I get a NullPointerException. The docs suggest that you shouldn't need to set the nodes on transitions included in a ParallelTransition. Is this a bug?
You should create another node add your rect to that node, and then:
RotateTransition rotationY = new RotateTransition();
rotationY.setAxis( Rotate.Y_AXIS );
rotationY.setDuration( Duration.seconds( 5 ) );
rotationY.setByAngle( 360 );
rotationY.setNode( rect );
rotationY.setAutoReverse( true );
rotationY.setCycleCount( Animation.INDEFINITE );
RotateTransition rotationX = new RotateTransition();
rotationX.setAxis( Rotate.X_AXIS );
rotationX.setDuration( Duration.seconds( 5 ) );
rotationX.setByAngle( 360 );
rotationX.setNode( NEWNODE );
rotationX.setAutoReverse( true );
rotationX.setCycleCount( Animation.INDEFINITE );

Categories