package gui;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import javax.swing.JPanel;
import tikzmodel.TikzBezier;
import tikzmodel.TikzCircle;
import tikzmodel.TikzFigure;
import tikzmodel.TikzLine;
import tikzmodel.TikzPath;
import tikzmodel.TikzRectangle;
import tikzmodel.TikzPicture;
import main.ManipulationMode;
/**
* This class provides all functionality to display and modify
* TikZ pictures.
*
* @author Florian Noack
*
*/
public class DrawPanel extends JPanel {
private ManipulationMode mode;
protected ArrayList<Figure> figures;
protected ArrayList<FigureListener> figListeners;
private AffineTransform at;
/**
* Create a new DrawPanel
*/
public DrawPanel() {
super();
setBackground(Color.WHITE);
mode = ManipulationMode.SELECT;
figures = new ArrayList<Figure>();
figListeners = new ArrayList<FigureListener>();
prepareGraphics();
}
/**
* Returns the current ManipulationMode.
*
* @return
* Current ManipulationMode
*/
public ManipulationMode getMode() {
return mode;
}
/**
* Sets the active <code>ManipulationMode</code> for this DrawPanel.
*
* @param mode
* Mode to be set.
*/
public void setMode(ManipulationMode mode) {
this.mode = mode;
}
public void addFigureListener(FigureListener fl) {
figListeners.add(fl);
}
public void removeFigureListener(FigureListener fl) {
figListeners.remove(fl);
}
public ArrayList<Figure> getFigures() {
return figures;
}
public void createFigures(TikzPicture pic) {
for(Object o : pic.getList()) {
if(o instanceof TikzPath) {
TikzPath path = (TikzPath) o;
for(TikzFigure fig : path.getFigures()) {
if(fig instanceof TikzRectangle) {
figures.add(new FigureRectangle((TikzRectangle) fig));
} else if(fig instanceof TikzCircle) {
figures.add(new FigureCircle((TikzCircle) fig));
} else if(fig instanceof TikzBezier) {
/*
* IMPLEMENT ME!
*/
} else if(fig instanceof TikzLine) {
figures.add(new FigureLine((TikzLine) fig));
}
}
}
}
repaint();
}
public AffineTransform getTransform() {
return at;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
/*
* Enable anti-aliasing
*/
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.transform(at);
for(Figure f : figures) {
f.paint(g2);
}
}
private void prepareGraphics() {
at = new AffineTransform();
/*
* Set transformations
*/
at.scale(1, -1);
at.translate(0, -200);
}
}