Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    hunterr1234 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default doing this using swings or awt?

    how do i create this in java using awt/swings ?
    I am new to graphics and creating gui's not only in java but any language.
    Last edited by hunterr1234; 10-31-2011 at 07:11 PM.

  2. #2
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: How do i create this board ?

    Do u really want to use awt/swing or is it obligation?

  3. #3
    hunterr1234 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: How do i create this board ?

    I thought thats the only way for creating gui's ....
    Last edited by hunterr1234; 11-03-2011 at 07:32 PM.

  4. #4
    hunterr1234 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: How do i create this board ?

    ......................................

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

    Default Re: How do i create this board ?

    Your question is very general and begs another question: Where exactly are you stuck?

    The geometry?
    The painting?

    And where specifically in either of these subcategories are you stuck?

  6. #6
    hunterr1234 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: How do i create this board ?

    I am sorry but i do not know how to start also ... i am doing my search and learning about the objects in the awt api's nut everything is very absract ...


    Thanks
    Last edited by hunterr1234; 11-03-2011 at 07:33 PM.

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

    Default Re: How do i create this board ?

    I would use Swing if a basic game or a 3rd party graphics library if a commercial game, and I'd start with the Oracle Swing tutorials, in particular the graphics section. You need to understand enough basics of Swing graphics before anyone can help you.

  8. #8
    hunterr1234 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: How do i create this board ?

    Yeah okay !!
    there is soo much to read man ... i got no enough time ...
    anyways i wil get back after reading and learning swings ...

  9. #9
    Join Date
    Aug 2011
    Location
    Pretoria, South Africa
    Posts
    27
    Blog Entries
    3
    Rep Power
    0

    Default Re: How do i create this board ?

    Well... you need to at least have some sup-class of the Window, like the JFrame, JWindow or JDialog... I would recommend using a JFrame as it seems like you are more of a new guy in java, next I would say using the paint method with the Graphics2D class to do some drawPolygon methods, after that its simply adding some JLabels with images representing the coins with DnD(Drag and Drop) capability's and and methods to do what you need. Ir really doesn't seem that hard... we can merely help you with your current code problems and help you in the right directions... otherwise you can also resource to this link

    Freelancer.com | Online Jobs | Freelance Employment | Outsourcing Services | Programmers | Web Design | Freelancers

  10. #10
    hunterr1234 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: How do i create this board ?

    Thanks a man
    Last edited by hunterr1234; 11-03-2011 at 07:33 PM.

  11. #11
    hunterr1234 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default Re: How do i create this board ?

    Also ??????????????
    Last edited by hunterr1234; 11-03-2011 at 07:34 PM.

  12. #12
    Join Date
    Aug 2011
    Location
    Pretoria, South Africa
    Posts
    27
    Blog Entries
    3
    Rep Power
    0

    Default Re: How do i create this board ?

    o my... I hope by this time you know how to use JFrame... lets move over to JPanel... here is some links
    Using JPanel as a canvas : JPanel**Swing**Java Tutorial

    Now for some drawing.
    Draw a Polygon : Shape**2D Graphics GUI**Java

    and then a good demo of MouseListener.
    Demonstrating the MouseListener and MouseMotionListener : Various Event Listener**Event**Java

  13. #13
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: How do i create this board ?

    here some code for you
    first array is x points
    second array is y points
    6 is number of points
    Shape p=new Polygon({10,20,30,40,50,60},{20,30,40,50,60,70},6) ;
    Graphics2D g=(Graphics2D) JComponent.getGraphics();
    g.setColor(Color.blue);
    g.draw(p);
    g.fill(p); //This method exists.I guest
    //JComponent is where you are drawing. Maybe panel

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

    Default Re: How do i create this board ?

    Quote Originally Posted by Grkn View Post
    here some code for you
    first array is x points
    second array is y points
    6 is number of points
    Shape p=new Polygon({10,20,30,40,50,60},{20,30,40,50,60,70},6) ;
    Graphics2D g=(Graphics2D) JComponent.getGraphics();
    g.setColor(Color.blue);
    g.draw(p);
    g.fill(p); //This method exists.I guest
    //JComponent is where you are drawing. Maybe panel
    Are you seriously recommending this code including recommending to get the Graphics2D object via calling getGraphics on a component? You do understand that this Graphics object will not persist and that anything drawn with it will disappear as soon as anything is repainted, right? And that your Polygon is really nothing more than a straight line, at most a "degenerate" case if you want to call it a polygon and certainly not a regular hexagon.

    Perhaps you want to preface this recommendation with your experience and your results when you tested this code.

  15. #15
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: How do i create this board ?

    oke then I will write this code after I compile it I will post
    That code just abstract code not designed for real stuff

    I showed how he can create Polygon and how can draw Polygon thats all

  16. #16
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: How do i create this board ?

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package showthestuff;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Polygon;
    import java.awt.Shape;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.geom.Area;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     *
     * @author acer
     */
    public class ShowTheStuff extends JPanel{
            int []xArr={100,100,200};
            int []yArr={100,200,200};
        Shape p=new Polygon(xArr, yArr, 3);
        Area a=new Area(p);
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2=(Graphics2D) g;
            g2.draw(p);
            g2.fill(p);   
        }
        /**
         * @param args the command line arguments
         */
        
        public ShowTheStuff(){
            // I write a point x=110  , y =140 then I checked the click on point
            if(a.contains(110,140)){
                System.out.println("Clicked on image");
            }   
            this.addMouseListener(new MouseListener() {
    
                @Override
                public void mouseClicked(MouseEvent e) {
                   // throw new UnsupportedOperationException("Not supported yet.");
                    ShowTheStuff.this.mouseClickedOnImage(e);
                }
    
                @Override
                public void mousePressed(MouseEvent e) {
               //     throw new UnsupportedOperationException("Not supported yet.");
                }
    
                @Override
                public void mouseReleased(MouseEvent e) {
                 //   throw new UnsupportedOperationException("Not supported yet.");
                }
    
                @Override
                public void mouseEntered(MouseEvent e) {
                   // throw new UnsupportedOperationException("Not supported yet.");
                }
    
                @Override
                public void mouseExited(MouseEvent e) {
                   // throw new UnsupportedOperationException("Not supported yet.");
                }
            });
        }
        private void mouseClickedOnImage(MouseEvent e) {
        //            throw new UnsupportedOperationException("Not yet implemented");
            if(a.contains(e.getX(),e.getY())){
                System.out.println("Clicked on image");
            }   
            
        }
        public static void main(String[] args) {
            // TODO code application logic here
            JFrame anew=new JFrame("try");
            anew.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            anew.setVisible(true);
            anew.setSize(500, 500);
            anew.add(new ShowTheStuff());
        }
    }
    *****************************************
    Here is a code that creates Triangle and fills inside also detects when mouse is clicked on image
    Last edited by Fubarable; 10-25-2011 at 06:34 PM. Reason: Code tags added

  17. #17
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: How do i create this board ?

    If someone changes or arranges Xarr and Y arr then you can draw any shape also detects mouse click is on image or not.

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

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

    Default Re: How do i create this board ?

    Quote Originally Posted by Grkn View Post
    oke then I will write this code after I compile it I will post
    That code just abstract code not designed for real stuff

    I showed how he can create Polygon and how can draw Polygon thats all
    I was thinking more along these lines:

    Hexagon.java
    Java Code:
    import java.awt.Color;
    import java.awt.Polygon;
    import java.awt.Rectangle;
    import java.awt.Shape;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.PathIterator;
    import java.awt.geom.Point2D;
    import java.awt.geom.Rectangle2D;
    
    public class Hexagon implements Shape {
       public static final int SIDES = 6;
       public static final Color DEFAULT_FILL_COLOR = Color.pink;
       private static final Color DEFAULT_DRAW_COLOR = Color.blue;
       
       private Point2D center;
       private double shortRadius;
       private double longRadius;
       private Point2D[] vertices = new Point2D[SIDES];
       private Polygon polygon = null;
       private Color fillColor = DEFAULT_FILL_COLOR;
       private Color drawColor = DEFAULT_DRAW_COLOR;
    
       public Hexagon(Point2D center, double shortRadius) {
          this.center = center;
          this.shortRadius = shortRadius;
          longRadius = 2 * shortRadius / Math.sqrt(3);
          int[] xpoints = new int[SIDES];
          int[] ypoints = new int[SIDES];
          vertices[0] = new Point2D.Double(center.getX(), center.getY()
                - longRadius);
          vertices[1] = new Point2D.Double(center.getX() + shortRadius,
                center.getY() - longRadius / 2);
          vertices[2] = new Point2D.Double(center.getX() + shortRadius,
                center.getY() + longRadius / 2);
          vertices[3] = new Point2D.Double(center.getX(), center.getY()
                + longRadius);
          vertices[4] = new Point2D.Double(center.getX() - shortRadius,
                center.getY() + longRadius / 2);
          vertices[5] = new Point2D.Double(center.getX() - shortRadius,
                center.getY() - longRadius / 2);
          
          for (int i = 0; i < xpoints.length; i++) {
             xpoints[i] = (int)vertices[i].getX();
             ypoints[i] = (int)vertices[i].getY();
          }
          
          polygon = new Polygon(xpoints, ypoints, SIDES);
       }
    
       public Point2D getCenter() {
          return center;
       }
    
       public double getShortRadius() {
          return shortRadius;
       }
    
       public double getLongRadius() {
          return longRadius;
       }
    
       public Point2D[] getVertices() {
          return vertices;
       }
       
       public Color getFillColor() {
          return fillColor;
       }
    
       public void setFillColor(Color fillColor) {
          this.fillColor = fillColor;
       }
    
       public Color getDrawColor() {
          return drawColor;
       }
    
       public void setDrawColor(Color drawColor) {
          this.drawColor = drawColor;
       }
    
       @Override
       public boolean contains(Point2D arg0) {
          return polygon.contains(arg0);
       }
    
       @Override
       public boolean contains(Rectangle2D arg0) {
          return polygon.contains(arg0);
       }
    
       @Override
       public boolean contains(double arg0, double arg1) {
          return polygon.contains(arg0, arg1);
       }
    
       @Override
       public boolean contains(double arg0, double arg1, double arg2, double arg3) {
          return polygon.contains(arg0, arg1, arg2, arg3);
       }
    
       @Override
       public Rectangle getBounds() {
          return polygon.getBounds();
       }
    
       @Override
       public Rectangle2D getBounds2D() {
          return polygon.getBounds2D();
       }
    
       @Override
       public PathIterator getPathIterator(AffineTransform arg0) {
          return polygon.getPathIterator(arg0);
       }
    
       @Override
       public PathIterator getPathIterator(AffineTransform arg0, double arg1) {
          return polygon.getPathIterator(arg0, arg1);
       }
    
       @Override
       public boolean intersects(Rectangle2D arg0) {
          return polygon.intersects(arg0);
       }
    
       @Override
       public boolean intersects(double arg0, double arg1, double arg2, double arg3) {
          return polygon.intersects(arg0, arg1, arg2, arg3);
       }
    
    }

    TestHex.java
    Java Code:
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Stroke;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Point2D;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    @SuppressWarnings("serial")
    public class TestHex extends JPanel {
       private static final Color FILL_COLOR = Color.pink;
       private static final Color SELECTED_FILL_COLOR = new Color(200, 200, 255);
       private static final Color BORDER_COLOR = Color.blue;
       private static final int HEX_COUNT = 8;
       private static final int SHORT_RADIUS = 50;
       private static final Stroke HEX_STROKE = new BasicStroke(3f);
       private List<Hexagon> hexList = new ArrayList<Hexagon>();
    
       public TestHex() {
          int startingX = 5 * SHORT_RADIUS;
          int startingY = 2 * SHORT_RADIUS;
          for (int j = 0; j < HEX_COUNT - 1; j++) {
             int jX = 3 - Math.abs(j - 3);
             int x0 = (int) (startingX - jX * SHORT_RADIUS);
             int y0 = (int) (startingY + j * Math.sqrt(3) * SHORT_RADIUS);
             for (int i = 0; i < jX + 4; i++) {
                int x = i * SHORT_RADIUS * 2 + x0;
                Point2D center = new Point2D.Double(x, y0);
                Hexagon hex = new Hexagon(center, SHORT_RADIUS);
                hex.setDrawColor(BORDER_COLOR);
                hex.setFillColor(FILL_COLOR);
                hexList.add(hex);
             }
          }
          
          addMouseListener(new MouseAdapter() {
             @Override
             public void mousePressed(MouseEvent e) {
                for (Hexagon hex : hexList) {
                   if (hex.contains(e.getPoint())) {
                      Color c = hex.getFillColor();
                      
                      c = (c == FILL_COLOR) ? SELECTED_FILL_COLOR : FILL_COLOR;
                      hex.setFillColor(c);
                      repaint();
                      return;
                   }
                }
             }
          });
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(2 * SHORT_RADIUS * HEX_COUNT, 2 * SHORT_RADIUS
                * HEX_COUNT);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
          for (Hexagon hex : hexList) {
             g2.setColor(hex.getFillColor());
             g2.fill(hex);
          }
          Stroke originalStroke = g2.getStroke();
          g2.setStroke(HEX_STROKE);
          for (Hexagon hex : hexList) {
             g2.setColor(hex.getDrawColor());
             g2.draw(hex);
          }
          g2.setStroke(originalStroke);
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("TestHex");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new TestHex());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    Last edited by Fubarable; 10-25-2011 at 08:39 PM.

  20. #20
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: How do i create this board ?

    ahahha I have not time to deal with those things :)

    Todays I am dealing with android
    development framework.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 06-29-2011, 09:08 AM
  2. how to create a monopoly board using java?
    By dizzymj17 in forum AWT / Swing
    Replies: 4
    Last Post: 04-18-2011, 02:38 PM
  3. TINI Board
    By ApheoXAIO in forum New To Java
    Replies: 1
    Last Post: 07-03-2010, 12:52 PM
  4. How to do a score board?
    By vlan in forum Java Applets
    Replies: 11
    Last Post: 06-03-2010, 10:10 AM
  5. how to create board in java...
    By zenith in forum New To Java
    Replies: 5
    Last Post: 01-20-2010, 04:42 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
  •