Clip the area : Clip « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Book
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » ClipScreenshots 
Clip the area
Clip the area
   
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class ClipDemo extends JComponent {
  private static Color red = new Color(25500150);

  private static Color green = new Color(02550150);

  private static Color blue = new Color(00255150);

  private static Font monoFont = new Font("Monospaced", Font.BOLD
      | Font.ITALIC, 36);

  private static Font sanFont = new Font("SanSerif", Font.PLAIN, 12);

  private static Font serifFont = new Font("Serif", Font.BOLD, 24);

  private static ImageIcon java2sLogo = new ImageIcon("java2sLogo.gif");

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // get damaged region
    Rectangle clipRect = g.getClipBounds();
    int clipx = clipRect.x;
    int clipy = clipRect.y;
    int clipw = clipRect.width;
    int cliph = clipRect.height;

    // fill damaged region only
    g.setColor(Color.white);
    g.fillRect(clipx, clipy, clipw, cliph);

    if (clipx <= 240 && clipy <= 240) {
      g.setColor(Color.yellow);
      g.fillOval(00240240);
      System.out.println(" yellow Oval repainted.");
    }

    if (clipx + clipw >= 160 && clipx <= 400 && clipy + cliph >= 160
        && clipy <= 400) {
      g.setColor(Color.magenta);
      g.fillOval(160160240240);
      System.out.println(" magenta Oval repainted.");
    }

    int iconWidth = java2sLogo.getIconWidth();
    int iconHeight = java2sLogo.getIconHeight();

    if (clipx + clipw >= 280 (iconWidth / 2&& clipx <= (280 (iconWidth / 2))
        && clipy + cliph >= 120 (iconHeight / 2&& clipy <= (120 (iconHeight / 2))) {
      java2sLogo.paintIcon(this, g, 280 (iconWidth / 2)120 (iconHeight / 2));
      System.out.println(" logo below blue Rect repainted.");
    }

    if (clipx + clipw >= 120 (iconWidth / 2&& clipx <= (120 (iconWidth / 2))
        && clipy + cliph >= 280 (iconHeight / 2&& clipy <= (280 (iconHeight / 2))) {
      java2sLogo.paintIcon(this, g, 120 (iconWidth / 2)280 (iconHeight / 2));
      System.out.println(" logo below red Rect repainted.");
    }

    if (clipx + clipw >= 60 && clipx <= 180 && clipy + cliph >= 220
        && clipy <= 340) {
      g.setColor(red);
      g.fillRect(60220120120);
      System.out.println(" red Rect repainted.");

    }

    if (clipx + clipw > 140 && clipx < 260 && clipy + cliph > 140
        && clipy < 260) {
      g.setColor(green);
      g.fillOval(140140120120);
      System.out.println(" green Oval repainted.");

    }

    if (clipx + clipw > 220 && clipx < 380 && clipy + cliph > 60
        && clipy < 180) {
      g.setColor(blue);
      g.fillRect(22060120120);
      System.out.println(" blue Rect repainted.");
    }

    g.setColor(Color.black);

    g.setFont(monoFont);
    FontMetrics fm = g.getFontMetrics();
    iconWidth = fm.stringWidth("Java Source");
    iconHeight = fm.getAscent();
    int d = fm.getDescent();
    if (clipx + clipw > 120 (iconWidth / 2&& clipx < (120 (iconWidth / 2))
        && clipy + cliph > (120 (iconHeight / 4)) - iconHeight
        && clipy < (120 (iconHeight / 4)) + d) {
      g.drawString("Java Source"120 (iconWidth / 2)120 (iconHeight / 4));
      System.out.println(" Java Source repainted.");
    }

    g.setFont(sanFont);
    fm = g.getFontMetrics();
    iconWidth = fm.stringWidth("and");
    iconHeight = fm.getAscent();
    d = fm.getDescent();
    if (clipx + clipw > 200 (iconWidth / 2&& clipx < (200 (iconWidth / 2))
        && clipy + cliph > (200 (iconHeight / 4)) - iconHeight
        && clipy < (200 (iconHeight / 4)) + d) {
      g.drawString("and"200 (iconWidth / 2)200 (iconHeight / 4));
      System.out.println(" and repainted.");
    }

    g.setFont(serifFont);
    fm = g.getFontMetrics();
    iconWidth = fm.stringWidth("Support.");
    iconHeight = fm.getAscent();
    d = fm.getDescent();

    if (clipx + clipw > 280 (iconWidth / 2&& clipx < (280 (iconWidth / 2))
        && clipy + cliph > (280 (iconHeight / 4)) - iconHeight
        && clipy < (280 (iconHeight / 4)) + d) {
      g.drawString("Support."280 (iconWidth / 2)280 (iconHeight / 4));
      System.out.println(" Support. repainted.");
    }
  }

  public Dimension getPreferredSize() {
    return new Dimension(400400);
  }

  public Dimension getMinimumSize() {
    return getPreferredSize();
  }

  public static void main(String args[]) {
    JFrame mainFrame = new JFrame();
    mainFrame.getContentPane().add(new ClipDemo());
    mainFrame.pack();

    mainFrame.setVisible(true);
  }

}

           
         
    
    
  
Related examples in the same category
1.Clip ImageClip Image
2.Clip another areaClip another area
3.Setting the Clipping Area with a Shape
4.Copy Area Performance
5.Clipping is restricting of drawing to a certain area.Clipping is restricting of drawing to a certain area.
6.set Clip and get Clip
7.Represents a clipping rectangle in a prefuse DisplayRepresents a clipping rectangle in a prefuse Display
8.Clips the specified line to the given rectangle.
ww___w_._jav_a___2s__.__co_m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.