2012-06-29 51 views
7

bien lo que estoy haciendo un juego, y estoy tratando de modificar la imagen original del marcador éxito mediante la adición de texto en él, y estoy usando el siguiente código:BufferedImage produciendo fondo negro

import javax.swing.ImageIcon; 
import javax.swing.Timer; 

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
public class HitMarker { 

    public static final Image rangeHitMarker = new ImageIcon(HitMarker.class.getResource("rangeHitMarker.png")).getImage(); 
    public static final Image magicHitMarker = new ImageIcon(HitMarker.class.getResource("magicHitMarker.png")).getImage(); 
    public static final Image monsterHitMarker = new ImageIcon(HitMarker.class.getResource("monsterHitMarker.png")).getImage(); 

    public static final Font font = new Font("Tahoma", Font.PLAIN, 10); 

    public static final Color t = new Color(0,0,0,0); 

    public Image hitMarker; 
    public BufferedImage image; 
    public String hit; 

    public int attackStyle; 

    public boolean rangeAttack; 
    public int x; 
    public int y; 

    public Timer timer; 
    public boolean remove; 

    public HitMarker(int x, int y, int hit, int attackStyle){ 
     this.hit = String.format("%d", hit); 
     this.remove = false; 
     this.x = x; 
     this.y = y; 
     this.attackStyle = attackStyle; 
     this.hitMarker = getImage(); 
     BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_RGB); 
     Graphics2D g = bi.createGraphics(); 
     g.drawImage(hitMarker, 0, 0, null); 
     g.setFont(font); 
     g.setColor(Color.WHITE); 
     g.drawString(this.hit, 18, 13); 
     g.dispose(); 
     image = bi; 
     timer = new Timer(800, 
       new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       remove = true; 
       timer.stop(); 
      } 
     } 
     ); 
     timer.setInitialDelay(800); 
     timer.start(); 
    } 

    public HitMarker(int x, int y, int hit){ 
     this.hit = String.format("%d", hit); 
     this.remove = false; 
     this.x = x; 
     this.y = y; 
     this.hitMarker = monsterHitMarker; 
     BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_RGB); 
     Graphics2D g = bi.createGraphics(); 
     g.drawImage(hitMarker, 0, 0, null); 
     g.setFont(font); 
     g.setColor(Color.WHITE); 
     g.drawString(this.hit, 18, 13); 
     g.dispose(); 
     image = bi; 
     timer = new Timer(800, 
       new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       remove = true; 
       timer.stop(); 
      } 
     } 
     ); 
     timer.setInitialDelay(800); 
     timer.start(); 
    } 

    public boolean isRangeAttack(){ 
     return attackStyle == AttackStyleConstants.RANGE || attackStyle == AttackStyleConstants.RANGE_DEFENCE ? true : false; 
    } 

    public Image getImage(){ 
     return isRangeAttack() ? rangeHitMarker : magicHitMarker; 
    } 

} 

Centrándome particularmente en cualquiera de los constructores: Y el error que estoy teniendo es que cuando creo BufferedImage y dibujo la imagen en la imagen en búfer, está creando un fondo negro automáticamente y no sé por qué. Intenté investigar sobre este tema y algunos dicen que cambian algo sobre AlphaComposite y el método g.clearRect(), pero ninguno de estos parece funcionar. Por cierto, la imagen que estoy pintando en la imagen almacenada en el búfer es 35x20 (que es las dimensiones de la imagen almacenada en el búfer) y tiene un fondo transparente. Si alguien puede decirme cómo eliminar este fondo negro, sería muy apreciado, gracias.

+1

para una mejor ayuda antes publicar un [SSCCE] (http://sscce.org/), dos razones ---> la mayoría de las respuestas no van al 3er lado de los enlaces, 2) para los lectores de futuros – mKorbel

+0

¿Pensé que era así? –

+0

@JoshM Quería decir, es mejor publicar el código directamente aquí, o solo la parte del problema y un enlace a la fuente completa, por lo que si el enlace se apaga, su pregunta no se volverá inútil en el futuro. – Andrew

Respuesta

16

Probar BufferedImage.TYPE_INT_ARGB. Esto hará que las regiones sean transparentes en lugar de negras.

+0

Oh, gracias, funciona. –

+0

ya que tengo el mismo problema que probé anteriormente, la solución no funcionó, en realidad quiero que el fondo sea gris claro en lugar de negro. ¿Puede alguien decirme cómo hacer eso también? – Lasan

4

Es posible que desee para tratar de almacenar el canal alfa, así,

BufferedImage bi = new BufferedImage(35, 20, BufferedImage.TYPE_INT_ARGB); 
0

Uso png en lugar de JPEG. Png es muy adecuado para operaciones de transparencia. Aquí hay un fragmento de código de exportación png simple;

 BufferedImage bImage = new BufferedImage(640, 480, BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2d = (Graphics2D) bImage.getGraphics(); 
     DrawingContext context = new DrawingContext(g2d); 
     plot.draw(context); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     DrawableWriter wr = DrawableWriterFactory.getInstance().get("image/png"); 

     wr.write(plot, baos, 640, 480); 
     baos.flush(); 

     baos.close(); 
     InputStream inputStream = new ByteArrayInputStream(baos.toByteArray()); 
     BufferedImage bufferedImage = ImageIO.read(inputStream); 

     ImageIO.write(bufferedImage,"png",new File(outputFolder.getPath()+"/result.png")); 
1

Si necesita un JPG con fondo blanco, lo que necesita para dibujar la imagen como esta:

g.drawImage(hitMarker, 0, 0, Color.WHITE, null); 

esta manera se evita el fondo negro cuando se pasa de PNG a JPG.

Cuestiones relacionadas