Results 1 to 5 of 5
  1. #1
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: scrollRectToVisible not working right

    Quote Originally Posted by Alkor View Post
    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?
    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.

    ... and how could i fix 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.
    Last edited by Fubarable; 03-25-2012 at 11:47 PM.

  3. #3
    Alkor is offline Member
    Join Date
    Feb 2012
    Posts
    39
    Rep Power
    0

    Default Re: scrollRectToVisible not working right

    The Rect:
    private Rectangle PlayerXYRect = new Rectangle();
    Java Code:
    public Rectangle playerRect(){
            PlayerXYRect.setBounds(p.getPlayerX()- 400, p.getPlayerY() - 300, p.getPlayerX() + 400, p.getPlayerY() + 300);
            return PlayerXYRect;
        }
    Scrolling
    Java Code:
    if(e.getKeyCode() == KeyEvent.VK_A){
    p.moveLeft();
    repaint();
    scrollRectToVisible(playerRect());
            }
    Last edited by Alkor; 03-25-2012 at 11:56 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: scrollRectToVisible not working right

    Quote Originally Posted by Alkor View Post
    The Rect:
    private Rectangle PlayerXYRect = new Rectangle();
    Java Code:
    public Rectangle playerRect(){
            PlayerXYRect.setBounds(p.getPlayerX()- 400, p.getPlayerY() - 300, p.getPlayerX() + 400, p.getPlayerY() + 300);
            return PlayerXYRect;
        }
    Scrolling
    Java Code:
    if(e.getKeyCode() == KeyEvent.VK_A){
    p.moveLeft();
    repaint();
    scrollRectToVisible(playerRect());
            }
    We can neither compile nor run code snippets. I guess you didn't really read my previous response or click on the link that it contains.

    Best of luck.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. Collisions are working & not working
    By Jayayoh in forum Java 2D
    Replies: 1
    Last Post: 06-24-2011, 06:21 PM
  2. if else not working
    By silverglade in forum New To Java
    Replies: 5
    Last Post: 05-12-2011, 08:29 PM
  3. Replies: 2
    Last Post: 01-04-2011, 05:30 AM
  4. Why is my Do/While Not working?
    By Meta in forum New To Java
    Replies: 1
    Last Post: 05-11-2010, 07:05 PM
  5. Replies: 8
    Last Post: 05-28-2008, 08:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •