This is my first time working with extends and stuff like that :)
In mt most recent program, I have BasicTile extending Tile.
I construct the basic tile with a Bitmap.
The bitmap is not an actual bitmap, its a class that I wrote that contains an array of integers (holding color values).
When I render with the bitmap, I get a black screen. This goes away when I make the bitmap static (I do not want that because I want multiple basic tiles like grass, bushes, etc.)
It also goes away if I set the texture right in the render method (I don't want to do that either bc it would load 60*256 bitmaps a second).
I tested some and in the constructor of the BasicTile the array in the bitmap contains the right values. In the render method it changed to only be the number -16777216 though.
The information seems to get lost somewhere in between. I am having problems finding where it gets lost because I do not do anything to the bitmap in between the constructor and the render method.
This is my Tile, BasicTile, and Bitmap classes:
public abstract class Tile {
public static final Tile[] tiles = new Tile[576];
public static final Tile VOID = new BasicTile(0, Art.spritesheet[0][0]);
public static final Tile STONE = new BasicTile(1, Art.spritesheet[1][0]);
public static final Tile GRASS = new BasicTile(2, Art.spritesheet[3][0]);
protected byte id;
protected boolean solid;
protected boolean emitter;
public Tile(int id, boolean isSolid, boolean isEmitter){
this.solid = isSolid;
this.emitter = isEmitter;
tiles[id] = this;
}
public byte getId(){
return id;
}
public boolean isSolid(){
return solid;
}
public boolean isEmitter(){
return emitter;
}
public abstract void render(Screen screen, int x, int y);
}
public class BasicTile extends Tile{
protected int tileId;
protected Bitmap texture;
public BasicTile(int id, Bitmap bitmap) {
super(id, false, false);
texture = bitmap;
}
public void render(Screen screen, int x, int y) {
/*for(int i = 0; i < texture.h; i++){
for(int j = 0; j < texture.w; j++){
System.out.println(texture.pixels[j + i * texture.w]);
}
}*/ //the algorithm I used to debug (getting the values of the int array)
screen.render(texture, x, y);
}
}
public class Bitmap {
public int w, h;
public int[] pixels;
public Bitmap(int w, int h){
this.w = w;
this.h = h;
this.pixels = new int[w * h];
}
}
when I render to the screen, it renders it to another bigger array of ints :)
Added examples:
normal code: see above (sorry I can only post 2 links)
result: black screen
making bitmap static: in the BasicTile change "protected Bitmap texture;" to "protected static Bitmap texture;"
result
set it in the render method
result: same as the one with the bricks (so it works)
P.S.: If you need anything else to solve this issue please tell me :)
I figured out it was not displaying a black screen but instead the void tile...
I don't know why (I'm working on a fix and it will be posted here) but if I change the
public static final Tile VOID = new BasicTile(0, Art.spritesheet[0][0])
to
public static final Tile VOID = new BasicTile(0, Art.spritesheet[3][0])
it renders the bricks :)
Related
i need to change the colour of a car PNG image when it crash to a block, like a kind of filter here's the 2 class
public class Sprite {
//classe estendibile a tutti gli oggetti
protected int x;
protected int y;
protected int width;
protected int height;
protected boolean visible;
protected Image image;
public Sprite(int x, int y) {
this.x = x;
this.y = y;
visible = true;
}
protected void getImageDimensions() {
width = image.getWidth(null);
height = image.getHeight(null);
}
protected void loadImage(String imageName) {
ImageIcon ii = new ImageIcon(imageName);
image = ii.getImage();
}
public Image getImage() {
return image;
public Rectangle getBounds() {
return new Rectangle(x, y, width,
height);
}
}`
public class Car extends Sprite {
public Car(int x, int y) {
super(x, y);
loadImage("src/car.png");
getImageDimensions();
}
Well I suppose it depends on the picture. If your picture looks like this
I suppose that going pixel by pixel and using image.setRGB(x, y, newColour) on pixels, that satisfy a condition of image.getRGB(x, y) == colourToChange will work. If you want to process a picture like
you would probably first need to run some recognition algorithm to find out which pixels are actually of the car and which are the background (wheels, shadows, etc.) and then run a filter over those. While this time based on the RGB of the original pixel you would need to calculate the new colour so that what was originally dark, remains dark and what was originally light remains light and so on.
Class FirstTower:
public class FirstTower extends Sprite {
private Map map;
private Texture texture;
private float transfer = 1000;
private float hp;
private float radius;
public FirstTower(Map map) {
this.texture = new Texture("square.png");
this.map = map;
this.hp = 100;
radius = 40;
}
#Override
public void render(SpriteBatch batch) {
batch.draw(texture, Gdx.input.getX() - radius, transfer - Gdx.input.getY() - radius);
}
#Override
public void updade(float dt) {
}
}
I need simple to create example of this class (object of FirstTower) with mouse click on gamefield.
How can I do this?
Try to search, but not find anything useful.
Image of object follow with my cursor.
Here is screen when app is running:
You need to set the GDX InputProcessor. This is done by executing the following:
Replace MyClass with the class you wish to create (FirstTower I believe)
Gdx.input.setInputProcessor(new InputProcessor() {
public boolean touchDown(int x, int y, int point, int button) {
if (button == Input.Buttons.LEFT) {
new MyClass();
return true;
}
return false;
}
});
Note: I have excluded the other methods when creating a new InputProcessor to keep this answer tidy, but you will need to implement those methods.
The application displays streams of randomly changing characters, running down the page to achieve a matrix like effect. The app uses Processing tools to achieve this. Everything works fine, except calling textSize() changes the text size, but the characters are now all turned into 0 or square like characters as shown in the below image.
I have recreated a smaller hard coded version to recreate the problem. This version simply draws a Symbol object in the middle of the screen. The character does not change and does not move.
import processing.core.PApplet;
public class Main extends PApplet {
private Symbol symbol;
public static void main(String[] args) {
PApplet.main("Main");
}
public void setup() {
noCursor();
background(0);
symbol = new Symbol(this, width / 2, height / 2);
}
public void settings() {
fullScreen();
}
public void draw() {
background(0);
symbol.show();
}
public static int getRandomInt(final int min, final int max) {
return (int) (Math.random() * ((max - min) + 1)) + min;
}
public class Symbol {
private float xPosition;
private float yPosition;
private char symbol;
private PApplet parent;
private float size = 35f;
private int opacity = 255;
public Symbol(final PApplet parent,
final float xPosition,
final float yPosition) {
this.parent = parent;
this.xPosition = xPosition;
this.yPosition = yPosition;
// Sets symbol to a random Katakana character.
this.symbol = generateRandomSymbol(0x30A0, 96);
}
public char generateRandomSymbol(final int origin, final int bound) {
return (char) (origin + getRandomInt(0, bound));
}
public void show() {
parent.fill(155, 155, 155, opacity);
// TODO: Fix this dumb bug
// parent.textSize(size);
parent.text(symbol, xPosition, yPosition);
}
}
}
The entire project was a bit large to include in this post so here is the projects github repo page: https://github.com/JSextonn/MatrixRain
Any explanations on why this is happening would be greatly appreciated. Thanks in advance.
I have a problem I was trying to solve for almost 2 days. (It ended up by switching to OpenGL :D)
Anyway... I have Canvas and I am trying to do a simple snake game. Just for this problem imagine our snake is made up from 5x5 pixels rectangles, he is leaving the trail (no clearing) and moving to the right from 0, 50 position... here is the code:
public class Snake extends AnimationWallpaper {
#Override
public Engine onCreateEngine() {
return new SnakeEngine();
}
class SnakeEngine extends AnimationEngine {
int i = 0;
Paint paint = new Paint();
#Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
// By default we don't get touch events, so enable them.
setTouchEventsEnabled(true);
}
#Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
}
#Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xOffsetStep, float yOffsetStep, int xPixelOffset,
int yPixelOffset) {
super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
xPixelOffset, yPixelOffset);
}
#Override
public Bundle onCommand(String action, int x, int y, int z,
Bundle extras, boolean resultRequested) {
if ("android.wallpaper.tap".equals(action)) {
}
return super.onCommand(action, x, y, z, extras, resultRequested);
}
#Override
protected void drawFrame() {
SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
draw(c);
}
} finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
}
void draw(Canvas c) {
//c.save();
//c.drawColor(0xff000000);
paint.setAntiAlias(true);
// paint the fill
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
c.drawRect(i*5, 50, (i+1)*5, 55, paint);
//c.restore();
}
#Override
protected void iteration() {
i++;
super.iteration();
}
}
}
And here is output:
And that is not all, when it draws its kinda shifting... meaning what you see is shifted to the right side so it doesnt actualy stand in the same place...
If you have any idea why is it behave like that, please tell me!
In canvas, there's a reusability functionality which practically just add new drawings to whatever is in the canvas, hence if you are drawing 1 square at the time, the next time you will have whatever u had before +1, if you don't want that, and you want to fully manipulate every single draw from scratch, just add this line of code before to start drawing
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
It will clear anything you had in the previous state of the canvas, and you can start again drawing whatever you want...
Hope It Helps!
Regards!
I'm a newbie so please don't scald me too much as I'd just like to follow the good OOP trail from the very beginning :) So I'm coding a minesweeper in Java with Swing and for now, my code looks like this:
a class with main() only which starts the game by creating an object Minesweeper()
the Minesweeper class in which I create a JFRame, JPanel for menu (and ActionListener for it) and create a Grid(x,y) object
the Grid(int width, int height) class which extends JPanel using which I create a grid with the given dimensions, put mines on it and handle all the playing
I have some worries about the Grid(), though. Is it OK to handle everything from drawing the desired number of JButtons, through setting mines on them and listening for clicks (and also addressing those clicks) to the finding_empty_cells algorithm in case user clicks on something other than bomb and we have to show the surrounding empties in one class? Doens't that violate the single responsibility principle? Or is it OK?
I am not familiar with swing, so I can only give you some pseudo-java code.
However, it should serve the demonstrational purpose. When you want to reach
the next level of OOP, I would recommend creating a class for a cell in the
Minesweeper grid.
public class Cell extends JPanel {
private MinesweepController controller;
private int points;
private boolean revealed;
// Index in the grid.
private int x, y;
public Cell(MinesweepController controller_, int x_, int y_, int points_) {
controller = controller_;
points = points_;
x = x_;
y = y_;
}
public void onClick(MouseEvent event) {
controller.reveal(x, y);
}
public void paint(Graphics gfx) {
if (revealed) {
if (points < 0) {
drawBomb(getX(), getY())
}
else {
drawPoints(getX(), getY(), points);
}
}
else {
drawRect(getX(), getY());
}
}
public int getPoints() {
return points;
}
public boolean isRevealed() {
return revealed;
}
public void reveal() {
revealed = true;
}
}
public class MinesweepController {
private Grid grid;
private int score;
// ...
public boid reveal(int x, int y) {
Cell cell = grid.getCell(x, y);
if (cell.getPoints() < 0) {
// End game.
}
else {
score += cell.getPoints();
// Reveal ascending cells.
}
}
}