עקרונות תכנות מונחה עצמים תרגול 6 - GUI. Outline Game of Life Painting

Preview:

Citation preview

עקרונות תכנות מונחה עצמים

GUI - 6תרגול

Outline

Game of Life

Painting

The paint method

Each graphical component has a paint method.

To perform a painting of a component we need to override its paint method.

public class MyPanel extends Jpanel {

public void paint(Graphics g){

super.paint(g);

}

}

The Graphics object

Graphics methods:

drawLine(int x1, int y1, int x2, int y2)

drawOval(int x, int y, int width, int height)

drawRect(int x, int y, int width, int height)

fillOval(int x, int y, int width, int height)

fillRect(int x, int y, int width, int height)

setColor(Color c)

Example

public class MyPanel extends Jpanel {

public void paint(Graphics g){

super.paint(g);

g.drawLine(0,0,200,200);

g.fillRect(10,20,50,50);

}

}

Invoking the paint method

The paint method is invoked automatically:

Once the component appears for the first time.

Each time the component is resized.

Each time the component moves.

Each time the component becomes visible

Invoking the method explicity is performed using the method repaint() of the component.

Example 1: Canvas

DrawingCanvas Class

Main method

The modified program

The modified program

Example 2: Graphicak Calculator

Polynom Class

Calculator Class

MainWin Class

MainWin Class

Recommended