In afara posibilitatii de a utiliza componente grafice standard, Java ofera 
  si posibilitatea de controlului la nivel de punct (pixel) pe dispozitivul grafic, 
  respectiv desenarea a diferite forme grafice direct pe suprafata unei componente. u4d2di
  Desi este posibil, in general nu se deseneaza la nivel de pixel direct pe suprafata 
  ferestrelor. In Java a fost definit un tip special de componenta numita Canvas 
  (panza de pictor), al carui scop este de a fi extins pentru a implementa componente 
  cu o anumita infatisare. Asadar clasa Canvas este o clasa generica din care 
  se deriveaza subclase pentru crearea suprafetelor de desenare. 
  Constructorul Canvas() creeaza o plansa, adica o componenta pe care se poate 
  desena. Plansele nu pot contine alte componente grafice, ele fiind utilizate 
  doar ca suprafete de desenat sau ca fundal pentru animatie.
  Constructor
  Canvas () 
  Metode addNotify () Creates the peer of the canvas.  paint(Graphics) Paints the canvas in the default background color.
  Metoda paint() a clasei Canvas() picteaza plansa in culoarea implicita a fundalului. 
  Pentru a redesena plansa cu un alt continut, se recomanda supradefinirea acestei 
  metode implicite. class Tablou extends Canvas S public void paint(Graphics g) S
  //...desenare continut
  T
  T
Toate desenele care trebuie sa apara pe o suprafata de desenare se realizeaza 
  in metoda public void paint(Graphics g), in general apelata intern in urma unui 
  apel repaint(), ori de cate ori componenta respectiva trebuie reafisata. In 
  general desenarea se poate face :
  • pe o portiune de ecran, 
  • la imprimanta sau 
  • intr-o zona virtuala de memorie
  Inainte ca utilizatorul sa poata desena el trbuie sa obtina un context de desenare 
  pentru fereastra careia ii apartine regiunea pe care se va desena. Acest context 
  grafic este specificat prin intermediul obiectelor de tip Graphics primite ca 
  parametru in functia paint(). In functie de dispozitivul fizic pe care se face 
  afisarea (ecran, imprimanta, plotter, etc) metodele de desenar au implementari 
  interne diferite, transparente utilizatorului.
  Asadar, clasa Graphics ofera posibilitatea de a desena linii, forme geometrice, 
  imagini si 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(bytest, int, int, int, int)
  Draws the specified bytes using the current font and color. drawChars(charst, 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(intst, intst, 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(intst, intst, 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 intelege zona din suprafata componentei 
  de afisare in care sunt vizibile operatiile efectuate. Orice operatie efectuata 
  in afara acestui dreptunghi nu are nici un efect. Stabilirea unui dreptunghi 
  de decupare se realizeaza prin : clipRect(int x, int y, int width, int height)
 
Proprietatile contextului grafic
  • culoarea de desenare
  Color getColor() void setColor(Color)
  • originea coordonatelor - poate fi modificata prin : translate(int x, int y)
  • modul de desenare void setXorMode() - scriere “sau exclusiv”
  (culoare + fundal a culoare,  culoare + culoare a fundal,
  (culoare + culoare1) + culoare a culoare1 ) void setPaintMode() - suprascriere
  • fontul curent pentru desenarea caracterelor
  Font getFont() void setFont(Font)
  • zona de decupare (in care sunt vizibile modificarile)
  Shape getClip() void setClip(Shape) void setClip(int x, int y, int w, int h)
Desenarea fonturilor
  La afisarea unui sir cu metoda drawString trebuie sa specificam pozitia la care 
  sa apara sirul pe ecran. In momentul in care avem de afisat mai multe siruri 
  trebuie sa calculam pozitiile lor de afisare in functie de lungimea si inaltimea 
  in pixeli a textului fiecarui sir. Pentru aceasta este folosita clasa FontMetrics. 
  Un obiect din aceasta clasa se construieste pornind de la un obiect de tip Font 
  si pune la dispozitie informatii 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 (bytest, 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(charst, 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 a new Font(“TimesRoman”, Font.ITALIC, 24); g.setFont(f);
  FontMetrics fm a 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());