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 7 of 7

Thread: Making a Stop sign

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Making a Stop sign

    import javax.swing.*;
     
    import java.awt.*;
    public class StopSign extends JFrame {
     
    public void paint(Graphics gr){
    	super.paint(gr);
    	int[] xPoints = {90, 180, 300, 390, 390, 300, 180, 90 };
    	int[] yPoints = {180, 90, 90,  180,  300, 390, 390, 300 };
    	int numPoints = 8;
    	gr.drawPolygon(xPoints, yPoints, xPoints.length);
    	gr.setColor(Color.RED);
    	gr.fillPolygon(xPoints, yPoints, numPoints);
    	int[] xPoints1 = {100, 190, 290, 380, 380, 290, 190, 100 };
    	int[] yPoints1 = {190, 100, 100,  190,  290, 380, 380, 290 };
    	int numPoints1 = 8;
    	gr.drawPolygon(xPoints1, yPoints1, xPoints1.length);
    	gr.setColor(Color.white);
    	gr.fillPolygon(xPoints1, yPoints1, numPoints1);
    	int[] xPoints2 = {120, 200, 280, 360, 360, 280, 200, 120 };
    	int[] yPoints2 = {200, 120, 120,  200,  280, 360, 360, 280 };
    	int numPoints2 = 8;
    	gr.drawPolygon(xPoints2, yPoints2, xPoints2.length);
    	gr.setColor(Color.red);
    	gr.fillPolygon(xPoints2, yPoints2, numPoints2);
    }
     
    public static void main(String[] args){
    	StopSign frame = new StopSign();
    	frame.setSize(500,500);
    	frame.setVisible(true);
     
    }
    }

    So i made a stop sign using the following code. However, i need to put the word STOP in the middle of it ... I don't know how to put a the word on top of the polygon its self.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,075
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Making a Stop sign

    how to put a the word on top of the polygon its self.
    Use the Graphic class's drawString() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Making a Stop sign

    import javax.swing.*;
    import java.awt.*;
    public class StopSign extends JFrame {
    String Stop = new String("STOP");
    public void paint(Graphics gr){
    	super.paint(gr);
    	int[] xPoints = {90, 180, 300, 390, 390, 300, 180, 90 };
    	int[] yPoints = {180, 90, 90,  180,  300, 390, 390, 300 };
    	int numPoints = 8;
    	gr.drawPolygon(xPoints, yPoints, xPoints.length);
    	gr.setColor(Color.RED);
    	gr.fillPolygon(xPoints, yPoints, numPoints);
    	int[] xPoints1 = {100, 190, 290, 380, 380, 290, 190, 100 };
    	int[] yPoints1 = {190, 100, 100,  190,  290, 380, 380, 290 };
    	int numPoints1 = 8;
    	gr.drawPolygon(xPoints1, yPoints1, xPoints1.length);
    	gr.setColor(Color.white);
    	gr.fillPolygon(xPoints1, yPoints1, numPoints1);
    	int[] xPoints2 = {120, 200, 280, 360, 360, 280, 200, 120 };
    	int[] yPoints2 = {200, 120, 120,  200,  280, 360, 360, 280 };
    	int numPoints2 = 8;
    	gr.drawPolygon(xPoints2, yPoints2, xPoints2.length);
    	gr.setColor(Color.red);
    	gr.fillPolygon(xPoints2, yPoints2, numPoints2);
    }
    public void paint2(Graphics gr){
    	super.paint(gr);
    	gr.drawString(Stop, 0, 0);
    	gr.setFont(new Font("Arial", Font.BOLD, 22)); 
    	gr.setColor(Color.blue);
    }
    public static void main(String[] args){
    	StopSign frame = new StopSign();
    	frame.setSize(500,500);
    	frame.setVisible(true);
    }
    }

    I used this code to just see if STOP would appear in the JFrame but it didnt...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,075
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Making a Stop sign

    Where is the paint2() method called? If a method is not called, it won't be executed.

    Put all the code in the paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Making a Stop sign

    import javax.swing.*;
    import java.awt.*;
    public class StopSign extends JFrame {
    String Stop = new String("STOP");
    public void paint(Graphics gr){
    	super.paint(gr);
    	int[] xPoints = {90, 180, 300, 390, 390, 300, 180, 90 };
    	int[] yPoints = {180, 90, 90,  180,  300, 390, 390, 300 };
    	int numPoints = 8;
    	gr.drawPolygon(xPoints, yPoints, xPoints.length);
    	gr.setColor(Color.RED);
    	gr.fillPolygon(xPoints, yPoints, numPoints);
    	int[] xPoints1 = {100, 190, 290, 380, 380, 290, 190, 100 };
    	int[] yPoints1 = {190, 100, 100,  190,  290, 380, 380, 290 };
    	int numPoints1 = 8;
    	gr.drawPolygon(xPoints1, yPoints1, xPoints1.length);
    	gr.setColor(Color.white);
    	gr.fillPolygon(xPoints1, yPoints1, numPoints1);
    	int[] xPoints2 = {120, 200, 280, 360, 360, 280, 200, 120 };
    	int[] yPoints2 = {200, 120, 120,  200,  280, 360, 360, 280 };
    	int numPoints2 = 8;
    	gr.drawPolygon(xPoints2, yPoints2, xPoints2.length);
    	gr.setColor(Color.red);
    	gr.fillPolygon(xPoints2, yPoints2, numPoints2);
    	gr.drawString(Stop, 0, 0);
    	gr.setFont(new Font("Arial", Font.BOLD, 22)); 
    	gr.setColor(Color.blue);
    }
    public static void main(String[] args){
    	StopSign frame = new StopSign();
    	frame.setSize(500,500);
    	frame.setVisible(true);
    }
    }

    Alright, I put the code all under the paint method. However, the text is still not visible.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Making a Stop sign

    You're drawing the String at 0, 0. Test out different x and y values of where you're drawing your String to see what happens.

    --- Update ---

    Once you have this licked, you'll likely want to draw in the paintComponent(...) method of a JPanel that is displayed in the JFrame rather than directly in the paint(...) method of a JFrame, but one step at a time. Please have a look at the drawing with Swing tutorial: Lesson: Performing Custom Painting

  7. The Following User Says Thank You to curmudgeon For This Useful Post:

    remedys (December 3rd, 2012)

  8. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Making a Stop sign

    Thank you I finished my code with an awesome stop sign! end result :
    import javax.swing.*;
    import java.awt.*;
    public class StopSign extends JFrame {
    String Stop = new String("STOP");
    public void paint(Graphics gr){
    super.paint(gr);
    int[] xPoints = {90, 180, 300, 390, 390, 300, 180, 90 };
    int[] yPoints = {180, 90, 90, 180, 300, 390, 390, 300 };
    int numPoints = 8;
    gr.drawPolygon(xPoints, yPoints, xPoints.length);
    gr.setColor(Color.RED);
    gr.fillPolygon(xPoints, yPoints, numPoints);
    int[] xPoints1 = {100, 190, 290, 380, 380, 290, 190, 100 };
    int[] yPoints1 = {190, 100, 100, 190, 290, 380, 380, 290 };
    int numPoints1 = 8;
    gr.drawPolygon(xPoints1, yPoints1, xPoints1.length);
    gr.setColor(Color.white);
    gr.fillPolygon(xPoints1, yPoints1, numPoints1);
    int[] xPoints2 = {120, 200, 280, 360, 360, 280, 200, 120 };
    int[] yPoints2 = {200, 120, 120, 200, 280, 360, 360, 280 };
    int numPoints2 = 8;
    gr.drawPolygon(xPoints2, yPoints2, xPoints2.length);
    gr.setColor(Color.red);
    gr.fillPolygon(xPoints2, yPoints2, numPoints2);
    gr.setColor(Color.white);
    gr.setFont(new Font("Arial", Font.BOLD, 85)); 
    gr.drawString(Stop, 125, 260);
     
     
    }
    public static void main(String[] args){
    StopSign frame = new StopSign();
    frame.setSize(500,500);
    frame.setVisible(true);
    }
    }

Similar Threads

  1. [SOLVED] How to convert from string to math sign
    By usama8800 in forum Java Theory & Questions
    Replies: 4
    Last Post: June 29th, 2012, 08:37 AM
  2. xmpp single sign on sso
    By richie1985 in forum Java Networking
    Replies: 2
    Last Post: April 15th, 2011, 11:39 AM
  3. Chat application Sign in Problem (maybe : Exception)
    By aymane-tech in forum Java Networking
    Replies: 1
    Last Post: March 23rd, 2011, 11:01 AM
  4. sign XML with WS-Security
    By splendes in forum Java SE APIs
    Replies: 1
    Last Post: October 6th, 2010, 08:49 AM
  5. How do I privately sign my applet?
    By AlphaWhelp in forum Java Theory & Questions
    Replies: 1
    Last Post: December 16th, 2009, 08:42 AM