Results 1 to 8 of 8
  1. #1
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default Drawing in Java? or Maybe movable buttons?

    Hello, I wanted to create a program that could calculate how big amount of boxes can a room fit. Boxes are different size all the time and you never know what dimension you are going to get. I wanted to make it somewhat visual by lets say a user enters dimensions and a box is drawn. I also need to make sure that the box fits in the room and they are not drawn one over another.

    My idea is to just simply create buttons that user can move so user can see for himself if they fit or not, also have a function that auto arranges boxes (This is extra for later)

    Now I Need a way to do this and i never learned anything like this in school, I only finished 1 year of java programming and got few months till next so I am trying to do something new to keep up with my programming.

    Is there a way to make buttons movable once created in a compiled program. Or is there a way for java to draw regular boxes in compiled program and delete them?

    Thanks upfront for taking the time to help me out.


    EDIT: The movable buttons needs to be dragged with a mouse and be leveled up with edges of the room or other boxes.
    Last edited by Bagzli; 06-29-2011 at 02:41 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Yes you can draw things at different locations and move them around.
    Look at Mouse Listeners for how to detect where the mouse/cursor is located and where it is moving.
    Look at overriding the paintComponent() method for redrawing a shape on a panel.

  3. #3
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Like Norm said, stick to drawing. Don't try to shift components like buttons or panels around: they have "a life of their own" (risizing, being influenced by layoutmanagers etc.).
    If you use java.awt.geom.Rectangle2D or other Shapes and don't rotate your boxes, you have a built in collisiondetection for points and rectangles with contains() and intersects().
    Last edited by Jodokus; 06-29-2011 at 12:37 PM. Reason: spelling
    No bug ever had to calculate its fitnessfunction.

  4. #4
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by Jodokus View Post
    Like Norm said, stick to drawing. Don't try to shift components like buttons or panels around: they have "a life of their own" (risizing, being influenced by layoutmanagers etc.).
    If you use java.awt.geom.Rectangle2D or other Shapes and don't rotate your boxes, you have a build in collisiondetection for points and rectangles with contains() and intersects().
    sorry didn't quite understand this: "you have a build in collisiondetection for points and rectangles with contains() and intersects()"

    Would you please elaborate?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    I also need to make sure that the box fits in the room and they are not drawn one over another.
    Jodokus comments were an answer to your concern. The Rectangle class has the methods he mentioned that will help you with your concern

  6. #6
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    I'v never used Rectangle2D class before nor have i ever drawn something with Java, I was wondering if somebody can point me in the right direction with a guide maybe? I was googling for some example and it appears some guy is using Applets to draw, now I'm not sure if this is how you really are suppose to do it or is there a more efficient way?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    The code to draw shapes uses the Graphics class methods in the paintComponent() method. It has nothing to do with applets. Search on the forum for paintComponent or for fillRect or for drawLine for sample code that draws shapes.

  8. #8
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Would you please elaborate?
    Norm explained most. If you want to know when your cursor is on a box, when boxes are colliding or when your boxes try to penetrate the wall, you need some form of collisiondetection. Shapes (including the mentioned rectangle) have some methods to do this for you.
    Just try to practice some basic drawing, then add moving by the cursor (not the same as just following the cursor as in the next tutorial), then try to make your boxes and wall inpenetrable.
    The basic paintingtutorial Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) shifts a box around, but doesn't use Shapes. (I now risk banning for telling too much.)
    Last edited by Jodokus; 06-29-2011 at 01:19 PM. Reason: functions to methods
    No bug ever had to calculate its fitnessfunction.

Similar Threads

  1. Drawing labels/buttons on an image
    By Eric Coulthard in forum Java 2D
    Replies: 0
    Last Post: 05-25-2010, 11:11 PM
  2. Java SWT Transparent Buttons
    By ynnorj in forum AWT / Swing
    Replies: 7
    Last Post: 11-25-2009, 06:48 AM
  3. JToolBar movable but not floatable
    By Fosters in forum AWT / Swing
    Replies: 0
    Last Post: 08-22-2008, 04:04 PM
  4. movable image with buttons
    By boytjie in forum NetBeans
    Replies: 0
    Last Post: 04-18-2008, 01:00 PM
  5. Replies: 4
    Last Post: 05-19-2007, 12:15 PM

Posting Permissions

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