How to make a class to manipulate Graphics2d in Paint?

Asked

Viewed 175 times

-1

In the following code I tried to create a Sprite class to manipulate the drawings, its function would be to transform the sprites into Graphics2d and use it in the Paint. But the only way I could find to connect them was by doing this:

public Sprite(Graphics g) {
    g2d = (Graphics2D) g;
}

The problem is that in Title code, when the load = true occurs, it does not go through the Sprite object initialization, the drawImage does not work, so in the first Paint it does everything right, in the second Paint with the load = true, everything adds up and the draw does not work. Any idea how to fix this?


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;

import engine.Display;
import engine.Sprite;

public class Title extends JPanel implements ActionListener, KeyListener{

    // IMAGENS

    Sprite city1;
    Sprite city2;
    Sprite tipo;

    private int phase = 1;
    private boolean load = false;

    public Title(Display window){

        setSize( 800, 600 );
        setBackground(Color.WHITE);
        Timer t = new Timer(20, this);
        t.start();
        window.addKeyListener(this);
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        if (load == false) {

            city1 = new Sprite(g);
            city2 = new Sprite(g);
            tipo = new Sprite(g);

            city1.setImage("images/scene/logo2.png");
            city2.setImage("images/scene/logo2.png");
            tipo.setImage("images/scene/tipo.png");

            city1.setLocation((800/2) - (city1.getWidth()/2), 150 + (tipo.getHeight()/2));
            city2.setLocation((800/2) - (city2.getWidth()/2), 150 + (tipo.getHeight()/2));
            tipo.setLocation ((800/2) - (city2.getWidth()/2), 150);

            city1.setHeight(0);
            city2.setHeight(0);

            city1.setAnimation(true);
            city2.setAnimation(true);

            city1.setMotionAnimation(0, 0, 0, -1, 0);
            city2.setMotionAnimation(0, 0, 0, +1, 0);

            city1.draw();
            city2.draw();  
            tipo.draw();

            load = true;

        }

        city1.draw();
        city2.draw();  
        tipo.draw();

    }

    public void actionPerformed(ActionEvent e) {
        if (phase == 1){
            repaint();  
        } 
    }

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Sprite {

    private BufferedImage image;
    private int width;
    private int height;
    private int x = 0;
    private int y = 0;
    private String string;
    private boolean visibility;
    private float alpha = 1f;
    private Font font;
    private Graphics2D g2d;

    private boolean animation = false;
    private int spd_x = 0;
    private int spd_y = 0;
    private int spd_width = 0;
    private int spd_height = 0;
    private float spd_alpha = 0;

    public Sprite(Graphics g) {
        g2d = (Graphics2D) g;
    }

    public void setImage(String path){
        try {
            image = ImageIO.read(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Arquivo (" + path + ") não encontrado!");
        }

        this.setHeight(image.getHeight());
        this.setWidth(image.getWidth());
    }

    public void setString(String string, Font font, float alpha){
        this.font = font;
        this.alpha = alpha;
        this.string = string;
        g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setFont(font);
    }

    public void setLocation(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setDimension(int width, int height){
        this.width = width;
        this.height = height;
    }

    public void setMotionAnimation(int spd_x, int spd_y, int spd_width, int spd_height, float spd_alpha){
        this.spd_x = spd_x;
        this.spd_y = spd_y;
        this.spd_width = spd_width;
        this.spd_height = spd_height;
        this.spd_alpha = spd_alpha;
    }

    public void setAnimation (boolean animation){
        this.animation = animation;
    }

    public void draw() {
        if (image != null){
            if (animation == true) {
                this.x += this.spd_x;
                this.y += this.spd_y;
                this.width += this.spd_width;
                this.height += this.spd_height;
                this.alpha += this.spd_alpha;
            }
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            g2d.drawImage(image, x, y, width, height, null);
        } else if (string != null){
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            g2d.drawString(string, x, y);
        }
    }

1 answer

0

I solved the problem by making the draw() command receive the Graphics g and after that I did the g2d variable again, as I did at the Sprite initiation.

public void draw(Graphics g)
g2d = (Graphics2D) g

But if there is any hint to make this class correctly, I accept.

  • It makes no sense to pass Graphics in the constructor, since you only need it to draw in the draw method. This is the correct way, remove it from the constructor and leave the constructor only to define object properties and leave only in this draw method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.