Skip to main content

Textures swing


                  Textures in java Swing

  A texture is a bitmap image applied to the surface in computer graphics. Besides colors and gradients, we can fill our graphics shapes with textures.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class Textures extends JPanel {
    public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(new Color(212, 212, 212));
            g2d.drawRect(10, 15, 90, 60);
            g2d.drawRect(130, 15, 90, 60);
            g2d.drawRect(250, 15, 90, 60);
            g2d.drawRect(10, 105, 90, 60);
            g2d.drawRect(130, 105, 90, 60);
            g2d.drawRect(250, 105, 90, 60);
            BufferedImage bimage1 = null;
            BufferedImage bimage2 = null;
            BufferedImage bimage3 = null;
            BufferedImage bimage4 = null;
            BufferedImage bimage5 = null;
            BufferedImage bimage6 = null;
            URL url1 = ClassLoader.getSystemResource("texture1.png");
            URL url2 = ClassLoader.getSystemResource("texture2.png");
            URL url3 = ClassLoader.getSystemResource("texture3.png");
            URL url4 = ClassLoader.getSystemResource("texture4.png");
            URL url5 = ClassLoader.getSystemResource("texture5.png");
            URL url6 = ClassLoader.getSystemResource("texture6.png");
            try {
                bimage1 = ImageIO.read(url1);
                bimage2 = ImageIO.read(url2);
                bimage3 = ImageIO.read(url3);
                bimage4 = ImageIO.read(url4);
                bimage5 = ImageIO.read(url5);
                bimage6 = ImageIO.read(url6);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            Rectangle rect1 = new Rectangle(0, 0,
                bimage1.getWidth(), bimage1.getHeight());
            Rectangle rect2 = new Rectangle(0, 0,
                bimage2.getWidth(), bimage2.getHeight());
            Rectangle rect3 = new Rectangle(0, 0,
                bimage3.getWidth(), bimage3.getHeight());
            Rectangle rect4 = new Rectangle(0, 0,
                bimage4.getWidth(), bimage4.getHeight());
            Rectangle rect5 = new Rectangle(0, 0,
                bimage5.getWidth(), bimage5.getHeight());
            Rectangle rect6 = new Rectangle(0, 0,
                bimage6.getWidth(), bimage6.getHeight());
            TexturePaint texture1 = new TexturePaint(bimage1, rect1);
            TexturePaint texture2 = new TexturePaint(bimage2, rect2);
            TexturePaint texture3 = new TexturePaint(bimage3, rect3);
            TexturePaint texture4 = new TexturePaint(bimage4, rect4);
            TexturePaint texture5 = new TexturePaint(bimage5, rect5);
            TexturePaint texture6 = new TexturePaint(bimage6, rect6);
            g2d.setPaint(texture1);
            g2d.fillRect(10, 15, 90, 60);
            g2d.setPaint(texture2);
            g2d.fillRect(130, 15, 90, 60);
            g2d.setPaint(texture3);
            g2d.fillRect(250, 15, 90, 60);
            g2d.setPaint(texture4);
            g2d.fillRect(10, 105, 90, 60);
            g2d.setPaint(texture5);
            g2d.fillRect(130, 105, 90, 60);
            g2d.setPaint(texture6);
            g2d.fillRect(250, 105, 90, 60);
    }
    public static void main(String[] args) {
        Textures rects = new Textures();
        JFrame frame = new JFrame("Textures");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(rects);
        frame.setSize(360, 210);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

In our example, we will draw six rectangles filled with different textures. To work with textures, Java Swing has a TexturePaint class. 

BufferedImage bimage1 = null;
...
URL url1 = ClassLoader.getSystemResource("texture1.png");
...
bimage1 = ImageIO.read(url1);

We read an image into the memory.

Rectangle rect1 = new Rectangle(0, 0,
    bimage1.getWidth(), bimage1.getHeight());

We get the size of the texture image.
 
TexturePaint texture1 = new TexturePaint(bimage1, rect1);

Here we create a TexturePaint object. The parameters are a buffered image and a rectangle of the image. The rectangle is used to anchor and replicate the image. The images are tiled.

g2d.setPaint(texture1);

g2d.fillRect(10, 15, 90, 60);

Join us on Facebook: Click Here 

Comments

Popular posts from this blog

Menu bar example with applet viewer in java

      Menu bar +applet viewer example-3 import java.applet.*; import java.awt.*;  /*   <applet code="MenuDemo" height=200 width=200>   </applet> */ public class MenuDemo extends Applet {             public void init( )             {                         Menus m = new Menus("Menu Bar Demo");                                    m.resize(200,200);       m.show();             } } class Menus extends Frame {             Menus(String s)             {                         super(s);                         Me...

Java question

  Java ques interview Ques:-List any five features of Java?  Ans :-Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded Ques . write full form of jdbc & odbc ?  Ans :-  JDBC -java database connectivity.             ODBC - open database connectivity Ques:- what are types of packages ? Ans: 1. Pre-defined packages 2. User defined packages Ques :-Define class?  Ans:- A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object. Ques:- Denfie string bulider? Ans :- Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects. Ques :- What is Singleton class?  Ans:Singleton class control object creation, limiting the number to one but allowing the flexibility to create m...

MVC Example 1

                        Swing MVC Example in java Java Swing first programs In this chapter, we will program our first programs in Swing toolkit. The examples are going  to be very simple. We will cover some basic functionality. Our first example In our first example, we will show a basic window. import javax.swing.JFrame; public class Simple extends JFrame { public Simple() { setSize(300, 200); setTitle("Simple"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { Simple simple = new Simple(); simple.setVisible(true); } } While this code is very small, the application window can do quite a lot. It can be resized, maximized, minimized. All the complexity that comes with it has been hidden from the application programmer. import javax.swing.JFrame; Here we import the JFrame widget. It is a toplevel container, which is used for plac...