Results 1 to 5 of 5
- 03-25-2012, 10:40 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 39
- Rep Power
- 0
scrollRectToVisible not working right
So im guess this is a problem with java becuase I have a rectangle which bounds are (x - 400,y- 300, x + 400, y - 300) the frame is 800,600 and it scrolls visable to that rectangle it works fine as long as you go down or right but not left or up... is this a problem with java or me? and how could i fix it.
-
Re: scrollRectToVisible not working right
From my own personal experience, whenever I've asked this question, the problem has been with me.
From my experience in this forum 99.9% of the time where posters have asked this question, the problem has been with the poster's code.
I'm willing to wager $1000 (or more) that the problem is with you, and this is not meant as an insult -- far from it.
Without our seeing compilable and runnable code, the answer is anybody's guess. If you need our help though, consider creating and posting an SSCCE, a *small* program that is complete enough to be compilable, runnable and be able to demonstrate your problem, but that contains no code unnecessary for demonstrating your problem and is small enough so as not to drown us in a lot of unrelated code.... and how could i fix it.Last edited by Fubarable; 03-25-2012 at 10:47 PM.
- 03-25-2012, 10:53 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 39
- Rep Power
- 0
Re: scrollRectToVisible not working right
The Rect:
private Rectangle PlayerXYRect = new Rectangle();
ScrollingJava Code:public Rectangle playerRect(){ PlayerXYRect.setBounds(p.getPlayerX()- 400, p.getPlayerY() - 300, p.getPlayerX() + 400, p.getPlayerY() + 300); return PlayerXYRect; }
Java Code:if(e.getKeyCode() == KeyEvent.VK_A){ p.moveLeft(); repaint(); scrollRectToVisible(playerRect()); }Last edited by Alkor; 03-25-2012 at 10:56 PM.
-
Re: scrollRectToVisible not working right
-
Re: scrollRectToVisible not working right
For example, an SSCCE like this:
Java Code:import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.*; public class ScrollToRectEg extends JPanel { private static final Color[] COLORS = {Color.red, Color.black, Color.orange, Color.blue, Color.yellow, Color.green, Color.cyan, Color.magenta}; private static final int PREF_W = 200; private static final int PREF_H = 200; private static final int RECT_WIDTH = 100; private static final int RP_W = 1500; private static final int RP_H = RP_W; private Rectangle[] rectangles = new Rectangle[4 * COLORS.length]; Random random = new Random(); private JScrollPane scrollpane; private JPanel rectPanel = new JPanel(){ protected void paintComponent(java.awt.Graphics g) { super.paintComponent(g); myPaint(g); }; }; public ScrollToRectEg() { rectPanel.setPreferredSize(new Dimension(RP_W, RP_H)); for (int i = 0; i < rectangles.length; i++) { int x = 50 + random.nextInt(RP_W - RECT_WIDTH - 100); int y = 50 + random.nextInt(RP_H - RECT_WIDTH - 100); rectangles[i] = new Rectangle(x, y, RECT_WIDTH, RECT_WIDTH); } setLayout(new BorderLayout()); scrollpane = new JScrollPane(rectPanel); add(scrollpane, BorderLayout.CENTER); int timerDelay = 1000; new Timer(timerDelay , new ActionListener() { private int rectanglesIndex = 0; @Override public void actionPerformed(ActionEvent arg0) { JViewport viewport = scrollpane.getViewport(); Rectangle selectedRect = rectangles[rectanglesIndex]; // position the rectangle in the center of the viewport int width = viewport.getWidth(); int height = viewport.getHeight(); int x = selectedRect.x - (width - selectedRect.width) / 2; int y = selectedRect.y - (height - selectedRect.height) / 2; Rectangle viewPortRect = new Rectangle(x, y, width, height); rectPanel.scrollRectToVisible(viewPortRect); scrollpane.getViewport().repaint(); rectanglesIndex++; rectanglesIndex %= rectangles.length; } }).start(); } protected void myPaint(Graphics g) { Graphics2D g2 = (Graphics2D) g; for (int i = 0; i < rectangles.length; i++) { g2.setColor(COLORS[i % COLORS.length]); g2.fill(rectangles[i]); } } @Override public Dimension getPreferredSize() { return new Dimension(PREF_W, PREF_H); } private static void createAndShowGui() { ScrollToRectEg mainPanel = new ScrollToRectEg(); JFrame frame = new JFrame("Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
Similar Threads
-
Collisions are working & not working
By Jayayoh in forum Java 2DReplies: 1Last Post: 06-24-2011, 05:21 PM -
if else not working
By silverglade in forum New To JavaReplies: 5Last Post: 05-12-2011, 07:29 PM -
\n not working in GUI (working code, but \n isn't working)
By cc11rocks in forum New To JavaReplies: 2Last Post: 01-04-2011, 04:30 AM -
Why is my Do/While Not working?
By Meta in forum New To JavaReplies: 1Last Post: 05-11-2010, 06:05 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks