MCPcopy Create free account
hub / github.com/codenameone/CodenameOne / paint

Method paint

CodenameOne/src/com/codename1/components/Switch.java:724–846  ·  view source on GitHub ↗
(Graphics g)

Source from the content-addressed store, hash-verified

722
723 /// {@inheritDoc}
724 @Override
725 public void paint(Graphics g) {
726
727 Image cthumbImage = getCurrentThumbImage();
728 Image cTrackImage = value ? getCurrentTrackOnImage() : getCurrentTrackOffImage();
729 //Image ctrackOnImage = getCurrentTrackOnImage();
730 //Image ctrackOffImage = getCurrentTrackOffImage();
731 int strackLength = Math.max(cTrackImage.getWidth(), cTrackImage.getWidth());
732 int sheight = Math.max(cthumbImage.getHeight(), Math.max(cTrackImage.getHeight(), cTrackImage.getHeight()));
733
734 int vdeltaX = -deltaX; //virtual increase in slider "value" where OFF state = 0 and ON state = itrackLength
735 if (isRTL()) {
736 vdeltaX = deltaX;
737 }
738
739 Style s = getStyle();
740 int padLeft = s.getPaddingLeft(isRTL()); //s.getPaddingLeftNoRTL();
741 int padRight = s.getPaddingRight(isRTL());
742 int padTop = s.getPaddingTop();
743 int padBot = s.getPaddingBottom();
744 int innerHeight = getHeight() - padTop - padBot; //cache it to avoid calling getPadding multiple times, which costs CPU as it make the unit conversion at each call...
745 int innerWidth = getWidth() - padLeft - padRight;
746 int halign = s.getAlignment(); //TODO: swap left and right if RTL
747
748 int thumbrX = 0; //X position of the thumb relative to the start of the track
749 if (isRTL()) {
750 if (!value) {
751 thumbrX = cTrackImage.getWidth() - cthumbImage.getWidth();
752 }
753 } else {
754 if (value) {
755 thumbrX = cTrackImage.getWidth() - cthumbImage.getWidth();
756 }
757 }
758
759 Image nextThumbImage = null;
760 Image nextTrackImage = null;
761 double nextImageProgress = 0.0;
762
763 if (value) { //switch is ON so only drag movements going to the OFF position are relevant, meaning a vdelta < 0
764 if (vdeltaX > 0) {
765 dragged = false;
766 } else {
767 nextThumbImage = getThumbOffImage();
768 nextTrackImage = getTrackOffImage();
769 int trackMLength = cTrackImage.getWidth() - cthumbImage.getWidth();
770 if (vdeltaX < -trackMLength) {
771 vdeltaX = -trackMLength;
772 }
773 nextImageProgress = Math.abs(vdeltaX) / (double) Math.abs(trackMLength);
774
775
776 }
777 } else { //switch is OFF so only consider drag movements toward the ON position, meaning a vdelta > 0
778 if (vdeltaX < 0) {
779 dragged = false;
780 } else {
781 nextThumbImage = getThumbOnImage();

Callers

nothing calls this directly

Calls 15

getCurrentThumbImageMethod · 0.95
maxMethod · 0.95
getWidthMethod · 0.95
getHeightMethod · 0.95
getPaddingLeftMethod · 0.95
getPaddingRightMethod · 0.95
getPaddingTopMethod · 0.95
getPaddingBottomMethod · 0.95
getAlignmentMethod · 0.95
getThumbOffImageMethod · 0.95

Tested by

no test coverage detected