Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The below code places my 8 shapes in 8 distinct positions.

Shape aShape = new RoundRectangle2D.Double(60,112,310,60,10,10);
Shape bShape = new RoundRectangle2D.Double(60,194,310,60,10,10);
Shape cShape = new RoundRectangle2D.Double(60,276,310,60,10,10);
Shape dShape = new RoundRectangle2D.Double(60,358,310,60,10,10);	
Shape eShape = new RoundRectangle2D.Double(60,440,310,60,10,10);
Shape fShape = new RoundRectangle2D.Double(60,522,310,60,10,10);
Shape gShape = new RoundRectangle2D.Double(60,604,310,60,10,10);
Shape hShape = new RoundRectangle2D.Double(60,686,310,60,10,10);


I have hardcoded the position of Font also in the shape.

//		aShape border color
		graphics2D.setStroke(new BasicStroke(borderThickness));
		graphics2D.setColor(Color.WHITE);
		
//		Drawing a Rounded Rectangle for A
		graphics2D.draw(aShape);
		graphics2D.setStroke(oldStroke);
		
//		Setting the Background Color
		graphics2D.setColor(color); 
		graphics2D.fill(aShape);
		
//		Setting the font inside the shape
		Font aFont = new Font("Serif", Font.PLAIN,25);
		graphics2D.setFont(aFont);
		graphics2D.setColor(Color.WHITE);
		graphics2D.drawString(ProjectConstants.A_KEY, 80,148);



But now I want to show only 4 at a time and introduce scrolling. Since the X and Y positions are hardcoded I am clueless as to how will I scroll my shapes ?

Can someone share some insights on how to dynamically load my graphical shapes and place it in a proper position when Scrolled ?

What I have tried:

I tried to put 4 shapes in one panel and another 4 shapes in another panel, and tried to introduce Scrolling, but that would replace 4 shapes in one scroll.
Posted
Updated 9-Jan-17 23:00pm

1 solution

This is the same issue you asked at Can I add key listener to graphical shapes in java AWT ?[^]. And I already explained that to scroll shapes you need to calculate the new positions based on how many pixels to move per keypress. You then redraw the shape at the new position.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900