Cześć, mam problem z paintem w Javie. Mianowicie jestem w stanie narysować wszystkie figury, z wyjątkiem prostokąta. Czy ktoś widzi gdzie jest problem? Z góry dzięki za pomoc.
Tu klasa prostokąt:
class Prostokat implements Figura {
int x1, y1, x2, y2;
int Xmax, Xmin, Ymax, Ymin;
int width, height;
Color kolor = new Color(0);
public void setX1(int x1) { this.x1 = x1; }
public void setY1(int y1) { this.y1 = y1; }
public void setX2(int x2) { this.x2 = x2; }
public void setY2(int y2) { this.y2 = y2; }
public int getX1() { return this.x1; }
public int getY1() { return this.y1; }
public int getX2() { return this.x2; }
public int getY2() { return this.y2; }
public void Rysuj(Graphics g){
Xmax = Math.max(x1, x2);
Xmin = Math.min(x1, x2);
Ymax = Math.max(y1, y2);
Ymin = Math.min(y1, y2);
width = Xmax - Xmin;
height = Ymax - Ymin;
g.setColor(kolor);
g.fillRect(Xmin, Ymin, width, height);
}
/**
* Ustawiamy kolor dla prostokąta
*/
public void UstawKolor(Color kolor) { this.kolor = kolor; }
public boolean CzyZawiera(int x, int y){ // typu boolean; zwraca true jeżeli warunek jest spełniony
return (x > Xmin && x < Xmax && y > Ymin && y < Ymax);
}
}
Tutaj fragment klasy z metodami :
class RysujFigure extends JPanel {
int x, y;
public int indeks;
static ArrayList<Figura> figury = new ArrayList<>();
Figura obecnyTypFigury;
Figura figura;
private JPopupMenu popupMenu;
public RysujFigure() {
setDoubleBuffered(true);
ObslugaMyszy mouse = new ObslugaMyszy();
addMouseListener(mouse);
popup();
}
/**
* Metoda odpowiedzialna za rysowanie figur.
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (Figura p:figury) p.Rysuj(g);
}
/**
* Ta klasa wewnętrzna obsługuje wybór figury po naciśnięciu myszy.
*/
class ObslugaMyszy extends MouseAdapter {
/**
* Dodajemy nową figurę do listy figur.
*/
public void mousePressed(MouseEvent event) {
switch (indeks) {
case 1:
figura = new Okrag();
break;
case 2:
figura = new Prostokat();
break;
case 3:
figura = new Trojkat();
break;
}
if (indeks == 1 || indeks == 2 || indeks == 3){
figury.add(figura);
figura.setX1(event.getX());
figura.setY1(event.getY());
}
if (indeks == 0) {
//x, y zadeklarowane na początku klasy głównej
x = event.getX();
y = event.getY();
for(int i = figury.size()-1; i >= 0; i--){
if(figury.get(i).CzyZawiera(x,y)) {
obecnyTypFigury = figury.get(i);
break;
}
}
}
}
/**
* Po wybraniu konkretnej figury możemy ją później edytować
*/
public void mouseReleased(MouseEvent e) { indeks = 0; }
}
}
I dla porównania działająca klasa okrąg:
class Okrag implements Figura {
/** Deklarujemy punkty lewego górnego i prawego dolnego rogu.
* Deklarujemy zmienne odpowiedzailne za maksymalne i minimalne wartości tych punktow.
* a- promień; kolor odpowiada za kolor figury
*/
int x1, y1, x2, y2;
int Xmax, Xmin, Ymax, Ymin;
int r;
Color kolor = new Color(0);
/**
* Ustawiamy pierwsza wspolrzedna jednego punktu.
*/
public void setX1(int x1) { this.x1 = x1; }
/**
* Ustawiamy druga wspolrzedna jednego punktu.
*/
public void setY1(int y1) { this.y1 = y1; }
/**
* Ustawiamy pierwsza wspolrzedna drugiego punktu.
*/
public void setX2(int x2) { this.x2 = x2; }
/**
* Ustawiamy druga wspolrzedna drugiego punktu
*/
public void setY2(int y2) { this.y2 = y2; }
/**
* Ustawiamy pierwsza wspolrzedna poczatkowego punktu
*/
public int getX1() { return this.x1; }
/**
* Ustawiamy druga wspolrzedna poczatkowego punktu
*/
public int getY1() { return this.y1; }
/**
* Ustawiamy pierwsza wspolrzedna koncowego punktu
*/
public int getX2() { return this.x2; }
/**
* Ustawiamy druga wspolrzedna koncowego punktu
*/
public int getY2() { return this.y2; }
/** Metoda wyznaczajaca maksymalne i minimalne wartosci punktow oraz rysujaca kolo
*
*/
public void Rysuj(Graphics g){
Xmax = Math.max(x1, x2);
Xmin = Math.min(x1, x2);
Ymax = Math.max(y1, y2);
Ymin = Math.min(y1, y2);
r = Math.max(Xmax - Xmin, Ymax - Ymin);
g.setColor(kolor);
g.fillOval(Xmin, Ymin, r, r);
}
/**
* Ustawiamy kolor dla okręgu
*/
public void UstawKolor(Color kolor) { this.kolor = kolor; }
public boolean CzyZawiera(int x, int y){
return(x > Xmin && x < (r + Xmin) && y > Ymin && y < (r + Ymin));
}
}