Karol Leszczyński gr.13 WIMIR
Sprawozdanie Lab3 Java
Opis:
Pierwsze przyciski pozwalają wybrać narzędzie
•
Brush – rysuje dowolny kształt
•
Eraser – rysuje dowolny kształt. Jedyny możliwy kolor to biały.
•
Line – rysuje prostą linię.
•
Rectangle – rysuje prostokąt
•
Rounded Rectangle – rysuje zaokrąglony prostokąt
•
Ellipse – rysuje elipsę
•
Polygon – rysuje wielobok
•
Bucket – wypełnia powierzchnię wybranym kolorem.
•
Text – rysuje wpisany tekst
Następne przyciski pozwalają wybrać rozmiar rysowanych kształtów w Brush i
Eraser, rozmiar czcionki w Text, a w pozostałych przypadkach grubość linii. Wyjątek
stanowi Bucket dla którego rozmiar nie ma znaczenia.
Kolejne przyciski odpowiedzalne są za to czy rysowana figura(Rectangle, Rounded
Rectangle, Ellipse i Polygon) będzie tylko obramowana(Outline) czy w całości
wypełniona(Solid).
Ostatnie przyciski pozwalają wybrać kolor rysowanych kształtów(nie dotyczy
Eraser).
Screenshot:
Kod:
import
java.awt.BasicStroke;
import
java.awt.Button;
import
java.awt.Color;
import
java.awt.Cursor;
import
java.awt.FlowLayout;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.Graphics2D;
import
java.awt.Label;
import
java.awt.Point;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.awt.event.KeyEvent;
import
java.awt.event.KeyListener;
import
java.awt.event.MouseEvent;
import
java.awt.event.MouseListener;
import
java.awt.event.MouseMotionListener;
import
java.awt.image.BufferedImage;
import
javax.swing.JApplet;
public
class
applet
extends
JApplet
implements
MouseListener, MouseMotionListener,
ActionListener, KeyListener{
private
boolean
mousePressed
,
fill
,
drawText
;
//deklaracje zmiennych
private
int
width
,
height
,
button
,
brushSize
,
mx
,
my
,
mx2
,
my2
,
mx3
,
my3
,
fontSize
,
points
;
private
int
[]
xpoly
,
ypoly
;
private
String
text
;
private
Button
buttonClear
,
buttonBrush
,
buttonEraser
,
buttonLine
,
buttonRect
,
buttonRRect
,
buttonOval
,
buttonPoly
,
buttonFill
,
buttonText
,
buttonOutline
,
buttonSolid
;
private
Button[]
buttonSize
,
buttonColor
;
private
Label
label1
,
label2
,
label3
,
labelC
,
labelFill
;
private
BufferedImage
backbuffer
;
private
Graphics2D
backg
;
private
Color
c
;
@Override
public
void
init() {
//
TODO
Auto-generated method stub
super
.init();
//wywołanie konstruktora superklasy
this
.setSize(1280, 720);
//początkowy rozmiar okna
this
.setCursor(
new
Cursor(Cursor.
CROSSHAIR_CURSOR
));
//ustawienie
kursora
width
= 1920;
//przypisanie domyślnych wartości zmiennym i stworzenie
obiektów
height
= 1080;
button
= 1;
brushSize
= 1;
fontSize
= 12;
points
= 0;
xpoly
=
new
int
[1000];
ypoly
=
new
int
[1000];
fill
=
false
;
mousePressed
=
false
;
drawText
=
false
;
text
=
""
;
c
= Color.
black
;
buttonClear
=
new
Button(
"Clear"
);
buttonBrush
=
new
Button(
"Brush"
);
buttonEraser
=
new
Button(
"Eraser"
);
buttonLine
=
new
Button(
"Line"
);
buttonRect
=
new
Button(
"Rectangle"
);
buttonRRect
=
new
Button(
"Rounded Rectangle"
);
buttonOval
=
new
Button(
"Ellipse"
);
buttonPoly
=
new
Button(
"Polygon"
);
buttonFill
=
new
Button(
"Bucket"
);
buttonText
=
new
Button(
"Text"
);
buttonOutline
=
new
Button(
"Outline"
);
buttonSolid
=
new
Button(
"Solid"
);
buttonSize
=
new
Button[5];
buttonColor
=
new
Button[13];
label1
=
new
Label(
"Selected tool: Brush"
);
label2
=
new
Label(
"Selected size: 1"
);
label3
=
new
Label(
"Selected color:"
);
labelC
=
new
Label(
""
);
labelFill
=
new
Label(
"Selected shape fill: Outline"
);
this
.addMouseListener(
this
);
//dodanie przechwytywania zdarzeń
this
.addMouseMotionListener(
this
);
this
.addKeyListener(
this
);
this
.setFocusable(
true
);
//zezwolenie appletowi na zdobycie focusu
this
.setBackground( Color.
white
);
//ustawienie koloru tła
this
.setLayout(
new
FlowLayout(FlowLayout.
LEFT
));
//ustawienie rozkładu
elementów
this
.add(
buttonClear
);
//dodanie obiektów do appletu
this
.add(
buttonBrush
);
this
.add(
buttonEraser
);
this
.add(
buttonLine
);
this
.add(
buttonRect
);
this
.add(
buttonRRect
);
this
.add(
buttonOval
);
this
.add(
buttonPoly
);
this
.add(
buttonFill
);
this
.add(
buttonText
);
this
.add(
label1
);
for
(
int
i=0;i<5;i++)
{
buttonSize
[i] =
new
Button(Integer.toString(i+1));
this
.add(
buttonSize
[i]);
buttonSize
[i].addActionListener(
this
);
buttonSize
[i].addKeyListener(
this
);
}
this
.add(
label2
);
this
.add(
buttonOutline
);
this
.add(
buttonSolid
);
this
.add(
labelFill
);
for
(
int
i=0;i<13;i++)
{
buttonColor
[i] =
new
Button();
this
.add(
buttonColor
[i]);
buttonColor
[i].addActionListener(
this
);
buttonColor
[i].addKeyListener(
this
);
}
this
.add(
label3
);
this
.add(
labelC
);
label1
.setBackground(Color.
white
);
//ustawienie koloru tła obiektów
label2
.setBackground(Color.
white
);
label3
.setBackground(Color.
white
);
labelC
.setBackground(Color.
black
);
labelFill
.setBackground(Color.
white
);
buttonColor
[0].setBackground(Color.
black
);
buttonColor
[1].setBackground(Color.
darkGray
);
buttonColor
[2].setBackground(Color.
gray
);
buttonColor
[3].setBackground(Color.
lightGray
);
buttonColor
[4].setBackground(Color.
white
);
buttonColor
[5].setBackground(Color.
pink
);
buttonColor
[6].setBackground(Color.
red
);
buttonColor
[7].setBackground(Color.
orange
);
buttonColor
[8].setBackground(Color.
yellow
);
buttonColor
[9].setBackground(Color.
green
);
buttonColor
[10].setBackground(Color.
cyan
);
buttonColor
[11].setBackground(Color.
blue
);
buttonColor
[12].setBackground(Color.
magenta
);
buttonClear
.addActionListener(
this
);
//dodanie przechwytywania zdarzeń do
obiektów
buttonBrush
.addActionListener(
this
);
buttonEraser
.addActionListener(
this
);
buttonLine
.addActionListener(
this
);
buttonRect
.addActionListener(
this
);
buttonRRect
.addActionListener(
this
);
buttonOval
.addActionListener(
this
);
buttonPoly
.addActionListener(
this
);
buttonFill
.addActionListener(
this
);
buttonText
.addActionListener(
this
);
buttonOutline
.addActionListener(
this
);
buttonSolid
.addActionListener(
this
);
buttonClear
.addKeyListener(
this
);
buttonBrush
.addKeyListener(
this
);
buttonEraser
.addKeyListener(
this
);
buttonLine
.addKeyListener(
this
);
buttonRect
.addKeyListener(
this
);
buttonRRect
.addKeyListener(
this
);
buttonOval
.addKeyListener(
this
);
buttonPoly
.addKeyListener(
this
);
buttonText
.addKeyListener(
this
);
buttonOutline
.addKeyListener(
this
);
buttonSolid
.addKeyListener(
this
);
backbuffer
=
new
BufferedImage(
width
,
height
, BufferedImage.
TYPE_INT_ARGB
);
//stworzenie obiektu buforującego obraz
backg
=
backbuffer
.createGraphics();
//przypisanie bufora do obiektu
rysującego
backg
.setBackground(Color.
white
);
//ustawienie koloru tła
backg
.clearRect(0, 0,
width
,
height
);
//wyczyszczenie obrazu
backg
.setStroke(
new
BasicStroke(1,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
//ustawienie rozmiaru i
kształtu rysowanych linii
backg
.setColor(Color.
black
);
//ustawienie koloru rysowania
}
@Override
public
void
mouseClicked(MouseEvent arg0) {
//zdarzenie kliknięcia myszy
//
TODO
Auto-generated method stub
if
(
button
== 8 &
drawText
==
false
)
//rozpoczęcie wpisywania tekstu
{
drawText
=
true
;
mx3
= arg0.getX();
my3
= arg0.getY();
}
}
@Override
public
void
mouseEntered(MouseEvent arg0) {
//zdarzenie wejścia myszy w okno
//
TODO
Auto-generated method stub
}
@Override
public
void
mouseExited(MouseEvent arg0) {
//zdarzenie wyjścia myszy z okna
//
TODO
Auto-generated method stub
}
@Override
public
void
mousePressed(MouseEvent arg0) {
//zdarzenie wciśnięcia myszy
//
TODO
Auto-generated method stub
mousePressed
=
true
;
mx
= arg0.getX();
my
= arg0.getY();
if
(
button
== 1){
backg
.fillOval(
mx
-
brushSize
/2,
my
-
brushSize
/2,
brushSize
,
brushSize
);
//narysowanie okręgu narzędziem Brush
repaint();
}
else
if
(
button
== 2)
{
backg
.fillRect(
mx
-
brushSize
/2,
my
-
brushSize
/2,
brushSize
,
brushSize
);
//narysowanie kwadratu narzędziem Eraser
repaint();
}
else
if
(
button
== 7 &
backbuffer
.getRGB(
mx
,
my
) !=
c
.getRGB())
//wywołanie metody flood fill
{
floodFill(
backbuffer
,
mx
,
my
,
c
.getRGB());
repaint();
}
else
if
(
button
== 9 &
points
== 0)
//rozpoczęcie rysowania poligonu
{
points
++;
xpoly
[0] =
mx
;
ypoly
[0] =
my
;
repaint();
}
}
@Override
public
void
mouseReleased(MouseEvent arg0) {
//zdarzenie puszczenia myszy
//
TODO
Auto-generated method stub
mousePressed
=
false
;
if
(
button
== 2)
{
backg
.fillRect(
mx
-
brushSize
/2,
my
-
brushSize
/2,
brushSize
,
brushSize
);
//narysowanie kwadratu narzędziem Eraser
}
else
if
(
button
== 3 & (
mx2
!=
mx
|
my2
!=
my
))
{
backg
.drawLine(
mx
,
my
,
mx2
,
my2
);
//narysowanie linii
narzędziem Line
}
else
if
(
button
== 4)
//narysowanie prostokątu narzędziem Rectangle
{
if
(
fill
==
false
)
{
if
(
mx
<
mx2
&
my
<
my2
)
{
backg
.drawRect(
mx
,
my
,
mx2
-
mx
,
my2
-
my
);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
backg
.drawRect(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
backg
.drawRect(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
backg
.drawRect(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
);
}
}
else
{
if
(
mx
<
mx2
&
my
<
my2
)
{
backg
.fillRect(
mx
,
my
,
mx2
-
mx
,
my2
-
my
);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
backg
.fillRect(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
backg
.fillRect(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
backg
.fillRect(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
);
}
}
}
else
if
(
button
== 5)
//narysowanie zaokrąglonego prostokątu narzędziem
Rounded Rectangle
{
if
(
fill
==
false
)
{
if
(
mx
<
mx2
&
my
<
my2
)
{
backg
.drawRoundRect(
mx
,
my
,
mx2
-
mx
,
my2
-
my
, 20, 20);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
backg
.drawRoundRect(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
, 20, 20);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
backg
.drawRoundRect(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
, 20, 20);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
backg
.drawRoundRect(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
, 20, 20);
}
}
else
{
if
(
mx
<
mx2
&
my
<
my2
)
{
backg
.fillRoundRect(
mx
,
my
,
mx2
-
mx
,
my2
-
my
, 20, 20);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
backg
.fillRoundRect(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
, 20, 20);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
backg
.fillRoundRect(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
, 20, 20);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
backg
.fillRoundRect(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
, 20, 20);
}
}
}
else
if
(
button
== 6)
//narysowanie elipsy narzędziem Ellipse
{
if
(
fill
==
false
)
{
if
(
mx
<
mx2
&
my
<
my2
)
{
backg
.drawOval(
mx
,
my
,
mx2
-
mx
,
my2
-
my
);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
backg
.drawOval(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
backg
.drawOval(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
backg
.drawOval(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
);
}
}
else
{
if
(
mx
<
mx2
&
my
<
my2
)
{
backg
.fillOval(
mx
,
my
,
mx2
-
mx
,
my2
-
my
);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
backg
.fillOval(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
backg
.fillOval(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
backg
.fillOval(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
);
}
}
}
else
if
(
drawText
==
true
)
//narysowanie tekstu
{
drawText
=
false
;
backg
.drawString(
text
,
mx3
-5,
my3
+5);
text
=
""
;
}
else
if
(
button
== 9 &
points
>0)
//kontynuacja rysowania poligonu
{
if
(
points
== 1)
{
if
(arg0.getX() !=
xpoly
[0] | arg0.getY() !=
ypoly
[0])
{
points
++;
xpoly
[1] = arg0.getX();
ypoly
[1] = arg0.getY();
}
else
{
points
= 0;
xpoly
=
new
int
[1000];
ypoly
=
new
int
[1000];
}
}
else
{
xpoly
[
points
] = arg0.getX();
ypoly
[
points
] = arg0.getY();
points
++;
if
(
points
== 1000)
{
if
(
fill
==
false
)
backg
.drawPolygon(
xpoly
,
ypoly
,
points
);
else
backg
.fillPolygon(
xpoly
,
ypoly
,
points
);
points
= 0;
xpoly
=
new
int
[1000];
ypoly
=
new
int
[1000];
repaint();
}
}
}
repaint();
}
@Override
public
void
mouseDragged(MouseEvent arg0) {
//zdarzenie ruchu wciśniętej myszy
//
TODO
Auto-generated method stub
mx2
= arg0.getX();
my2
= arg0.getY();
if
(
button
== 1)
{
backg
.drawLine(
mx
,
my
,
mx2
,
my2
);
//narysowanie linii
zapewniającej ciągłość kształtu narysowanego narzędziem Brush
mx
= arg0.getX();
my
= arg0.getY();
}
else
if
(
button
== 2)
{
backg
.drawLine(
mx
,
my
,
mx2
,
my2
);
//narysowanie linii
zapewniającej ciągłość kształtu narysowanego narzędziem Eraser
mx
= arg0.getX();
my
= arg0.getY();
}
repaint();
}
@Override
public
void
mouseMoved(MouseEvent arg0) {
//zdarzenie ruchu myszy
//
TODO
Auto-generated method stub
mx2
= arg0.getX();
my2
= arg0.getY();
repaint();
}
@Override
public
void
actionPerformed(ActionEvent arg0) {
//zdarzenie działania na
obiektach(np. przyciskach)
//
TODO
Auto-generated method stub
if
(arg0.getSource() ==
buttonClear
){
//wyczyszczenie ekranu klawiszem
Clear
backg
.clearRect(0, 0,
width
,
height
);
repaint();
}
else
if
(arg0.getSource() ==
buttonBrush
){
//wybranie narzędzia
odpowiednim przyciskiem
button
= 1;
backg
.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
label1
.setText(
"Selected tool: Brush"
);
backg
.setColor(
c
);
label1
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonEraser
){
button
= 2;
backg
.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
label1
.setText(
"Selected tool: Eraser"
);
backg
.setColor(Color.
white
);
label1
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonLine
){
button
= 3;
backg
.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
label1
.setText(
"Selected tool: Line"
);
backg
.setColor(
c
);
label1
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonRect
){
button
= 4;
backg
.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
label1
.setText(
"Selected tool: Rectangle"
);
backg
.setColor(
c
);
label1
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonRRect
){
button
= 5;
backg
.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
label1
.setText(
"Selected tool: Rounded Rectangle"
);
backg
.setColor(
c
);
label1
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonOval
){
button
= 6;
backg
.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
label1
.setText(
"Selected tool: Ellipse"
);
backg
.setColor(
c
);
label1
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonFill
){
button
= 7;
label1
.setText(
"Selected tool: Bucket"
);
label1
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonText
){
button
= 8;
label1
.setText(
"Selected tool: Text"
);
backg
.setColor(
c
);
label1
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonPoly
){
button
= 9;
backg
.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
backg
.setColor(
c
);
label1
.setText(
"Selected tool: Polygon"
);
label1
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonOutline
){
//wybranie sposobu
wypełniania figur
fill
=
false
;
labelFill
.setText(
"Selected shape fill: Outline"
);
labelFill
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonSolid
){
fill
=
true
;
labelFill
.setText(
"Selected shape fill: Solid"
);
labelFill
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonSize
[0]){
//wybranie rozmiaru
rysowanych kształtów
brushSize
= 1;
fontSize
= 12;
backg
.setStroke(
new
BasicStroke(1,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
backg
.setFont(
new
Font(
"TimesRoman"
,Font.
PLAIN
,
fontSize
));
label2
.setText(
"Selected size: 1"
);
label2
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonSize
[1]){
brushSize
= 3;
fontSize
= 16;
backg
.setStroke(
new
BasicStroke(3,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
backg
.setFont(
new
Font(
"TimesRoman"
,Font.
PLAIN
,
fontSize
));
label2
.setText(
"Selected size: 2"
);
label2
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonSize
[2]){
brushSize
= 5;
fontSize
= 20;
backg
.setStroke(
new
BasicStroke(5,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
backg
.setFont(
new
Font(
"TimesRoman"
,Font.
PLAIN
,
fontSize
));
label2
.setText(
"Selected size: 3"
);
label2
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonSize
[3]){
brushSize
= 7;
fontSize
= 24;
backg
.setStroke(
new
BasicStroke(7,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
backg
.setFont(
new
Font(
"TimesRoman"
,Font.
PLAIN
,
fontSize
));
label2
.setText(
"Selected size: 4"
);
label2
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonSize
[4]){
brushSize
= 9;
fontSize
= 29;
backg
.setStroke(
new
BasicStroke(9,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
backg
.setFont(
new
Font(
"TimesRoman"
,Font.
PLAIN
,
fontSize
));
label2
.setText(
"Selected size: 5"
);
label2
.invalidate();
validate();
}
else
if
(arg0.getSource() ==
buttonColor
[0]){
//wybranie koloru rysowania
c
= Color.
black
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[1]){
c
= Color.
darkGray
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[2]){
c
= Color.
gray
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[3]){
c
= Color.
lightGray
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[4]){
c
= Color.
white
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[5]){
c
= Color.
pink
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[6]){
c
= Color.
red
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[7]){
c
= Color.
orange
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[8]){
c
= Color.
yellow
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[9]){
c
= Color.
green
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[10]){
c
= Color.
cyan
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[11]){
c
= Color.
blue
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
else
if
(arg0.getSource() ==
buttonColor
[12]){
c
= Color.
magenta
;
backg
.setColor(
c
);
labelC
.setBackground(
c
);
}
if
(
drawText
==
true
)
//zakończenie pisania tekstu przez naciśnięcie
przycisku
{
boolean
tmp =
true
;
for
(
int
i=0;i<13;i++)
{
if
(arg0.getSource() ==
buttonColor
[i] | arg0.getSource() ==
buttonText
)
{
tmp =
false
;
break
;
}
else
if
(i < 5)
{
if
(arg0.getSource() ==
buttonSize
[i])
{
tmp =
false
;
break
;
}
}
}
if
(tmp ==
true
)
{
drawText
=
false
;
backg
.drawString(
text
,
mx3
-5,
my3
+5);
text
=
""
;
repaint();
}
}
else
if
(
points
> 0)
//zakończenie rysowania poligonu przez naciśnięcie
przycisku
{
boolean
tmp =
true
;
for
(
int
i=0;i<13;i++)
{
if
(arg0.getSource() ==
buttonColor
[i])
{
tmp =
false
;
break
;
}
else
if
(i < 5)
{
if
(arg0.getSource() ==
buttonSize
[i])
{
tmp =
false
;
break
;
}
else
if
(i<2)
{
if
(arg0.getSource() ==
buttonOutline
|
arg0.getSource() ==
buttonSolid
)
{
tmp =
false
;
break
;
}
}
}
}
if
(tmp ==
true
)
{
if
(
points
== 1)
{
points
= 0;
xpoly
=
new
int
[1000];
ypoly
=
new
int
[1000];
}
else
{
if
(
fill
==
false
)
backg
.drawPolygon(
xpoly
,
ypoly
,
points
);
else
backg
.fillPolygon(
xpoly
,
ypoly
,
points
);
points
= 0;
xpoly
=
new
int
[1000];
ypoly
=
new
int
[1000];
repaint();
}
}
}
repaint();
}
@Override
public
void
keyPressed(KeyEvent arg0) {
//zdarzenie wciśnięcia klawisza
//
TODO
Auto-generated method stub
}
@Override
public
void
keyReleased(KeyEvent arg0) {
//zdarzenie puszczenia klawisza
//
TODO
Auto-generated method stub
}
@Override
public
void
keyTyped(KeyEvent arg0) {
//zdarzenie przechwycenia znaku z
klawiatury przez system
//
TODO
Auto-generated method stub
if
(
drawText
==
true
)
//dodanie znaku wpisanego na klawiaturze do
tekstu w narzędziu text
{
char
ch = arg0.getKeyChar();
if
(ch != KeyEvent.
CHAR_UNDEFINED
) {
text
+= ch;
repaint();
}
}
else
if
(arg0.getKeyChar() == KeyEvent.
VK_ENTER
&
points
>0)
//przechwycenie
wciśnięcia klawisza Enter i zakończenie rysowania poligonu
{
if
(
points
== 1)
{
points
= 0;
xpoly
=
new
int
[1000];
ypoly
=
new
int
[1000];
}
else
{
if
(
fill
==
false
)
backg
.drawPolygon(
xpoly
,
ypoly
,
points
);
//narysowanie pustego poligonu
else
backg
.fillPolygon(
xpoly
,
ypoly
,
points
);
//narysowanie pełnego poligonu
points
= 0;
xpoly
=
new
int
[1000];
ypoly
=
new
int
[1000];
repaint();
}
}
}
public
void
update(Graphics g)
//nadpisanie metody update
{
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(
backbuffer
,
null
, 0, 0);
//rysowanie obrazu z bufora
if
(
button
== 1)
//rysowanie tymczasowych obrazów
{
g2.setColor(
c
);
g2.fillOval(
mx2
-
brushSize
/2,
my2
-
brushSize
/2,
brushSize
,
brushSize
);
}
else
if
(
button
== 2)
{
g2.setColor(Color.
white
);
g2.fillRect(
mx2
-
brushSize
/2,
my2
-
brushSize
/2,
brushSize
,
brushSize
);
g2.setColor(Color.
black
);
g2.drawRect(
mx2
-
brushSize
/2,
my2
-
brushSize
/2,
brushSize
,
brushSize
);
}
else
if
(
button
== 3 &
mousePressed
==
true
)
{
g2.setColor(
c
);
g2.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
g2.drawLine(
mx
,
my
,
mx2
,
my2
);
}
else
if
(
button
== 4 &
mousePressed
==
true
)
{
g2.setColor(
c
);
g2.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
if
(
fill
==
false
)
{
if
(
mx
<
mx2
&
my
<
my2
)
{
g2.drawRect(
mx
,
my
,
mx2
-
mx
,
my2
-
my
);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
g2.drawRect(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
g2.drawRect(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
g2.drawRect(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
);
}
}
else
{
if
(
mx
<
mx2
&
my
<
my2
)
{
g2.fillRect(
mx
,
my
,
mx2
-
mx
,
my2
-
my
);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
g2.fillRect(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
g2.fillRect(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
g2.fillRect(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
);
}
}
}
else
if
(
button
== 5 &
mousePressed
==
true
)
{
g2.setColor(
c
);
g2.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
if
(
fill
==
false
)
{
if
(
mx
<
mx2
&
my
<
my2
)
{
g2.drawRoundRect(
mx
,
my
,
mx2
-
mx
,
my2
-
my
, 20, 20);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
g2.drawRoundRect(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
, 20, 20);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
g2.drawRoundRect(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
, 20, 20);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
g2.drawRoundRect(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
, 20, 20);
}
}
else
{
if
(
mx
<
mx2
&
my
<
my2
)
{
g2.fillRoundRect(
mx
,
my
,
mx2
-
mx
,
my2
-
my
, 20, 20);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
g2.fillRoundRect(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
, 20, 20);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
g2.fillRoundRect(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
, 20, 20);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
g2.fillRoundRect(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
, 20, 20);
}
}
}
else
if
(
button
== 6 &
mousePressed
==
true
)
{
g2.setColor(
c
);
g2.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
if
(
fill
==
false
)
{
if
(
mx
<
mx2
&
my
<
my2
)
{
g2.drawOval(
mx
,
my
,
mx2
-
mx
,
my2
-
my
);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
g2.drawOval(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
g2.drawOval(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
g2.drawOval(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
);
}
}
else
{
if
(
mx
<
mx2
&
my
<
my2
)
{
g2.fillOval(
mx
,
my
,
mx2
-
mx
,
my2
-
my
);
}
else
if
(
mx
>
mx2
&
my
<
my2
)
{
g2.fillOval(
mx2
,
my
,
mx
-
mx2
,
my2
-
my
);
}
else
if
(
mx
<
mx2
&
my
>
my2
)
{
g2.fillOval(
mx
,
my2
,
mx2
-
mx
,
my
-
my2
);
}
else
if
(
mx
>
mx2
&
my
>
my2
)
{
g2.fillOval(
mx2
,
my2
,
mx
-
mx2
,
my
-
my2
);
}
}
}
else
if
(
drawText
==
true
)
{
g2.drawLine(
mx3
-5,
my3
+5,
mx3
-5,
my3
-5);
g2.drawLine(
mx3
-5,
my3
+5,
mx3
+5,
my3
+5);
g2.setColor(
c
);
g2.setFont(
new
Font(
"TimesRoman"
,Font.
PLAIN
,
fontSize
));
g2.drawString(
text
,
mx3
-5,
my3
+5);
}
else
if
(
points
> 0)
{
g2.setColor(
c
);
g2.setStroke(
new
BasicStroke(
brushSize
,BasicStroke.
CAP_ROUND
,BasicStroke.
JOIN_ROUND
));
int
xpoly2[] =
xpoly
;
int
ypoly2[] =
ypoly
;
xpoly2[
points
] =
mx2
;
ypoly2[
points
] =
my2
;
if
(
fill
==
false
)
g2.drawPolygon(xpoly2, ypoly2,
points
+1);
else
g2.fillPolygon(xpoly2, ypoly2,
points
+1);
}
getToolkit().sync();
//metoda wspomagająca buforowanie obrazu na niektóych
systemach
}
public
void
paint(Graphics g)
//metoda rysująca
{
update(g);
}
public
void
floodFill(BufferedImage image,
int
x,
int
y,
int
fillColor)
//metoda z algorytmem flood fill
{
java.util.ArrayList<Point> list =
new
java.util.ArrayList<Point>();
int
color = image.getRGB(x,y);
list.add(
new
Point(x,y));
while
(list.size()>0)
{
Point p = list.remove(0);
x = p.
x
;
y = p.
y
;
if
(image.getRGB(x,y)==color)
{
image.setRGB(x, y, fillColor);
if
(x > 0)
list.add(
new
Point(x-1,y));
if
(x <
width
-1)
list.add(
new
Point(x+1,y));
if
(y > 0)
list.add(
new
Point(x,y-1));
if
(y <
height
-1)
list.add(
new
Point(x,y+1));
}
}
}
}