Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Points in a JPanel

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Points in a JPanel

    So i am making a GUI that lets you add in vertices onto a JPanel and so far I have:

    import javax.swing.*;
     
    import java.awt.*;
    import java.util.ArrayList;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
     
    public class Main {
     
    	static ArrayList<Vertices> Vertices = new ArrayList<Vertices>();
     
    	static JPanel panel = new JPanel();
    	static JPanel spacing = new JPanel();
    	static JButton AddVert = new JButton("Add Vertex");
    	static JButton AddEdge = new JButton("Add Edge");
    	static JButton RemVert = new JButton("Remove Vertex");
    	static JButton RemEdge = new JButton("Remove Edge");
    	static JPanel Output = new JPanel();
    	static JScrollPane Output2 = new JScrollPane();
    	static JFrame Frame = new JFrame();
     
    	private static Point points[];
     
     
    	public static void main(String[] args) {
    		JFrame Window = new JFrame("Graph");
    		//JFrame.setDefaultLookAndFeelDecorated(true);
     
    		Output.setPreferredSize(new Dimension(950,600));
    		Output2.setPreferredSize(new Dimension(200,600));
     
    		Window.setLayout(new BorderLayout(50, 10));
    		//Window.setIconImage(new ImageIcon("H:\\Graph\\img.png").getImage());
     
    	    Container pane = Window.getContentPane();
    		panel.add(AddVert);
    		panel.add(AddEdge);
    		panel.add(RemVert);
    		panel.add(RemEdge);
    		pane.add(spacing, BorderLayout.NORTH);
    		pane.add(spacing, BorderLayout.EAST);
    		pane.add(panel, BorderLayout.SOUTH);
    	    pane.add(Output2, BorderLayout.WEST);
    	    pane.add(Output, BorderLayout.CENTER);
    	    Output.add(new Vertices());
     
    	    Window.pack();
    		Window.setVisible(true);
    	}
    }

    I also have a class Vertices

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Insets;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.util.Random;
     
     
    public class Vertices extends JPanel {
     
    		public void paintComponent(Graphics g) {
    		      super.paintComponent(g);
     
    		      Graphics2D g2d = (Graphics2D) g;
     
    		      g2d.setColor(Color.blue);
     
    		      Dimension size = getSize();
    		      Insets insets = getInsets();
    		      int w =  size.width - insets.left - insets.right;
          		      int h =  size.height - insets.top - insets.bottom;
     
    		      Random r = new Random();
     
    		      for (int i=0; i<1000; i++) {
    		          int x = Math.abs(r.nextInt()) % w;
    		          int y = Math.abs(r.nextInt()) % h;
    		          g2d.drawLine(x, y, x, y);
    		      }
    		}
    	}

    I couldn't get it to work on its own so I am trying to make it randomly generate points around the JPanel but its doesn't seem to get it to work


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Points in a JPanel

    You're going to have to be more specific than that. What exactly do you mean when you say it doesn't work? Does it only draw a single point? Does it draw too many? Does it draw different points than what you'd expect? Does it throw an Exception? Can you see the JPanel at all?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Points in a JPanel

    The GUI and the JPanel work alright

    the points are the problem. The area they are drawn into is really small and when I try to change the size of the area it doesn't work at all

    So I want the points to spread out all over the JPanel

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Points in a JPanel

    I'd check your math with the insets. Trace through it with some example heights, widths, and insets and see what comes out. I'm not even sure why you're dealing with insets since your component doesn't have a border. You might want to take them out entirely until you need them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Points in a JPanel

    So at this point:

    for (int i=0; i<1000; i++) {
    		          int x = r.nextInt() % w;
    		          int y = r.nextInt() % h;
    		          g2d.drawLine(x, y, x, y);
    		      }

    What should I change for the size because I can't work out what size I would need to make it into a larger area

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Points in a JPanel

    Quote Originally Posted by Trunk Monkeey View Post
    So at this point:

    for (int i=0; i<1000; i++) {
    		          int x = r.nextInt() % w;
    		          int y = r.nextInt() % h;
    		          g2d.drawLine(x, y, x, y);
    		      }

    What should I change for the size because I can't work out what size I would need to make it into a larger area
    The problem isn't with those lines, it's with how you're calculating w and h in the first place. I'd throw some print statements in there to double-check your calculations.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Points in a JPanel

    Yeah I put in some print statements and I tried just changing the numbers of h and w but it stays the same size but with less points

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Points in a JPanel

    Did the print statements make sense? Did they print out the correct width and height?

    Also, I'd recommend using fillOval() instead of drawLine() to draw a point.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Points in a JPanel

    Well h and w are 10 and x and y range between like 1-10

    If I try to put a higher value for x and y nothing will come up.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Points in a JPanel

    What are h and w supposed to represent? Height and width, right? If so, 10 x 10 is pretty small, since those values are pixels. Also, they shouldn't be fluctuating unless you're resizing your JPanel, right? Like I said, there's something off with your calculation involving insets, which I'm not sure why you're using at all.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Points in a JPanel

    I got rid of the inset but whenever I make y or x larger there is nothing in the JPanel

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Points in a JPanel

    Quote Originally Posted by Trunk Monkeey View Post
    I got rid of the inset but whenever I make y or x larger there is nothing in the JPanel
    You're going to have to throw together an SSCCE that demonstrates exactly what you're doing then.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Distance between 2 points
    By captain in forum Java Theory & Questions
    Replies: 3
    Last Post: February 22nd, 2012, 12:53 AM
  2. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  3. ArrayList Plotting Points
    By basketball8533 in forum Collections and Generics
    Replies: 1
    Last Post: October 18th, 2011, 03:59 PM
  4. I like plotting points :) HI!
    By fractalorbit in forum Member Introductions
    Replies: 1
    Last Post: September 5th, 2011, 10:49 AM
  5. Getting all points in line
    By Mike in forum Java Theory & Questions
    Replies: 7
    Last Post: September 13th, 2010, 11:59 AM

Tags for this Thread