codice:
//x brevità nn ho messo gli import
public class Layer extends JPanel implements ComponentListener{
private static final long serialVersionUID = 1L;
private Rectangle2D.Double areaCentrale = new Rectangle2D.Double(100,100,850,1285);
private double larghFoglio = 21.0*50;
private double altFoglio = 29.7*50;
private double zoom = 1.0;
private JScrollPane sp = new JScrollPane(this);
private Rectangle2D.Double posFoglio = new Rectangle2D.Double();
private int lastX,lastY;
private PanelLayer pl;
private Point mouse = null;
private JEditorPane text;
public Layer(PanelLayer pl){
this.setLayout(null);
this.pl = pl;
this.setPreferredSize(new Dimension((int)(40+larghFoglio*zoom), (int)(100+altFoglio*zoom)));
areaCentrale = new Rectangle2D.Double(100*zoom,100*zoom,850*zoom,1285*zoom);
sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//try{
text = new JEditorPane();
text.setBounds(50,50,200,200);
this.add(text);
//}catch(IOException e){}
lastX = sp.getHorizontalScrollBar().getValue();
lastY = sp.getVerticalScrollBar().getValue();
this.addComponentListener(this);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paintComponent(Graphics g){
if(posFoglio.height + posFoglio.width == 0){
this.posFoglio = new Rectangle2D.Double((this.getWidth()-larghFoglio*zoom)/2,50,larghFoglio*zoom,altFoglio*zoom);
}
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(Color.lightGray);
g2d.fillRect(0,0,this.getWidth(),this.getHeight());
g2d.translate((this.getWidth()-larghFoglio*zoom)/2,50);
g2d.setPaint(Color.black);
g2d.draw(new Rectangle2D.Double(0,0,larghFoglio*zoom,altFoglio*zoom));
g2d.setPaint(Color.white);
g2d.fill(new Rectangle2D.Double(1,1,larghFoglio*zoom-1,altFoglio*zoom-1));
g2d.setPaint(Color.blue);
g2d.draw(areaCentrale);
}
public JScrollPane getJScrollPane(){
return sp;
}
public Rectangle2D.Double getDimFoglio(){
return this.posFoglio;
}
public double getCmWidth(){
return larghFoglio*zoom/50+((larghFoglio*zoom/50)%1 <= getIncr() ? 0 : getIncr());
}
public double getCmHeight(){
return altFoglio*zoom/50+((altFoglio*zoom/50)%1 <= getIncr() ? 0 : getIncr());
}
public Point getPointMouse(){
return mouse;
}
public void refresh(){
lastX = sp.getHorizontalScrollBar().getValue();
posFoglio.x = (this.getWidth()-larghFoglio*zoom)/2-lastX;
}
public double getIncr(){
return zoom/2;
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
mouse = e.getPoint();
mouse.x -= sp.getHorizontalScrollBar().getValue();
mouse.y -= sp.getVerticalScrollBar().getValue();
pl.repaint();
}
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {
lastX = sp.getHorizontalScrollBar().getValue();
posFoglio.x = (this.getWidth()-larghFoglio*zoom)/2-lastX;
lastY = sp.getVerticalScrollBar().getValue();
posFoglio.y = 50-lastY;
pl.repaint();
}
public void componentResized(ComponentEvent e) {
this.repaint();
pl.repaint();
}
public void componentShown(ComponentEvent e) {}
}
public class PanelLayer extends JPanel implements MouseWheelListener,MouseInputListener{
private static final long serialVersionUID = 1L;
private Layer layer;
private JScrollPane sp = null;
private boolean visualizzaMouse = false;
public PanelLayer(){
this.setLayout(null);
layer = new Layer(this);
layer.addMouseListener(this);
layer.addMouseMotionListener(this);
}
public void paintComponent(Graphics g){
if(layer.getDimFoglio().getWidth()+layer.getDimFoglio().getHeight() == 0){
layer.setSize(50,50);
}
if(sp == null){
sp = layer.getJScrollPane();
sp.addMouseWheelListener(this);
sp.setBounds(30,30,this.getWidth()-30,this.getHeight()-30);
this.add(sp);
}else{
layer.refresh();
}
sp.setBounds(30,30,this.getWidth()-30,this.getHeight()-30);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new Color(153,255,153));
g2d.fill(new Rectangle2D.Double(0,0,this.getWidth(),30));
g2d.fill(new Rectangle2D.Double(0,0,30,this.getHeight()));
g2d.setPaint(Color.black);
g2d.drawLine(0,0,this.getWidth(),0);
g2d.drawLine(0,30,this.getWidth(),30);
g2d.drawLine(0,0,0,this.getHeight());
g2d.drawLine(30,0,30,this.getHeight());
for(double i=0,j=0;i<=layer.getCmWidth();i+=layer.getIncr(),j++){
if(j%2==0){
g2d.drawLine((int)(layer.getDimFoglio().x+31+i*50), 15,(int)(layer.getDimFoglio().x+31+i*50), 30);
g2d.drawString(String.valueOf((int)(j/2)), (float)(layer.getDimFoglio().x+31+i*50), 13);
}else{
g2d.drawLine((int)(layer.getDimFoglio().x+31+i*50), 22,(int)(layer.getDimFoglio().x+31+i*50), 30);
}
}
for(double i=0,j=0;i<=layer.getCmHeight();i+=layer.getIncr(),j++){
if(j%2==0){
g2d.drawLine(15, (int)(layer.getDimFoglio().y+31+i*50),30,(int)(layer.getDimFoglio().y+31+i*50));
g2d.drawString(String.valueOf((int)(j/2)),5,(float)(layer.getDimFoglio().y+31+i*50));
}else{
g2d.drawLine(22, (int)(layer.getDimFoglio().y+31+i*50),30,(int)(layer.getDimFoglio().y+31+i*50));
}
}
if((layer.getPointMouse() != null)&&(visualizzaMouse)){
g2d.drawLine(layer.getPointMouse().x+30,0,layer.getPointMouse().x+30,30);
g2d.drawLine(0,layer.getPointMouse().y+30,30,layer.getPointMouse().y+30);
}
g2d.setPaint(new Color(153,255,153));
g2d.fillRect(1,1,29,29);
int largh = layer.getJScrollPane().getVerticalScrollBar().getWidth();
g2d.fillRect(this.getWidth()-largh, 1, largh, 29);
g2d.fillRect(1, this.getHeight()-largh, 29, largh);
}
public void mouseWheelMoved(MouseWheelEvent e) {
JScrollBar vsb = layer.getJScrollPane().getVerticalScrollBar();
vsb.setValue(vsb.getValue()+20*e.getWheelRotation());
}
public void refresh(){
if((layer != null)&&(sp != null)){
layer.setSize(this.getWidth()-30,this.getHeight()-30);
sp.setViewportView(layer);
this.repaint();
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {
visualizzaMouse = true;
repaint();
}
public void mouseExited(MouseEvent e) {
visualizzaMouse = false;
repaint();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
}