Piszę namiastkę prostego programu graficznego, którego zadaniem jest rysowanie elips i prostokątów. Problem w tym, że program nie działa :P Nie wiem czemu. Sprawdzałem czy reaguje na wciskanie myszy - i okazało się, że tak. Nie mniej jednak, program nie działa. Podam kod:
//Paint.java
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Paint extends JFrame implements MouseListener, ActionListener, MouseMotionListener {
private JPanel pPanel;
private JButton bElipsa, bProstokat, bR, bG, bB;
private boolean nbFigura;
private int nKolor;
private Figura [] fig;
private int x1, y1, x2, y2;
private boolean czyRysowac;
private Graph gGrafika;
public Paint() {
inicjalizuj();
setSize(1024, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void mousePressed(MouseEvent me) {
x1 = me.getX();
y1 = me.getY();
if (!czyRysowac)
gGrafika.set1(x1, y1);
rozpocznijRysowanie();
}
@Override
public void mouseReleased(MouseEvent me) {
zakonczRysowanie();
}
@Override
public void mouseClicked(MouseEvent me) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent me) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent me) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent me) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent me) {
x2 = me.getX();
y2 = me.getY();
gGrafika.set2(x2, y2);
if (czyRysowac)
gGrafika.repaint();
}
public void inicjalizuj() {
pPanel = new JPanel();
bElipsa = new JButton("Elipsa");
bElipsa.addActionListener(this);
bProstokat = new JButton("Prostokąt");
bProstokat.addActionListener(this);
bR = new JButton("Czerwony");
bR.addActionListener(this);
bG = new JButton("Zielony");
bG.addActionListener(this);
bB = new JButton("Niebieski");
bB.addActionListener(this);
fig = new Figura[0];
czyRysowac = false;
pPanel.setSize(224, 600);
pPanel.setLayout(new GridLayout(5, 1, 0, 120));
pPanel.add(bElipsa, BorderLayout.NORTH);
pPanel.add(bProstokat,BorderLayout.NORTH);
pPanel.add(bR, BorderLayout.SOUTH);
pPanel.add(bG, BorderLayout.SOUTH);
pPanel.add(bB, BorderLayout.SOUTH);
add(pPanel, BorderLayout.WEST);
gGrafika = new Graph();
gGrafika.addMouseListener(this);
gGrafika.addMouseMotionListener(this);
add(gGrafika, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if (str.equals("Elipsa"))
nbFigura = false;
if (str.equals("Prostokąt"))
nbFigura = true;
if (str.equals("Czerwony"))
nKolor = 1;
if (str.equals("Zielony"))
nKolor = 2;
if (str.equals("Niebieski"))
nKolor = 3;
}
public int minimum(int n1, int n2) {
return n1 < n2 ? n1 : n2;
}
public int modulo(int n) {
if (n < 0)
return -n;
return n;
}
public void rozpocznijRysowanie() {
x2 = x1;
y2 = y1;
czyRysowac = true;
}
public void zakonczRysowanie() {
int len = fig.length;
Figura fig2 [] = new Figura[len + 1];
for (int i = 0; i < len - 1; i++)
fig2[i] = fig[i];
fig2[len] = new Figura(minimum(x1, x2), minimum(y1, y2), modulo(x1 - x2), modulo(y1 - y2), nbFigura, nKolor);
fig = fig2;
gGrafika.setFF(fig);
czyRysowac = false;
}
public static void main(String [] args) {
new Paint();
}
}
//Figura.java
public class Figura {
private int x, y, szer, wys;
private boolean type; // false - elipsa, true - prostokąt
private int color; // 1 - red, 2 - green - 3 blue;
public Figura(int nx, int ny, int nszer, int nwys, boolean ntype, int ncolor) {
x = nx;
y = ny;
szer = nszer;
wys = nwys;
type = ntype;
color = ncolor;
}
public Figura() {
}
public void setAll(int nx, int ny, int nszer, int nwys, boolean ntype, int ncolor) {
x = nx;
y = ny;
szer = nszer;
wys = nwys;
type = ntype;
color = ncolor;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSzer() {
return szer;
}
public int getWys() {
return wys;
}
public boolean getType(){
return type;
}
public int getColor() {
return color;
}
}
//Graph.java
import java.awt.Graphics;
import javax.swing.JPanel;
public class Graph extends JPanel {
private Figura [] f;
private int x1, x2, y1, y2;
private boolean nbFigura;
private int nKolor;
public Graph() {
f = new Figura[0];
}
public void set1(int x, int y) {
x1 = x;
y1 = y;
}
public void set2(int x, int y) {
x2 = x;
y2 = y;
}
public void setFT(boolean bF) {
nbFigura = bF;
}
public void setK(int kolor) {
nKolor = kolor;
}
public void setFF(Figura [] ff) {
f = ff;
}
@Override
public void paint(Graphics g) {
for (int i = 0; i < f.length; i++)
g.drawOval(f[i].getX(), f[i].getY(), f[i].getSzer(), f[i].getWys());
g.drawOval(minimum(x1, x2), minimum(y1, y2), modulo(x1 - x2), modulo(y1 - y2));
}
public int minimum(int n1, int n2) {
return n1 < n2 ? n1 : n2;
}
public int modulo(int n) {
if (n < 0)
return -n;
return n;
}
}
Czy mógłby ktoś zerknąć czemu nie działa? Przyznaję, że nie potrafię grzebać w kodzie, jeżeli uczę się sam z internetem, a nie z książki.
Pozdrawiam