Skip to content

graphics rotation interacts incorrectly with clipping #2567

@ddyer0

Description

@ddyer0

combining clipping regions with graphics rotation doesn't work correctly. I believe
the simulator behavior is correct. IOS initially is also correct, but curiously, if you then
rotate the orientation, subsequent calls to paint produce incorrect results. Android
never works right.

package dtest.boardspace;
// 
// this version shows that g.rotate interacts badly with clipping
//
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.ui.Graphics;
import com.codename1.ui.layouts.BorderLayout;

public class Dtest {

private Form current;
@SuppressWarnings("unused")
private Resources theme;

public void init(Object context) {
    theme = UIManager.initFirstTheme("/theme");
    // Pro only feature, uncomment if you have a pro subscription
    // Log.bindCrashProtection(true);
}
int loops = 0;
public void start() {
    if(current != null){
        current.show();
        return;
    }
    Form hi = new Form("Hi >>0 World");
     current = hi;
    hi.setLayout(new BorderLayout());
    hi.addComponent(BorderLayout.CENTER, new Label("Hi Overlay World 2") {

        @Override
        public void paint(Graphics g) {
        	//g.resetAffine();
         	int w = getWidth();
        	int h = getHeight();
        	float ang = (float)(Math.PI);
        	
         	g.setColor(0x8f8f9f7f);
        	g.fillRect(0,0,w,h);

        	g.setColor(0xff);
        	int [] original_clip = g.getClip();
        	
        	// clip
        	g.clipRect(100, 100, 100, 100);
        	g.fillRect(0,0,w,h);
        	g.setColor(0);
        	g.setClip(original_clip);
        	g.drawString("clip only",110,150);
        	
        	// clip first, then rotate
        	g.rotate(ang,w/2,h/2);
        	g.setColor(0xff00);
        	g.clipRect(100,200,100,100);
        	g.fillRect(0, 0,w,h);
        	g.setColor(0);
        	g.setClip(original_clip);
        	g.drawString("clip,rotate",110,250);
        	g.rotate(-ang,w/2,h/2);
  
        	// rotate first, then clip
           	// clip first, then rotate
         	g.rotate(ang,w/2,h/2);
        	g.clipRect(100,300,100,100);
        	g.setColor(0xff0000);
        	g.fillRect(0, 0,w,h);
        	g.setColor(0);
        	g.setClip(original_clip);
        	g.drawString("rotate,clip",110,350);
        	g.rotate(-ang,w/2,h/2);
   
   
        	loops++;
        
        }
        
    });
    hi.show();
	Runnable rr = new Runnable (){ 
		public void run() {
			System.out.println("running");
		while(true) 
		{ 
		  hi.repaint();
		  try {
			  Thread.sleep(1); 
			  }
		  
		  catch (InterruptedException e) {};
		}}};
	new Thread(rr).start();
}
public void stop() {
    current = Display.getInstance().getCurrent();
}

public void destroy() {
}
}

This is a sample from the simulator
cliptest

this is the same program, on android
screenshot_2018-09-28-12-18-34

this the same result on IOS, launching the test program in portrait mode
them rotating to horizontal mode.
img_0118

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions