Professional Documents
Culture Documents
Simon Kroly
simon.karoly@codespring.ro
Introduction
Methods are provided for drawing different shapes, for defining different
properties. E.g.:
void setColor(Color c)
void drawRect(int x, int y, int width, int height)
void fillRect(int x, int y, int width, int height)
setFont, drawOval, drawPolygon, drawString, drawImage etc.
The Graphics2D class is derived from the Graphics base class. Several
additional methods are provided by this extension (e.g. for more efficient
color handling, transformations etc.).
A Canvas is added to a Frame and there is possibility for the user to draw little circles
onto that canvas, by clicking with the mouse.
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Graphics;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyCanvas extends Canvas {
private int x = 0;
private int y = 0;
public MyCanvas() {
setBackground(new Color(50,100,250));
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
});
2016. 07. 11.
} 15:36 AWT grafika s Applet 10
Canvas - example
//imports
public class MyCanvas extends Canvas {
private Image img;
private Graphics gr;
public MyCanvas() {
setBackground(new Color(50,100,250));
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
gr.fillOval(e.getX(),e.getY(),20,20);
repaint();
}
});
}