001package jmri.jmrit.display.controlPanelEditor.shape; 002 003import java.awt.Rectangle; 004import java.awt.event.ActionEvent; 005import java.awt.geom.Ellipse2D; 006 007import javax.swing.BoxLayout; 008import javax.swing.JLabel; 009import javax.swing.JPanel; 010import javax.swing.JTextField; 011 012import jmri.jmrit.display.Editor; 013import jmri.util.swing.JmriMouseEvent; 014import jmri.util.swing.JmriMouseMotionListener; 015 016/** 017 * @author Pete Cressman Copyright (c) 2012 018 */ 019public class DrawCircle extends DrawFrame { 020 021 JTextField _diameterText; 022 023 public DrawCircle(String which, String title, PositionableShape ps, Editor ed, boolean create) { 024 super(which, title, ps, ed, create); 025 } 026 027 @Override 028 protected JPanel makeParamsPanel() { 029 JPanel panel = super.makeParamsPanel(); 030 031 JPanel p = new JPanel(); 032 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); 033// p.add(new JLabel(Bundle.getMessage("Circle"))); 034 JPanel pp = new JPanel(); 035 _diameterText = new JTextField(6); 036 _diameterText.setText(Integer.toString(_shape.getWidth())); 037 _diameterText.setHorizontalAlignment(JTextField.RIGHT); 038 pp.add(_diameterText); 039 _diameterText.addMouseMotionListener(JmriMouseMotionListener.adapt( 040 new JmriMouseMotionListener() { 041 @Override 042 public void mouseDragged(JmriMouseEvent e) { 043 updateShape(); 044 } 045 046 @Override 047 public void mouseMoved(JmriMouseEvent e) { 048 _shape.setWidth(getInteger(_diameterText, _shape.getWidth())); 049 updateShape(); 050 } 051 })); 052 _diameterText.addActionListener((ActionEvent e) -> { 053 _shape.setWidth(getInteger(_diameterText, _shape.getWidth())); 054 updateShape(); 055 }); 056 pp.add(new JLabel(Bundle.getMessage("circleRadius"))); 057 p.add(pp); 058 panel.add(p); 059 return panel; 060 } 061 062 @Override 063 protected PositionableShape makeFigure(Rectangle r, Editor ed) { 064 if (r != null) { 065 int dia = Math.max(r.width, r.height); 066 Ellipse2D.Double rr = new Ellipse2D.Double(0, 0, dia, dia); 067 _shape = new PositionableCircle(ed, rr); 068 } 069 return _shape; 070 } 071 072 @Override 073 void setDisplayWidth(int w) { 074 _diameterText.setText(Integer.toString(w)); 075 } 076 077 @Override 078 void setDisplayHeight(int h) { 079 _diameterText.setText(Integer.toString(h)); 080 } 081}