Lecția 5 programare grafica

7
Lecția 5. Bazele programării în Java. Programarea graficii

Transcript of Lecția 5 programare grafica

Page 1: Lecția 5 programare grafica

Lecția 5. Bazele programării în Java.

Programarea graficii

Page 2: Lecția 5 programare grafica

Variabila gA graphics context is represented by a variable. The type for the variable is Graphics (just like the type for a string variable is String). The variable is often named g, but the name of the variable is of course up to the programmer. Here are a few of the subroutines that are available in a graphics context g:

g.setColor(c), is called to set the color to be used for drawing. The parameter, c is an object belonging to a class named Color. There are about a dozen constants representing standard colors that can be used as the parameter in this subroutine. The standard colors include Color.BLACK, Color.WHITE, Color.LIGHT GRAY, Color.RED, Color.GREEN, and Color.BLUE. (Later, we will see that it is also possible to create new colors.) For example, if you want to draw in red, you would say “g.setColor(Color.RED);”. The specified color is used for all subsequent drawing operations up until the next time g.setColor() is called.

Page 3: Lecția 5 programare grafica

g.drawLine(x1,y1,x2,y2) draws a line from the point with coordinates (x1,y1) to the point with coordinates (x2,y2). g.drawRect(x,y,w,h) draws the outline of a rectangle with vertical and horizontal sides.The parameters x, y, w, and h must be integers or integer-valued expressions. This subroutine draws the outline of the rectangle whose top-left corner is x pixels from the left edge of the drawing area and y pixels down from the top. The width of the rectangle is w pixels, and the height is h pixels. The color that is used is black, unless a different color has been set by calling g.setColor().

g.fillRect(x,y,w,h) is similar to g.drawRect() except that it fills in the inside of the rectangle instead of drawing an outline.

g.drawOval(x,y,w,h) draws the outline of an oval. The oval just fits inside the rectangle that would be drawn by g.drawRect(x,y,w,h). To get a circle, use the same values for w and for h.

g.fillOval(x,y,w,h) is similar to g.drawOval() except that it fills in the inside of the oval instead of drawing an outline.

Page 4: Lecția 5 programare grafica

Activarea regimului grafic:import java.awt.*;import java.awt.event.*;import javax.swing.*;

Includerea bibliotecilor grafice

Page 5: Lecția 5 programare grafica

public class graphics_001_ex extends JPanel implements ActionListener { // Draws 500 disks with random colors and locations. public void drawFrame(Graphics g, int frameNumber, int width, int height) { int centerX; // The x-coord of the center of a disk. int centerY; // The y-coord of the center of a disk. int colorChoice; // Used to select a random color. int count; // Loop control variable for counting disks. for (count = 0; count < 500; count++) { colorChoice = (int)(3*Math.random()); switch (colorChoice) { case 0: g.setColor(Color.RED); break; case 1: g.setColor(Color.GREEN); break; case 2: g.setColor(Color.BLUE); break; } centerX = (int)(width*Math.random()); centerY = (int)(height*Math.random()); g.fillOval( centerX - 50, centerY - 50, 100, 100 ); g.setColor(Color.BLACK); g.drawOval( centerX - 50, centerY - 50, 100, 100 ); } }

Page 6: Lecția 5 programare grafica

public static void main(String[] args) { JFrame window = new JFrame("Random Disks"); // Titlul graphics_001_ex drawingArea = new graphics_001_ex();

drawingArea.setBackground(Color.WHITE); window.setContentPane(drawingArea);

drawingArea.setPreferredSize(new Dimension(500,500));

window.pack(); window.setLocation(100,50); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setResizable(true); Timer frameTimer = new Timer(3000,drawingArea);

window.setVisible(true); frameTimer.start();

} // end main

Page 7: Lecția 5 programare grafica

private int frameNum; public void actionPerformed(ActionEvent evt) { frameNum++; repaint(); }

protected void paintComponent(Graphics g) { super.paintComponent(g); drawFrame(g, frameNum, getWidth(), getHeight()); }