
Когда я использую эту строку кода
Код: Выделить всё
g2.drawImage(image, (int)x, (int)y, width, height, null);

Я знаю, что есть способ сгладить фигуру g2d с помощью renderingHints, но я не уверен, влияет ли это на форму изображения...
Итак, любая помощь приветствуется!
Полный код ниже:
Код: Выделить всё
package entity;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import main.Game;
import main.GamePanel;
// Module HP
// Acceleration and speed
public class Player extends Plane{
private BufferedImage basic;
// acceleration vector lists
private final ArrayList accelerations;
// rate of increasing speed
// 1.0 / (time to reach max speed in seconds) * FPS
private final double ACCCONST = 1.0/(0.5*Game.FPS);
// special attributes
//TEMP
private final ArrayList bullets;
public Player(GamePanel gp) {
this.loadImages();
this.gp = gp;
// set image
image = basic;
// basic attributes
x = Game.SPAWNX - width/2;
y = Game.SPAWNY - height;
// init lists
accelerations = new ArrayList();
//TEMP
bullets = new ArrayList();
}
private void loadImages() {
try {
basic = ImageIO.read(getClass().getResourceAsStream("/res/players/basic.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
// setters
public void addAcceleration(double[] pair) {
if (pair.length == 2) {
accelerations.add(pair);
}
}
@Override
public void setEnginePower(double enginePower) {
this.enginePower = enginePower;
this.maxSpeed = enginePower;
}
@Override
public void setAtkSpeed(double atkSpeed) {
this.atkSpeed = atkSpeed;
if (atkSpeed == 0) atkInterval = (int)Double.POSITIVE_INFINITY;
else atkInterval = (int)(Game.FPS/atkSpeed);
}
// getters
// get movement info
private double[] getRAcceleration() {
// get resultant acceleration
xAPF = 0; yAPF = 0;
for (double[] pair: accelerations) {
if (pair.length == 2) {
xAPF += pair[0];
yAPF += pair[1];
}
}
accelerations.clear();
return new double[]{xAPF, yAPF};
}
private void move() {
double[] pair = this.getRAcceleration();
// no acceleration
if (pair[0] == 0 && pair[1] == 0) {
// reset speed to 0
if (xSPF > 0) xSPF = Math.max(xSPF - Game.DECELERATION, 0);
else if (xSPF < 0) xSPF = Math.min(xSPF + Game.DECELERATION, 0);
if (ySPF > 0) ySPF = Math.max(ySPF - Game.DECELERATION, 0);
else if (ySPF < 0) ySPF = Math.min(ySPF + Game.DECELERATION, 0);
}
// accleration exists
else {
// max speed = acceleration
if (xAPF > 0) xSPF = Math.min(xSPF + xAPF*ACCCONST, maxSpeed);
else xSPF = Math.max(xSPF + xAPF*ACCCONST, -maxSpeed);
if (yAPF > 0) ySPF = Math.min(ySPF + yAPF*ACCCONST, maxSpeed);
else ySPF = Math.max(ySPF + yAPF*ACCCONST, -maxSpeed);
}
// move
x += xSPF;
y += ySPF;
}
private void shoot() {
double spawnX = x + singleShootingScale * width;
double spawnY = y;
Bullet bullet = new Bullet(gp, this, spawnX, spawnY);
bullet.addAcceleration(new double[]{0, -10});
bullets.add(bullet);
}
private void getAFromKey() {
boolean[] keyPressed = gp.keyH.keyPressed;
if (keyPressed[0]) accelerations.add(new double[]{0, -enginePower / Game.FPS});
if (keyPressed[1]) accelerations.add(new double[]{-enginePower / Game.FPS, 0});
if (keyPressed[2]) accelerations.add(new double[]{0, enginePower / Game.FPS});
if (keyPressed[3]) accelerations.add(new double[]{enginePower / Game.FPS, 0});
}
@Override
public void update() {
// check all events
//TEMP
for (Bullet bullet: bullets) bullet.update();
// shoot
if (atkInterval == 0) this.shoot();
else if (frameCnt % atkInterval == 0) this.shoot();
this.getAFromKey();
this.move();
frameCnt += 1;
}
@Override
public void draw(Graphics2D g2) {
//TEMP
for (Bullet bullet: bullets) bullet.draw(g2);
g2.drawImage(image, (int)x, (int)y, width, height, null);
}
}
Если есть на Java это невозможно сделать, возможно, я так рисую? (Какой лучший способ добавить искусство в дизайн игры Java)
Подробнее здесь: https://stackoverflow.com/questions/781 ... graphics2d