2D-Grafik Massive Frame Drops beim Benutzen von AffineTransformOp

Diskutiere Massive Frame Drops beim Benutzen von AffineTransformOp im AWT, Swing, JavaFX & SWT Bereich.
R

realodds

Wie der Titel schon sagt, habe ich massive frames drops beim folgenden Code:
Java:
// Class bat
    public AffineTransformOp getOp() {
        double degrease = this.getDegree(p.getX() - pos.x, p.getY() - pos.y);
        double rotationRequired = Math.toRadians(degrease);
        double locationX = getImg().getWidth() / 2;
        double locationY = getImg().getHeight() / 2;
        AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        return op;
    }
Java:
// Class frame
g2.drawImage(bat.getOp().filter(bat.getImg(), null), bat.getX(), bat.getY(),
                        Bat.getWidth(), Bat.getHeight(), null);
                        
// Der folgende code zeichnet das gleiche, nur ohne den AffineTransformOP
g2.drawImage(bat.getImg(), batgetX(), bat.getY(), Bat.getWidth(), Bat.getHeight(), null);
// dort habe ich keine frame drops
Warum laggt das so oder wie geht es eine lagfreie Methode, ein Bild zu drehen?
 
Blender3D

Blender3D

Warum laggt das so oder wie geht es eine lagfreie Methode, ein Bild zu drehen?
Hier ein Beispiel für ein RotationsImage. Probiere es aus. ;)


Java:
import javax.swing.JFrame;

public class start {
    public static void main(String[] args) {
        JFrame frame = new JFrame("rotate");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TestRotationPanel(500, 500));
        frame.pack();
        frame.setVisible(true);
    }
}
Java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class TestRotationPanel extends JPanel implements Runnable {
    private RotationImage image;
    private boolean running;
    private Thread animator;

    public TestRotationPanel(int width, int height) {
        setPreferredSize(new Dimension(width, height));
    }

    @Override
    public void addNotify() {
        super.addNotify();
        image = new RotationImage(getImage(100, 100));
        image.setPosition(100, 100);
        start();
    }

    private BufferedImage getImage(int width, int height) {
        BufferedImage img = (BufferedImage) createImage(width, height);
        Graphics g = img.getGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, width, height);
        g.setColor(Color.WHITE);
        g.drawRect(1, 1, width - 2, height - 2);
        g.fillOval(width / 4, height / 4, width / 10, height / 10);
        g.dispose();
        return img;
    }

    @Override
    public void paintComponent(Graphics g) {
        g.fillRect(0, 0, getWidth(), getHeight());
        image.draw(g);
    }

    @Override
    public void run() {
        while (running) {
            image.setAngle(image.getAngle() + 1f);
            repaint();
            try {
                Thread.sleep(12);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void start() {
        running = false;
        while (animator != null && animator.isAlive())
            ;
        animator = new Thread(this);
        running = true;
        animator.start();
    }

    public void stop() {
        running = false;
    }
}
Java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

public class RotationImage {
    private BufferedImage image;
    private float x = 0;
    private float y = 0;
    private float angle = 0;

    public RotationImage(BufferedImage image) {
        createImage(image);
    }

    private void createImage(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        int dim = (int) Math.sqrt(width * width + height * height);
        this.image = new BufferedImage(dim, dim, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = (Graphics2D) this.image.getGraphics();
        g2.setBackground(new Color(0, true));// transparent background
        g2.clearRect(0, 0, dim, dim);
        g2.drawImage(image, (dim - width) / 2, (dim - height) / 2, null);
        g2.dispose();
    }

    public void draw(Graphics g) {
        if (image == null)
            return;
        BufferedImage img = (angle != 0) ? getRotatedImage(image) : image;
        g.drawImage(img, (int) x, (int) y, null);
    }

    private BufferedImage getRotatedImage(BufferedImage img) {
        AffineTransform aT = AffineTransform.getRotateInstance(angle, img.getWidth() / 2, img.getHeight() / 2);
        AffineTransformOp op = new AffineTransformOp(aT, AffineTransformOp.TYPE_BILINEAR);
        return op.filter(img, null);
    }

    public float getAngle() {
        return (float) Math.toDegrees(angle);
    }

    public int getHeight() {
        return image.getHeight();
    }

    public int getWidth() {
        return image.getWidth();
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public void setAngle(float angle) {
        this.angle = (float) Math.toRadians(angle % 360);
    }

    public void setPosition(float x, float y) {
        this.x = x;
        this.y = y;
    }
}
 
Thema: 

Massive Frame Drops beim Benutzen von AffineTransformOp

Passende Stellenanzeigen aus deiner Region:
Anzeige

Neue Themen

Anzeige

Anzeige
Oben