This needs to be in java. DText The DTextis basicaly rectangular, like DRectand
ID: 3573970 • Letter: T
Question
This needs to be in java.
DText
The DTextis basicaly rectangular, like DRectand DOval, but with String text and String font name in its model. The text of a new DTextshould be "Hello" and the font should be "Dialog".
• The text shape will display inside a bounding rec tangle, just like the other shapes.
• There is a JTextFieldthat allows the user to see and edit the string used by the selected text shape. Use the setMaximumSize()feature of JComponentto prevent the text field from getting enormous as the frame is resized. Entering text in the text field should set the text of the selected shape, if it is a text shape.
• There is a JComboBoxto control the font. The combo box should list all the available font family names. See the docs for the JComboBox, Font, and GraphicsEnvironmentclasses. Changing the selection in the font control should change the font name of the selected shape, if it is a text shape.
• The font size used will be a function of the height of the shape. The font will be just large enough so that the letters are tall enough to fill the shape height. The width of the shape rectangle is only used for clipping whatever part of the string extends past the width will not be drawn (see clipping below).
It turns out that the most convenient place to compute the Fontis in paintComponent(). (You'll need to dig around in the docs for the Fontclass to see how it works.) Write a utility method called computeFont() that is called by the DText draw code, and uses its Graphics object. computeFont() should use the following strategy...
•The Font objects should be determined by the shape font name. We will not worry about the case where the font name does not match a font on our system, although a good default behavior in that case would be to substitute in the Dialog font.
•Start withdoublesize=1.0;. Get aFontobject using the (int) of that size.
•Get the FontMetricsfor that Font. Check to see if the height of the Fontfits within the shape rectangle height. (Dig around in the Font docs.)
•(looping) If the Fontdoes fit, then try an approximately 10% bigger size with adjustment
size=(size*1.10)+1;. Check to see if that Fontsize fits. Continue doing this until a Fontsize does not fit. Use the Fontsize from the previous iteration. (The +1 in the equation helps its behavior when the font size is small.)
• (optional) Computing the font size every time we draw the shape is costly, especially since many times the height is not even changing. Add code to cache the result of the most recent font computation for a shape, so that if its height has not changed, it knows what font size to use without recomputing it. This optimization can make dragging a text shape look more smooth.
The draw shape code should call computeFont()to get a Fontobject. Then set the graphics object to use that font and draw the current string at the bounds left and font "descent" pixels up from the bounds bottom. Usually we try to avoid doing heavy computation in paintComponent(), but it's really the best approach available since we need a Graphicsobject context to do Fontcomputations at all.
There is a problem that the text can easily draw outside of the shape bounds rect. The fix is to manipulate the clipping rectangle of the graphics when drawing the text string. The "clip" is a boundary property of the Graphics object that limits draw operations to only change pixels that are within the clip. We want to temporarily change the clip to be the intersection of the old clip and the text shape bounds, then draw the text, then restore the old clip. Here's the code for that case:
This will cut off any pixels that fall outside the shape bounds rect. This technique is only needed for the text shape. The other shapes intrinsically draw within their bounds.
Get the current clip Shape clip g.getClip Intersect the clip with the text shape bounds i.e. We won't lay down any pixels that fall outside our bounds g. setClip (clip.getBounds ection (getBounds draw the text. Restore the old clip g. see tclip (clip)Explanation / Answer
Please find below the java program with comments to help you understand the program :
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
public class Clip implements Painter { // public class called clip declared here
public void paint(Graphics2D g) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// The below will Save the original clip
Shape clipShape = g.getClip();
java.awt.geom.AffineTransform transform = g.getTransform();
g.setPaint(Color.black); //setpaint() method
Dimension size = new Dimension(250, 350); // Dimensions are set here
int w=100, h=50;
int vOffset = h + 20;
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D gi = image.createGraphics(); // createGraphics() method declared here
gi.setPaint(Color.white); // setpaint()
gi.fillRect(0, 0, 100, 50);
gi.setPaint(Color.green);
gi.fillRect(0, 0, 50, 25); //fillRect()
gi.setPaint(Color.black);
gi.fillRect(50, 0, 50, 25);
gi.setPaint(Color.red);
gi.fillRect(50, 25, 50, 25);
gi.dispose();
// The bwlow will Set the simple clip : does not modify the output
g.clipRect(0, 0, size.width, size.height);
g.drawImage(image, 0, 0, null);
g.setClip(clipShape);
g.drawString("Clip set to device bounds", 110, 25);
g.translate(0, vOffset);
// The below will Intersect current clip with a smaller clip : And will show only
// the top right corner of the image
g.drawString("Clip set to upper right quarter", 110, 25);
g.clipRect(w/2, 0, w/2, h/2);
g.drawImage(image, 0, 0, null);
// The below will Restore
g.setTransform(transform);
g.setClip(clipShape);
g.translate(0, 2*vOffset);
// The below will Scale before setting the same clip
g.drawString("Clip set to upper right quarter", 110, 15);
g.drawString("after .5 scale", 110, 30);
g.scale(.5, .5);
g.clipRect(w/2, 0, w/2, h/2);
g.drawImage(image, 0, 0, null);
// This will Restore
g.setTransform(transform);
g.setClip(clipShape);
g.translate(0, 3*vOffset);
// The below will Use a non-rectangle clipping area
g.drawString("Non-Rectagular clip", 110, 25);
Shape circle = new Ellipse2D.Float(0, 0, w, h);
g.clip(circle);
g.drawImage(image, 0, 0, null);
// The below will Restore
g.setTransform(transform);
g.setClip(clipShape);
g.translate(0, 4*vOffset);
// The below will Use a non-rectangle clipping area again,
// after setting a scale transform
g.drawString("Non-Rectagular clip after", 110, 15);
g.drawString(".5 scale", 110, 30);
g.scale(.5, .5);
g.clip(circle);
g.drawImage(image, 0, 0, null);
// This will Restore
g.setTransform(transform);
g.setClip(clipShape);
g.translate(0, 5*vOffset);
// This will Use a non-rectangle clipping area again,
// before setting a scale transform
g.drawString("Non-Rectagular clip before", 110, 15);
g.drawString(".5 scale", 110, 30);
g.clip(circle);
g.scale(.5, .5);
g.drawImage(image, 0, 0, null);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.