import javax.swing.JPanel; import java.awt.Graphics; import java.io.IOException; import java.awt.Dimension;import javax.swing.BorderFactory; import java.awt.Color; import java.awt.BorderLayout; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.Point; public class BitmapPanel extends JPanel implements MouseListener{ public final static int STYLE_FILL_RECT=0; public final static int STYLE_SMILEY=1; private int pixelStyle=STYLE_FILL_RECT; /** * Get the PixelStyle value. * @return the PixelStyle value. */ public int getPixelStyle() { return pixelStyle; } /** * Set the PixelStyle value. * @param newPixelStyle The new PixelStyle value. */ public void setPixelStyle(int newPixelStyle) { this.pixelStyle = newPixelStyle; } // Implementation of java.awt.event.MouseListener /** * Describe <code>mouseClicked</code> method here. * * @param mouseEvent a <code>MouseEvent</code> value */ public void mouseClicked(MouseEvent mouseEvent) { } /** * Describe <code>mousePressed</code> method here. * * @param mouseEvent a <code>MouseEvent</code> value */ public void mousePressed(MouseEvent mouseEvent) { Point pix=pointToPixel(mouseEvent.getPoint()); bitmap.flip(pix.y, pix.x); repaint(); } /** * Describe <code>mouseReleased</code> method here. * * @param mouseEvent a <code>MouseEvent</code> value */ public void mouseReleased(MouseEvent mouseEvent) { } /** * Describe <code>mouseEntered</code> method here. * * @param mouseEvent a <code>MouseEvent</code> value */ public void mouseEntered(MouseEvent mouseEvent) { } /** * Describe <code>mouseExited</code> method here. * * @param mouseEvent a <code>MouseEvent</code> value */ public void mouseExited(MouseEvent mouseEvent) { } private Bitmap bitmap; public Point pointToPixel(Point coords){ return new Point( coords.x*bitmap.getColumns()/getWidth(), coords.y*bitmap.getRows()/getHeight()); } public BitmapPanel (String fileName) throws IOException{ bitmap=new Bitmap(fileName); setDefaultSize(); addMouseListener(this); setBorder(BorderFactory.createRaisedBevelBorder()); } public final int DEFAULT_PIXEL_WIDTH=10; public final int DEFAULT_PIXEL_HEIGHT=10; private void setDefaultSize(){ int height=bitmap.getRows()*DEFAULT_PIXEL_HEIGHT; int width=bitmap.getColumns()*DEFAULT_PIXEL_WIDTH; setPreferredSize(new Dimension(width,height)); } public void paintComponent(Graphics g){ int panelWidth,panelHeight; super.paintComponent(g); // make opaque g.setColor(getForeground()); for (int i=0; i<bitmap.getRows(); i++){ for (int j=0; j< bitmap.getColumns(); j++){ if (bitmap.isSet(i,j)){ drawPixel(g,i,j); } } } } private void drawPixel(Graphics g, int i, int j){ int pixelWidth=getWidth()/bitmap.getColumns(); int pixelHeight=getHeight()/bitmap.getRows(); if (pixelStyle==STYLE_FILL_RECT){ g.fillRect(j*pixelWidth, i*pixelHeight, pixelWidth, pixelHeight); } else { Smiley s=new Smiley(pixelWidth,pixelHeight); s.draw(g,j*pixelWidth, i*pixelHeight); } } public static void main(String [] args) throws IOException{ DBFrame f=new DBFrame(new BorderLayout()); BitmapPanel panel=new BitmapPanel("noisy.pbm"); panel.setPixelStyle(STYLE_SMILEY); f.getContentPane().add(panel,"Center"); f.pack(); f.setVisible(true); } }