2013-12-19 5 views
0

BufferedImage의 하위 영역에 색상을 지정하고 싶습니다. 현재 수행 중입니다.BufferedImage 영역에 색칠

public BufferedImage paintSubImage(BufferedImage img, int x, int y, int w, int h) { 
    Graphics g = img.getGraphics(); 
    g.setColor(Color.BLACK); 
    g.fillRect(x,y,w,h); 
    return img; 
} 

하지만 색상을 지정할 수 없습니다. 내가 잘못하고 있니?

+1

Splat

나에게 좋아 보이는 .../닫기는 폐기해야한다. 문제를 나타내는 'SSCCE'를 게시하면 BufferedImage의 일부를 칠한 후에 BufferedImage를 사용하는 방법의 컨텍스트를 볼 수 있습니다. – camickr

+0

@camickr :이 메서드는 전달 된 원래 BufferedImage 개체를 변경합니까 아니면 무색의 BufferedImage를 반환합니까? –

+0

원본 BufferedImage가 변경됩니다. – camickr

답변

1

계속 진행할 문맥이 거의 없거나 전혀 없다는 사실을 감안할 때, 정확히 무엇이 잘못되었는지를 아는 것은 어렵습니다. 당신은/그것을 개방 만드는 경우 엄지 손가락의

일반 규칙하지만, 당신은

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class ColorMe { 

    public static void main(String[] args) { 
     new ColorMe(); 
    } 

    public ColorMe() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private BufferedImage img; 

     public TestPane() { 
      try { 
       img = ImageIO.read(new File("/path/to/image...")); 

       addMouseListener(new MouseAdapter() { 

        @Override 
        public void mouseClicked(MouseEvent e) { 
         Point p = e.getPoint(); 
         Rectangle bounds = getPictureBounds(); 
         if (bounds.contains(p)) { 

          int x = p.x - bounds.x; 
          int y = p.y - bounds.y; 

          splat(img, x, y); 
          repaint(); 

         } 
        } 

       }); 

      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 

     protected void splat(BufferedImage img, int x, int y) { 
      Graphics2D g2d = img.createGraphics(); 
      g2d.setColor(Color.RED); 
      g2d.fillOval(x - 10, y - 10, 20, 20); 
      g2d.dispose(); 
     } 

     protected Rectangle getPictureBounds() { 

      Rectangle bounds = new Rectangle(); 

      if (img != null) { 

       bounds.x = (getWidth() - img.getWidth())/2; 
       bounds.y = (getHeight() - img.getHeight())/2; 
       bounds.width = img.getWidth(); 
       bounds.height = img.getHeight(); 

      } 

      return bounds; 

     } 

     @Override 
     public Dimension getPreferredSize() { 
      return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight()); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (img != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       Rectangle bounds = getPictureBounds(); 
       g2d.drawImage(img, bounds.x, bounds.y, this); 
       g2d.dispose(); 
      } 
     } 
    } 

}