Desenarea In afara posibilit\]ii de a utiliza componente grafice standard, Java ofer\ [i posibilitatea de controlului la nivel de punct (pixel) pe dispozitivul grafic, respectiv desenarea a diferite forme grafice direct pe suprafa]a unei componente. De[i este posibil, in general nu se deseneaz\ la nivel de pixel direct pe suprafa]a ferestrelor. In Java a fost definit un tip special de component\ numit\ Canvas (panz\ de pictor), al c\rui scop este de a fi extins pentru a implementa componente cu o anumit\ inf\]i[are. A[adar clasa Canvas este o clas\ generic\ din care se deriveaz\ subclase pentru crearea suprafe]elor de desenare. Constructorul Canvas() creeaz\ o plan[\, adic\ o component\ pe care se poate desena. Plan[ele nu pot con]ine alte componente grafice, ele fiind utilizate doar ca suprafe]e de desenat sau ca fundal pentru anima]ie. Constructor Canvas () 24236nee15tho3d Metode addNotify () Creates the peer of the canvas. paint(Graphics) Paints the canvas in the default background color. Metoda paint() a clasei Canvas() picteaz\ plan[a in culoarea implicit\ a fundalului. Pentru a redesena plan[a cu un alt con]inut, se recomand\ supradefinirea acestei metode implicite. eh236n4215thho class Tablou extends Canvas { public void paint(Graphics g) { //...desenare continut } } Toate desenele care trebuie s\ apar\ pe o suprafa]\ de desenare se realizeaz\ in metoda public void paint(Graphics g), in general apelat\ intern in urma unui apel repaint(), ori de cate ori componenta respectiv\ trebuie reafi[at\. In general desenarea se poate face : pe o por]iune de ecran, la imprimant\ sau intr-o zon\ virtual\ de memorie Inainte ca utilizatorul s\ poat\ desena el trbuie s\ ob]in\ un context de desenare pentru fereastra c\reia ii apar]ine regiunea pe care se va desena. Acest context grafic este specificat prin intermediul obiectelor de tip Graphics primite ca parametru in func]ia paint(). In func]ie de dispozitivul fizic pe care se face afi[area (ecran, imprimant\, plotter, etc) metodele de desenar au implement\ri interne diferite, transparente utilizatorului. A[adar, clasa Graphics ofer\ posibilitatea de a desena linii, forme geometrice, imagini [i caractere. Constructor Graphics () Constructs a new Graphics Object. Metode clearRect (int, int, int, int) Clears the specified rectangle by filling it with the current background color of the current drawing surface. clipRect(int, int, int, int) Clips to a rectangle. copyArea(int, int, int, int, int, int) Copies an area of the screen. create() Creates a new Graphics Object that is a copy of the original Graphics Object. create(int, int, int, int) Creates a new Graphics Object with the specified parameters, based on the original Graphics Object. dispose() Disposes of this graphics context. draw3DRect(int, int, int, int, boolean) Draws a highlighted 3-D rectangle. drawArc(int, int, int, int, int, int) Draws an arc bounded by the specified rectangle from startAngle to endAngle. drawBytes(byte[], int, int, int, int) Draws the specified bytes using the current font and color. drawChars(char[], int, int, int, int) Draws the specified characters using the current font and color. drawImage(Image, int, int, ImageObserver) Draws the specified image at the specified coordinate (x, y). drawImage(Image, int, int, int, int, ImageObserver) Draws the specified image inside the specified rectangle. drawImage(Image, int, int, Color, ImageObserver) Draws the specified image at the specified coordinate (x, y), with the given solid background Color. drawImage (Image, int, int, int, int, Color, ImageObserver) Draws the specified image inside the specified rectangle, with the given solid background Color. drawLine(int, int, int, int) Draws a line between the coordinates (x1,y1) and (x2,y2). drawOval(int, int, int, int) Draws an oval inside the specified rectangle using the current color. drawPolygon(int[], int[], int) Draws a polygon defined by an array of x points and y points. drawPolygon(Polygon) Draws a polygon defined by the specified point. drawRect(int, int, int, int) Draws the outline of the specified rectangle using the current color. drawRoundRect(int, int, int, int, int, int) Draws an outlined rounded corner rectangle using the current color. drawString(String, int, int) Draws the specified String using the current font and color. fill3DRect(int, int, int, int, boolean) Paints a highlighted 3-D rectangle using the current color. fillArc(int, int, int, int, int, int) Fills an arc using the current color. fillOval(int, int, int, int) Fills an oval inside the specified rectangle using the current color. fillPolygon(int[], int[], int) Fills a polygon with the current color using an even-odd fill rule (otherwise known as an alternating rule). fillPolygon(Polygon) Fills the specified polygon with the current color using an even-odd fill rule (otherwise known as an alternating rule). fillRect(int, int, int, int) Fills the specified rectangle with the current color. fillRoundRect(int, int, int, int, int, int) Draws a rounded rectangle filled in with the current color. finalize() Disposes of this graphics context once it is no longer referenced. getClipRect() Returns the bounding rectangle of the current clipping area. getColor() Gets the current color. getFont() Gets the current font. getFontMetrics() Gets the current font metrics. getFontMetrics(Font) Gets the current font metrics for the specified font. setColor(Color) Sets the current color to the specified color. setFont(Font) Sets the font for all subsequent text-drawing operations. setPaintMode() Sets the paint mode to overwrite the destination with the current color. setXORMode(Color) Sets the paint mode to alternate between the current color and the new specified color. toString() Returns a String object representing this Graphic's value. translate(int, int) Translates the specified parameters into the origin of the graphics context. Prin dreptunghi de decupare (clip area) se in]elege zona din suprafa]a componentei de afi[are in care sunt vizibile opera]iile efectuate. Orice opera]ie efectuat\ in afara acestui dreptunghi nu are nici un efect. Stabilirea unui dreptunghi de decupare se realizeaz\ prin : clipRect(int x, int y, int width, int height) Propriet\]ile contextului grafic culoarea de desenare Color getColor() void setColor(Color) originea coordonatelor - poate fi modificat\ prin : translate(int x, int y) modul de desenare void setXorMode() - scriere “sau exclusiv” (culoare + fundal = culoare, culoare + culoare = fundal, (culoare + culoare1) + culoare = culoare1 ) void setPaintMode() - suprascriere fontul curent pentru desenarea caracterelor Font getFont() void setFont(Font) zona de decupare (in care sunt vizibile modific\rile) Shape getClip() void setClip(Shape) void setClip(int x, int y, int w, int h) Desenarea fonturilor La afi[area unui [ir cu metoda drawString trebuie s\ specific\m pozi]ia la care s\ apar\ [irul pe ecran. In momentul in care avem de afi[at mai multe [iruri trebuie s\ calcul\m pozi]iile lor de afi[are in func]ie de lungimea [i in\l]imea in pixeli a textului fiec\rui [ir. Pentru aceasta este folosit\ clasa FontMetrics. Un obiect din aceast\ clas\ se construie[te pornind de la un obiect de tip Font [i pune la dispozi]ie informa]ii despre dimensiunile in pixeli pe care le au caracterele fontului respectiv. Clasa FontMetrics Variabile font The actual font. Constructor FontMetrics (Font) Creates a new FontMetrics object with the specified font. Metode bytesWidth (byte[], int, int) Returns the width of the specified array of bytes in this Font. charWidth(int) Returns the width of the specified character in this Font. charWidth(char) Returns the width of the specified character in this Font. charsWidth(char[], int, int) Returns the width of the specified character array in this Font. getAscent() Gets the font ascent. getDescent() Gets the font descent. getFont() Gets the font. getHeight() Gets the total height of the font. getLeading() Gets the standard leading, or line spacing, for the font. getMaxAdvance() Gets the maximum advance width of any character in this Font. getMaxAscent() Gets the maximum ascent of all characters in this Font. getMaxDecent() For backward compatibility only. getMaxDescent() Gets the maximum descent of all characters. getWidths() Gets the widths of the first 256 characters in the Font. stringWidth(String) Returns the width of the specified String in this Font. toString() Returns the String representation of this FontMetric's values. Exemplu Font f = new Font(“TimesRoman”, Font.ITALIC, 24); g.setFont(f); FontMetrics fm = g.getFontMetrics(f); g.drawString(“Litera W are latimea “ + fm.charWidth(‘W’) + “unitati”, 10, 10); g.drawString(“Inaltimea caracterelor este “ + fm.getHeight() + “unitati”, 10, 10 + fm.getHeight());