Creating a GUI with JFC/Swing
Lesson: Performing Custom Painting
Answers: Performing Custom Painting

Questions

Question 1. What method defined by JComponent paints the inside of a component?
Answer 1: As described in 14painting/conceptsHow Swing Components Are Displayed, the http://java.sun/j2se/1.4.2/docs/api/javax/swing/JComponentpaintComponent(java.awt.Graphics)paintComponent method paints a component's innards (with the exception of any children it contains).

Question 2. Which of the following code snippets paint a rectangle (filled or not) that is 100x100 pixels?

a. g.fillRect(x, y, 100, 100)
b. g.fillRect(x, y, 99, 99)
c. g.drawRect(x, y, 100, 100)
d. b and c
e. a and c

Answer 2: a. As noted in 14painting/concepts2Introduction to Painting Concepts, drawRect methods paint lines just below the specified rectangle. So to get a 100x100 rectangle, you need to specify a and of 99, 99 to drawRect. For fillXxx methods, on the other hand, you specify exactly the desired and , so g.fillRect(x, y, 100, 100) results in a 100x100 rectangle.


Question 3. What code would you use to make a component perform the next painting operation using the background color at 50% transparency?
Answer 3: You can find the answer by looking at the faded rocketship code in 14painting/practiceImplementing a Custom Component and then modifying the specified amount of fading. Here's the result:

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));

Exercises

Exercise 1. Using a standard border and custom component painting, implement a component that has a preferred size of 250x100, is opaque by , has a 5-pixel black border, and paints an &147;X&148; (using 5-pixel-thick lines) in the foreground color, as shown in the following figure. uiswing/QandE/ComponentDisplayer-1 260133ALIGNNATURALSIZEFLAG3ALTComponentDisplayer-1

Answer 1: See example-1dot4/XMarksTheSpot.javaXMarksTheSpot.java, which you can run using example-1dot4/ComponentDisplayer.javaComponentDisplayer.java.

Here's the code that sets the border of the XMarksTheSpot component:

setBorder(BorderFactory.createMatteBorder(5,5,5,5, Color.BLACK));

Here's the X-drawing code from the paintComponent method of XMarksTheSpot:

Graphics2D g2 = (Graphics2D)g; Insets insets = getInsets(); g2.setStroke(new BasicStroke(5.0f)); g2.draw(new Line2D.Double(insets.left, insets.top, get() - insets.right, get() - insets.)); g2.draw(new Line2D.Double(insets.left, get() - insets., get() - insets.right, insets.top));

Exercise 2. Implement an icon that&146;s 10x10 pixels and paints a solid rectangle that fills the 10x10 area. If the icon&146;s component is enabled, the rectangle should be red; if disabled, gray. Make a copy of ButtonDemo.java that uses your custom Icon for the middle button, instead of displaying middle.gif. The following pictures show what the icon should look like.  

Answer 2: See example-1dot4/SquareIcon.javaSquareIcon.java, which you can run using this example-1dot4/ButtonDemo.javamodified ButtonDemo.java

Here's the code that sets the icon:

Icon middleButtonIcon = new SquareIcon(); ... b2 = new JButton("Middle button, middleButtonIcon);

Here is SquareIcon's implementation of the three methods required by the http://java.sun/j2se/1.4.2/docs/api/javax/swing/IconIcon interface:

public void paintIcon(Component c, Graphics g, int x, int y) { if (c.isEnabled()) { g.setColor(Color.RED); } else { g.setColor(Color.GRAY); } g.fillRect(x, y, SIZE, SIZE); } public int getIcon() { return SIZE; } public int getIcon() { return SIZE; }

Exercise 3. Implement a border that paints a red 15-pixel-tall stripe all the way across the top of its component. Test this border by substituting it for the border on the component you created in exercise 1. The result should look like the following figure. uiswing/QandE/ComponentDisplayer-2 260133ALIGNNATURALSIZEFLAG3ALTComponentDisplayer-2 Answer 3: See example-1dot4/StripeBorder.javaStripeBorder.java, which you can run using example-1dot4/ComponentDisplayer.javaComponentDisplayer.java and example-1dot4/XMarksTheSpot.javaXMarksTheSpot.java (be sure to uncomment the line that sets the border to StripeBorder). Here is StripeBorder's implementation of the three methods required by the http://java.sun/j2se/1.4.2/docs/api/javax/swing/border/BorderBorder interface: public void paintBorder(Component c, Graphics g, int x, int y, int , int ) { g.setColor(Color.RED); g.fillRect(x, y, c.get(), ); } public Insets getBorderInsets(Component c) { return new Insets(, 0, 0, 0); } public boolean isBorderOpaque() { return true; }

Bhopal news
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

freelance web designer india ecommerce web developer | Ecommerce web design, software developer india | Web hosting India Windows hosting | India web hosting Windows hosting India | India software developer | web designer india