import java.awt.geom.*; import java.awt.image.*; import javax.swing.*; import java.awt.*; class texture extends JPanel { Rectangle r1 = new Rectangle(0, 0, 400, 400); public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; super.paintComponent(g); Image b = new ImageIcon("bvz.jpg").getImage(); // the third argument tells Java how to interpret the pixels in // the image. See the documentation for BufferedImage for details. BufferedImage bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB); // get a graphics2D object that will allow us to write into the // buffered image. Extract the portion of the image that starts // at (100, 100) and goes to (300, 300). Graphics2D big = bi.createGraphics(); big.drawImage(b, 0, 0, 200, 200, 100, 100, 300, 300, Color.BLACK, null); // scale the image size by 50% (we're fitting a 200x200 image into // a (100x100) image and start texturing at location (250, 250). Note // that the location means the location on the screen, not the // location in the image. TexturePaint t = new TexturePaint(bi, new Rectangle2D.Double(250, 250, 100, 100)); g2.setPaint(t); g2.fill(r1); } public Dimension getPreferredSize() { return new Dimension(400, 400); } public static void main(String argv[]) { JFrame myFrame = new JFrame(); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.getContentPane().add(new texture()); myFrame.pack(); myFrame.setVisible(true); } }