Cześć! Mam zrobić program w Applecie, w którym będą dostępne dwa tryby:
- w trybie tworzenia będzie można rysować prostokąty, elipsy i wielokąty
- w trybie edycji można powstałe figury przemieszczać, zmieniać ich rozmiar oraz kolor
Napisałem część odpowiedzialną za rysowanie prostokątów i elips, ale nie rozumiem czemu mimo pomyślnej kompilacji program nie rysuje po działaniach myszką na JPanelu. Dopiero zaczynam naukę Javy, więc pewnie błąd lub luka są dla bardziej doświadczonych oczywiste (ja ślęczę nad tym już któryś dzień) ;) dlatego zwracam się do Was o pomoc. Czego tu brakuje, co zmienić? Pozdrawiam
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class AlmostPaint extends JApplet{
private JPanel panel;
private ButtonGroup group;
private JRadioButton make;
private JRadioButton edit;
private JComboBox<String> colourCombo;
private JComboBox<String> figureCombo;
private JButton info;
private JPanel drawPanel;
private String currentFigure;
private String currentColour;
public AboutDialog dialog;
ArrayList<MyRectangle> rectangles = new ArrayList<MyRectangle>();
ArrayList<MyEllipse> ellipses = new ArrayList<MyEllipse>();
public void init(){
setLayout(new BorderLayout());
panel = new JPanel();
group = new ButtonGroup();
make = new JRadioButton("Create", true);
edit = new JRadioButton("Edit");
group.add(make);
group.add(edit);
panel.add(make);
panel.add(edit);
colourCombo = new JComboBox<>();
colourCombo.addItem("BLACK");
colourCombo.addItem("RED");
colourCombo.addItem("GREEN");
colourCombo.addItem("BLUE");
colourCombo.addItem("YELLOW");
panel.add(colourCombo);
figureCombo = new JComboBox<>();
figureCombo.addItem("Rectangle");
figureCombo.addItem("Polygon");
figureCombo.addItem("Ellipse");
panel.add(figureCombo);
info = new JButton("Info");
info.addActionListener(event -> {
if (dialog==null)
dialog = new AboutDialog(null);
dialog.setVisible(true);
});
panel.add(info);
add(panel,BorderLayout.NORTH);
JPanel draw = new DrawPanel();
draw.setBackground(Color.white);
add(draw,BorderLayout.CENTER);
}
}
class FigureAdapter extends MouseAdapter{
String currentFigure;
String currentColour;
ArrayList<MyRectangle> rectangles = new ArrayList<MyRectangle>();
ArrayList<MyEllipse> ellipses = new ArrayList<MyEllipse>();
private int x1, x2, y1, y2;
@Override
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
if(currentFigure=="Rectangle")
RectangleCreator();
else if(currentFigure=="Ellipse")
EllipseCreator();
//else
//PolygonCreator();
}
public void RectangleCreator(){
if(x1<x2 && y1<y2){
MyRectangle r = new MyRectangle(x1, y1, x2-x1, y2-y1, currentColour);
rectangles.add(r);
}
}
public void EllipseCreator(){
if(x1<x2 && y1<y2){
MyEllipse r = new MyEllipse(x1, y1, x2-x1, y2-y1, currentColour);
ellipses.add(r);
}
}
}
class MyRectangle extends Rectangle2D.Float {
String colour;
public MyRectangle(float x, float y, float width, float height, String colour) {
setRect(x, y, width, height);
this.colour=colour;
}
public boolean isHit(float x, float y) {
return getBounds2D().contains(x, y);
}
public void addX(float x) {
this.x += x;
}
public void addY(float y) {
this.y += y;
}
public void addWidth(float w) {
this.width += w;
}
public void addHeight(float h) {
this.height += h;
}
}
class MyEllipse extends Ellipse2D.Float {
String colour;
public MyEllipse(float x, float y, float width, float height, String colour) {
setFrame(x, y, width, height);
this.colour=colour;
}
public boolean isHit(float x, float y) {
return getBounds2D().contains(x, y);
}
public void addX(float x) {
this.x += x;
}
public void addY(float y) {
this.y += y;
}
public void addWidth(float w) {
this.width += w;
}
public void addHeight(float h) {
this.height += h;
}
}
class DrawPanel extends JPanel{
ArrayList<MyRectangle> rectangles = new ArrayList<MyRectangle>();
ArrayList<MyEllipse> ellipses = new ArrayList<MyEllipse>();
public Graphics2D g2d;
void initUI() {
FigureAdapter figureadapter = new FigureAdapter();
addMouseMotionListener(figureadapter);
addMouseListener(figureadapter);
}
public DrawPanel() {
initUI();
Creator();
}
public void doDrawing(Graphics g) {
g2d = (Graphics2D) g;
MyRectangle temp1;
for (int i=0; i<rectangles.size(); i++){
temp1=rectangles.get(i);
String col = temp1.colour;
if(col=="BLACK")
g2d.setPaint(Color.BLACK);
else if(col=="GREEN")
g2d.setPaint(Color.GREEN);
else if(col=="BLUE")
g2d.setPaint(Color.BLUE);
else if(col=="RED")
g2d.setPaint(Color.RED);
else
g2d.setPaint(Color.YELLOW);
g2d.fill(temp1);
repaint();
}
for (MyEllipse temp : ellipses){
String col = temp.colour;
if(col=="BLACK")
g2d.setPaint(Color.BLACK);
else if(col=="GREEN")
g2d.setPaint(Color.GREEN);
else if(col=="BLUE")
g2d.setPaint(Color.BLUE);
else if(col=="RED")
g2d.setPaint(Color.RED);
else
g2d.setPaint(Color.YELLOW);
g2d.fill(temp);
repaint();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
public void Creator(){
String currentFigure;
String currentColour;
JRadioButton make = new JRadioButton();
make.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JComboBox<String> figureCombo = new JComboBox<String>();
JComboBox<String> colourCombo = new JComboBox<String>();
String currentFigure=figureCombo.getItemAt(figureCombo.getSelectedIndex());
String currentColour=colourCombo.getItemAt(colourCombo.getSelectedIndex());
FigureAdapter figureadapter = new FigureAdapter();
}
});
}
}