DEALING WITH GRAPHICS AND TEXT
DEALING WITH GRAPHICS AND TEXT
first of all know that every component owns its own graphics context, which context is defined in the Graphics class of java.awt package and has the following properties:
1.the component object on which to draw
2.a translation origin for rendering and clipping coordinates
3.the current clip
4.the current color
5.the current font
6.the current logical pixel operation function (XOR or paint)
7.the current XOR alternation color
although in AWT to paint we typically override component's paint() method, to do rendering we use the update() method for implementing our own double buffering and background filling before calling paint(), but in swing, if any component wants to take charge of its rendering, it should override the paintComponent() method and not the paint() method and should always begin the paintComponent() method with a call to super.paintComponent() thereby it acts as its own lightweight canvas.
Note before: the above is useful for simple custom components but for normal swing component, since the UI delegate is in charge of rendering, never attempt it.
Inside the paintComponent() method, we have access to the component's graphics object or its graphics context used for painting shapes and drawing lines and text. Check the graphics class for useful methods.
Some code that illustrates the above.
package tests;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Property extends JPanel{
//declaring an instance
static Color dColor = new Color(0, 255, 100, 125);
static Color mainRed = new Color(255, 0, 0, 150);
static Color mainGreen = new Color(0, 255, 0, 150);
static Color mainBlue = new Color (0, 0, 255, 150);
static Font mainBIFont = new Font("Monospace", Font.BOLD| Font.ITALIC, 36);
static Font mainPFont = new Font("SansSerif", Font.PLAIN, 12);
static Font mainBFont = new Font("Serif", Font.BOLD, 24);
//the dimension
static Dimension dim = new Dimension(400, 400);
static JButton jButton1 = new JButton(" ");
static JTextArea jText;
/** Creates a new instance of Property */
public Property() {
jButton1 = new JButton("click me");
add(jButton1);
//we'll use the instance as foreground.
jButton1.setBackground(dColor);
}
private static void dButton(){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Property");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Property nProp = new Property();
//buffering and opaque value for painting and updating
nProp.setOpaque(true);
nProp.setDoubleBuffered(true);
//set max, min and preferred sizes
nProp.setMaximumSize(dim);
nProp.setMinimumSize(dim);
nProp.setPreferredSize(dim);
frame.setContentPane(nProp);
//let the above size stay 4ever
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
public static void simplyText(String txt){
jText.append(txt+"\n");
}
public void paintComponent(Graphics g){
super.paintComponent(g);
//fill entire component white
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
//fills yellow circle
g.setColor(Color.yellow);
g.fillOval(0, 0, 240, 240);
//filled magenta circle
g.setColor(Color.magenta);
g.fillOval(160, 160, 240, 240);
g.setColor(Color.black);
//text is bold, italic, 36-point, "Nigeria"
g.setFont(mainBIFont);
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth("NIGERIA");
int h = fm.getAscent();
g.drawString("NIGERIA", 120-(w/2), 120+(h/4));
//plain, 12-point, "go"
g.setFont(mainPFont);
fm = g.getFontMetrics();
w = fm.stringWidth("go");
h = fm.getAscent();
g.drawString("go", 200-(w/2), 200+(h/4));
//bold, 24-point, "SURVIVE"
g.setFont(mainBFont);
fm = g.getFontMetrics();
w = fm.stringWidth("GO");
h = fm.getAscent();
g.drawString("SURVIVE", 280-(w/2), 280+(h/4));
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
dButton();
}
});
}
}
If you run the above code, it'll display: “NIGERIA go SURVIVE” in bright colors that illustrates the power of the rendering and painting properties of every graphics context for swing component. Worthy of note though is that not the totality of the area of a component is painted but for the sake of efficiency there are some so-called “dirtied” areas or damaged areas which are defined as the bounds of the clipping area which are not painted. You can get a Rectangle object showing this by calling getClipBounds() method of the Graphics class.
No comments:
Post a Comment