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

Thread: Hi, Can Someone please confirm if what am trying to do with Graphics is possible...if it is, then how? Thanks

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    My Mood
    Cheerful
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Hi, Can Someone please confirm if what am trying to do with Graphics is possible...if it is, then how? Thanks

    Hello,
    Am trying to draw a square, but i want it to have more than one color (maybe the top half is blue and the bottom half is red)
    Currently i can only manage to do this by drawing two different squares very close to each other, so they look like one square.
    Can Someone please tell me how to draw one square, and it will have more than one color.
    If this cannot be achieved using Graphics, then please tell me how so i can go and learn.
    Thanks in Advance


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    621
    Thanks
    0
    Thanked 112 Times in 97 Posts

    Default Re: Hi, Can Someone please confirm if what am trying to do with Graphics is possible...if it is, then how? Thanks

    What graphics API are you using?

    If you are talking about the way you draw things with Swing then I think it is impossible. Just draw 2 rectangles next to each other.

  3. The Following User Says Thank You to Cornix For This Useful Post:

    crownjoshua (July 9th, 2014)

  4. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    My Mood
    Cheerful
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Hi, Can Someone please confirm if what am trying to do with Graphics is possible...if it is, then how? Thanks

    I am advanced beginner or begining itermediate...so i only understand APIs from android development.
    I am using the graphics class, here is my code

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
     
    public class Grab extends JPanel{
     
    public void paintComponent(Graphics g){
     
    	Graphics b = null;
     
    	super.paintComponent(g);
     
    	this.setBackground(Color.GREEN);
     
     
     
    	g.setColor(Color.BLUE);
    	g.fillRect(25, 25, 25, 25);
     
    	g.setColor(Color.RED);
    	g.fillRect(25,50, 25, 25);
     
    }
    }

    if it is not possible then i will continue using two square as you suggested, thanks

  5. #4
    Junior Member
    Join Date
    Nov 2012
    Location
    Sandwich
    Posts
    14
    My Mood
    Yeehaw
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Hi, Can Someone please confirm if what am trying to do with Graphics is possible...if it is, then how? Thanks

    Essential if you really think about it. one square is a bunch of many squares called pixels. but if you wanted you could have a loop that creates all the lines and half way through it changes color
    It is not enough to know programming, but to think like the program

  6. The Following User Says Thank You to Daryn For This Useful Post:

    crownjoshua (July 9th, 2014)

  7. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,299
    My Mood
    Mellow
    Thanks
    193
    Thanked 669 Times in 656 Posts

    Default Re: Hi, Can Someone please confirm if what am trying to do with Graphics is possible...if it is, then how? Thanks

    You can draw a shape pixel by pixel if you want, changing the color of each pixel as you go. Your goal as a programmer should be to do what needs to be done to achieve the desired result as simply as possible.

    Combining two differently colored rectangles to make a two-colored square is the simplest way to do what you've described, at least with the graphics packages provided with Java SE. There may be 3rd-party packages that give you more options, but I haven't seen one that does what you've described.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    crownjoshua (July 9th, 2014)

  9. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    My Mood
    Cheerful
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Hi, Can Someone please confirm if what am trying to do with Graphics is possible...if it is, then how? Thanks

    Thanks a lot, this forum is so helpful, am glad i joined.
    And i will try to use the pixel approach
    Thanks

  10. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Hi, Can Someone please confirm if what am trying to do with Graphics is possible...if it is, then how? Thanks

    Not sure why you need an alternative to drawing two rectangles, if that's the result you wish to achieve. This being said, have a look at the GradientPaint class
    GradientPaint (Java Platform SE 7 )
    If you set the x/y parameters to the appropriate value, you can achieve a multi-colored Shape with a hard or soft interface.

  11. The Following User Says Thank You to copeg For This Useful Post:

    crownjoshua (July 9th, 2014)

  12. #8
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    My Mood
    Cheerful
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Hi, Can Someone please confirm if what am trying to do with Graphics is possible...if it is, then how? Thanks

    Thank You very much, i think will solve my problem.
    You have been very helpful

Similar Threads

  1. Help me with the confirm dialog and counter
    By howarddermaus1111 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: March 30th, 2014, 06:58 AM
  2. Graphics
    By TheorizedGaming in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 16th, 2013, 08:41 PM
  3. how to use graphics g
    By steel55677 in forum AWT / Java Swing
    Replies: 11
    Last Post: November 21st, 2011, 05:35 PM
  4. Graphics class NullPointerException Initialize Graphics Class??
    By bglueck in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 13th, 2011, 11:13 PM
  5. Help about Graphics
    By mamech in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 9th, 2010, 03:20 PM

Tags for this Thread