Mega Code Archive

 
Categories / Java / 2D Graphics GUI
 

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(255, 0, 0, 150);   private static Color green = new Color(0, 255, 0, 150);   private static Color blue = new Color(0, 0, 255, 150);   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 rntsoftLogo = new ImageIcon("rntsoftLogo.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(0, 0, 240, 240);       System.out.println(" yellow Oval repainted.");     }     if (clipx + clipw >= 160 && clipx <= 400 && clipy + cliph >= 160         && clipy <= 400) {       g.setColor(Color.magenta);       g.fillOval(160, 160, 240, 240);       System.out.println(" magenta Oval repainted.");     }     int iconWidth = rntsoftLogo.getIconWidth();     int iconHeight = rntsoftLogo.getIconHeight();     if (clipx + clipw >= 280 - (iconWidth / 2) && clipx <= (280 + (iconWidth / 2))         && clipy + cliph >= 120 - (iconHeight / 2) && clipy <= (120 + (iconHeight / 2))) {       rntsoftLogo.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))) {       rntsoftLogo.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(60, 220, 120, 120);       System.out.println(" red Rect repainted.");     }     if (clipx + clipw > 140 && clipx < 260 && clipy + cliph > 140         && clipy < 260) {       g.setColor(green);       g.fillOval(140, 140, 120, 120);       System.out.println(" green Oval repainted.");     }     if (clipx + clipw > 220 && clipx < 380 && clipy + cliph > 60         && clipy < 180) {       g.setColor(blue);       g.fillRect(220, 60, 120, 120);       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(400, 400);   }   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);   } }