package exercicio3_2; import java.awt.Color; import java.awt.Dimension; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Polygon; import java.awt.TexturePaint; import java.awt.geom.Area; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.JPanel; public class Exercicio3_2 extends JApplet { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("Exercicio3_2"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new Exercicio3_2(); applet.init(); frame.getContentPane().add(applet); frame.pack(); frame.setVisible(true); } public void init() { JPanel panel = new Exercicio3_2Panel(); getContentPane().add(panel); } } class Exercicio3_2Panel extends JPanel { private BufferedImage imagem; public Exercicio3_2Panel() { setPreferredSize(new Dimension(400, 400)); URL url = getClass().getClassLoader().getResource("images/foto.jpg"); try { imagem = ImageIO.read(url); } catch (IOException ex) { ex.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; Rectangle2D rect = new Rectangle2D.Double(80, 80, 40, 40); Rectangle2D rectangulo = new Rectangle2D.Double(37.5, 37.5, 75, 75); Polygon poly = new Polygon(); for (int i = 0; i < 8; i++) { poly.addPoint((int) (100 + 50 * Math.cos(i * 2 * Math.PI / 8)), (int) (100 + 50 * Math.sin(i * 2 * Math.PI / 8))); } Area a1 = new Area(rect); Area a2 = new Area(poly); a2.subtract(a1); //Azul g2.setColor(Color.blue); g2.fill(a2); //Gradiante g2.translate(200, 0); GradientPaint gradiante = new GradientPaint(10, 0, Color.red, 200, 0, Color.GREEN); g2.setPaint(gradiante); g2.fill(a2); //Textura g2.translate(-100, 150); TexturePaint textura = new TexturePaint(imagem, rectangulo); g2.setPaint(textura); g2.fill(a2); } }