Modify ClipPoly.java (Original program here: http://pastebin.com/pjFxmiEm ), so
ID: 3553044 • Letter: M
Question
Modify ClipPoly.java (Original program here: http://pastebin.com/pjFxmiEm), so that as a result of clipping, it displays the
polygon line segments outside of the clip rectangle as dotted lines, those inside
the clip rectangle as solid lines, and then the clipped polygon (resulting polygon
is filled with blue color).
Extend the above modified polygon clipping program for
clipping regions other than rectangles. The clipping region should be a polygon
within a drawing rectangle of 500 x 300, and should be determined by the user's
mouse clicking in the same way as in this code: http://pastebin.com/sfij1MHq
Explanation / Answer
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JPanel {
public static void main(String[] args) {
JFrame f = new JFrame();
Test t = new Test();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(t,BorderLayout.CENTER);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public Test() {
setPreferredSize(new Dimension(300, 300));
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g.create();
Rectangle2D rectangleNotToDrawIn = new Rectangle2D.Double(100, 100, 20, 30);
Area outside = calculateRectOutside(rectangleNotToDrawIn);
g2.setPaint(Color.white);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.setPaint(Color.black);
g2.setClip(outside);
g2.drawLine(0, 0, getWidth(), getHeight());
}
private Area calculateRectOutside(Rectangle2D r) {
Area outside = new Area(new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
outside.subtract(new Area(r));
return outside;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.