I have a such code:
private Image image;
public void paint(GraphicsContext g, double x, double y) {
if (image == null) return;
g.drawImage(image, x, y);
}
How to change chage brightness and then draw the image?
I am aware about ColorAdjust effect and ImageView, but how to use it in this case?
You can set a ColorAdjust effect on the GraphicsContext object with setEffect(Effect):
public void paint(GraphicsContext g, double x, double y) {
if (image == null) return;
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(0.1);
g.setEffect(colorAdjust);
g.drawImage(image, x, y);
}
Related
when i use doublebuffering, painted image is expanded with white background.
Is there something wrong?
enter code here
private Image image_buffer;
private Graphics graphics_buffer
public void paint(Graphics g) {
super.paint(g);
buffering(img1, x1, 40, g);
}
public void buffering(Image img, int x, int y, Graphics g){
image_buffer = createImage(100,100);
graphics_buffer = image_buffer.getGraphics();
buffer.drawImage(img, x, y, this);
g.drawImage(image_buffer, x, y, this);
}
Why not replace your code with just:-
public void paint(Graphics g) {
super.paint(g);
g.drawImage(img1, x1, 40, this);
}
The reason you have a white background is because you're creating a 100x100 "canvas", and drawing on that.
So I am creating a sprite and drawing it to the screen using a different class.
I now want to scale it to be the same aspect ratio/size on each screen.
I have tried doing that by using this code:
public Player(Vector2 position, float width, float height, float rotation, float speed)
{
super(speed, rotation, width, height, position);
}
public void update()
{
width = Gdx.graphics.getWidth() / 10;
height = Gdx.graphics.getHeight() / 10;
}
But this doesn't work, it does nothing.
I have also tried setting the width and height to a static value for example 100 by 100. But this does not work either.
Thank you for any help! :)
Edit: adding base and derived classes:
Here's the code for the Entity class:
public abstract class Entity {
public void setPosition(Vector2 position) {
this.position = position;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public Rectangle getBounds() {
return bounds;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
}
Here's the code for the MoveEntity class:
public MoveEntity(float speed, float rotation,
float width, float height,
Vector2 position) {
super(position, width, height);
this.speed = speed;
this.rotation = rotation;
vel = new Vector2(0, 0);
}
public Vector2 getVel() {
return vel;
}
public void setVel(Vector2 target) {
this.vel = target;
}
public float getRotation() {
return rotation;
}
public void setRotation(float r) {
..............///
maybe it do not speak English, but you talk about a sprite and I do not see him any site, anyway.
assuming this is the sprite to which you refer -> spr_player
You can do this in several ways but basing on this line of code that you publish
Variable class. (for test):
float width = Gdx.graphics.getWidth() / 10;
float height = Gdx.graphics.getHeight() / 10;
Try to use this:
sb.draw(spr_player, p.getPosition().x, p.getPosition().y
width, height);
this is the funtion in the Class SpriteBatch:
public void draw (Texture texture, float x, float y,
float width, float height)
also you could use, setSize in the Sprite class, somewhere before render.
spr_player.setSize(width, height);
this is the funtion in the Class Sprite:
public void setSize (float width, float height)
if the size will not change during the game maybe, you can call the setSize, in method created or show, or in the constructor call this way, if the result you want:
Sprite spr_player = new Sprite (your_texture, width, height);
this is the funtion in the Class Sprite:
public Sprite (Texture texture, int srcWidth, int srcHeight)
I've made a Button class which allows me to have buttons (Kind of obvious). But in my button class, I'm using an image to display the button on the screen. I got that to work, but I want to resize the image to the size of the button.
My "Image Resizer" works flawlessly, but when I try to resize the button, the button doesn't show up. I don't get any errors.
Here's my Button class:
private String text;
private int size = 0;
private BufferedImage buttonHD;
public Button(int x, int y, int width, int height, int size) {
super(x, y, width, height);
this.size = size;
buttonHD = Renderer.resizeImage(Images.button, x, y, width, height);
}
public Button setText(String text) {
this.text = text;
return this;
}
public void drawButton(Graphics g, int xoffset, int yoffset) {
int xx = x + xoffset;
int yy = y + yoffset;
if(!MouseInput.MOUSE.intersects(this)) {
g.drawImage(buttonHD, x, y, width, height, null);
} else if(MouseInput.MOUSE.intersects(this)){
g.setColor(Color.DARK_GRAY);
g.fillRect(x, y, width, height);
}
Renderer.drawText(text, g, xoffset, yoffset, size);//Draws button text
}
The original image that I'm resizing is stored into my Images class as:
public static BufferedImage button;
Here's my "Button Resizer" method:
public static BufferedImage resizeImage(BufferedImage origImg, int x, int y, int initWidth, int initHeight) {
BufferedImage resizedImg = new BufferedImage(initWidth, initHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = resizedImg.createGraphics();
g2d.drawImage(origImg, x, y, initWidth, initHeight, null);
g2d.dispose();
return resizedImg;
}
The way I'm using these buttons are in ScreenState classes. Each class representing as each state. The buttons are set in there and are loaded up by the class's constructor.
The buttons do work as they should, but the images just don't show up. If more code is needed, just let me know and I'll provide you with it.
I've been trying to fix this problem, but had no luck. If someone could just hint out as to where my problem is or maybe have a solution, that'd be great. Thanks!
This function resizes the BufferedImage to the given width and height:
public static BufferedImage resizeImage(BufferedImage image, int width, int height) {
// calculate the scale factor
double xScale = width / (double) image.getWidth();
double yScale = height / (double) image.getHeight();
// create the object that will contain the resized image
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
// resize the image
Graphics2D g = (Graphics2D) resizedImage.getGraphics();
g.scale(xScale, yScale);
g.drawImage(image, 0, 0, null);
g.dispose();
// return the resized image
return resizedImage;
}
Then simply use it:
public class MyButton extends JButton
{
private BufferedImage image;
public MyButton() {
image = resizeImage(ImageIO.read(IMAGE_PATH), BUTTON_WIDTH, BUTTON_HEIGHT);
}
#Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
IMAGE_PATH is the File where your image is located, BUTTON_WIDTH and BUTTON_HEIGHT is your button dimension.
I'm in the process of making a simple 2-D game, however I am having trouble drawing images. Below are a few classes that are relevant to the problem
private Vector<Bullet> ammo = new Vector<Bullet>(100);
public class Bullet{
Image img;
int x, y, speed;
boolean show;
Bullet(Image img,int x, int y, int speed, boolean show){
this.x = x;
this.y = y;
this.speed = speed;
this.img = img;
this.show = show;
}
public void draw(ImageObserver obs) {
if(show)
g2.drawImage(img, this.x, this.y, obs);
}
public void update(){
this.y -= 1;
}
}
public class Movement{
....
Movement(....){
.....
}
public void fly(){
......
ammo.add(new Bullet(bullet1, m.x, m.y, 7, true));
}
public class MyPlane {
KeyControl key;
Movement flight;
Image img;
int x, y, speed, move = 0;
int boom;
...
}
public void drawDemo() {
...
for(Bullet bullets: ammo)
bullets.update();
...
for(Bullet bullets: ammo)
bullets.draw(this);
}
}
When I call bullets.draw(this) nothing actually is drawn on the screen. I know however that the ammo vector does contain the correct information, such as the x coordinate, y coordinate... I'm using Graphics 2-D by the way. Any help and or suggestions would be greatly appreciated thanks.
public void paint(Graphics g) {
if(bimg == null) {
Dimension windowSize = getSize();
bimg = (BufferedImage) createImage(windowSize.width,
windowSize.height);
g2 = bimg.createGraphics();
}
drawDemo();
g.drawImage(bimg, 0, 0, this);
}
I think you should draw the image like this:
public void draw(Graphics g2) {
if(show)
g2.drawImage(img, this.x, this.y, null);
}
Then in your plane class you have to add Graphics as an argument of the drawDemo() method:
public void drawDemo(Graphics g2) {
...
for(Bullet bullets: ammo)
bullets.update();
...
for(Bullet bullets: ammo)
bullets.draw(g2);
}
}
and finally in you paint(Graphics g) method you call this:
public void paint(Graphics g) {
/*
...
*/
drawDemo(g2);
g.drawImage(bimg, 0, 0, this);
}
I need to create a rectangle object and then paint it to the applet using paint(). I tried
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
Then tried to paint it to the applet using
g.draw(r);
It didn't work. Is there a way to do this in java? I have scoured google to within an inch of its life for an answer, but I haven't been able to find an answer. Please help!
Try this:
public void paint (Graphics g) {
Rectangle r = new Rectangle(xPos,yPos,width,height);
g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
[edit]
// With explicit casting
public void paint (Graphics g) {
Rectangle r = new Rectangle(xPos, yPos, width, height);
g.fillRect(
(int)r.getX(),
(int)r.getY(),
(int)r.getWidth(),
(int)r.getHeight()
);
}
You may try like this:
import java.applet.Applet;
import java.awt.*;
public class Rect1 extends Applet {
public void paint (Graphics g) {
g.drawRect (x, y, width, height); //can use either of the two//
g.fillRect (x, y, width, height);
g.setColor(color);
}
}
where x is x co-ordinate
y is y cordinate
color=the color you want to use eg Color.blue
if you want to use rectangle object you could do it like this:
import java.applet.Applet;
import java.awt.*;
public class Rect1 extends Applet {
public void paint (Graphics g) {
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
g.setColor(color);
}
}
Note:drawRect and fillRect are different.
Draws the outline of the specified rectangle:
public void drawRect(int x,
int y,
int width,
int height)
Fills the specified rectangle. The rectangle is filled using the graphics context's current color:
public abstract void fillRect(int x,
int y,
int width,
int height)