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

Thread: Need help with repaint();

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with repaint();

    Hi guys and gals, i've been trying to write this code but i keep getting something wrong and can't figure it out.
    I appreciate any help/advice you can give me, thanks!
    public class Saxophone extends JFrame implements KeyListener{
    char c;
    boolean n, b, m, o, k, h = false;
    private Graphics g;
    public void paint(Graphics g)
    {
    super.paint(g);
    if (c == 'n'){
    n = true;}
    if (c == 'm'){
    m = true;}
    if (c == 'b'){
    b = true;}
    if (c == 'o'){
    o = true;}
    if (c == 'k'){
    k = true;}
    if (c == 'h'){
    h = true;}
    }

    --- Update ---

    Here's the rest of the code

    @Override
    public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void keyTyped(KeyEvent e) {
    c = e.getKeyChar();
    if (n == true){
    /*Neck and Bell*/
    g.fillOval(406, 410, 80, 60);
    g.fillRoundRect(271, 260, 120, 30, 10, 10);
    }
    if (m == true){
    /*Mouthpiece*/
    g.setColor(Color.BLACK);
    g.fillOval(217, 259, 70, 32);
    g.fillRect(238, 260, 50, 30);
    }
    if (b == true){
    /*Body of Saxophone*/
    g.setColor(Color.ORANGE);
    g.fillArc(350, 560, 120, 120, 180, 180);
    g.fillRoundRect(351, 260, 40, 380, 10, 10);
    g.fillRoundRect(415, 423, 55, 200, 10, 10);
    }
    if (o == true){
    /*Octave Key*/
    g.setColor(Color.ORANGE);
    g.drawArc(294, 249, 95, 30, 180, -180);
    g.drawArc(294, 248, 95, 30, 180, -180);
    g.drawArc(294, 247, 95, 30, 180, -180);
    g.setColor(Color.BLACK);
    g.drawArc(294, 250, 95, 30, 180, -180);
    g.drawArc(294, 246, 95, 30, 180, -180);
    }
    if (k == true){
    /*Keys*/
    g.setColor(Color.BLACK);
    g.fillOval(376, 350, 15, 15);
    g.fillOval(376, 366, 15, 15);
    g.fillOval(376, 382, 15, 15);
    g.fillOval(360, 500, 15, 15);
    g.fillOval(360, 516, 15, 15);
    g.fillOval(360, 532, 15, 15);
    }
    if (h == true){
    /*Hole In Bell and Outline of Bell*/
    g.setColor(Color.BLACK);
    g.fillOval(416, 417, 60, 40);
    g.drawArc(410, 431, 70, 37, 186, 150);
    }
    }
    }


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default This program won't show the graphics I want it to

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame
    public class Sax extends JFrame implements KeyListener{
    char c;
    boolean n, b, m, o, k, h = false;
    private Graphics g;

    public void paint(Graphics g)
    {
    super.paint(g);

    repaint();
    }
    @Override
    public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub

    }
    @Override
    public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

    }
    @Override
    public void keyTyped(KeyEvent e) {
    c = e.getKeyChar();
    if (n == true){
    /*Neck and Bell*/
    g.fillOval(406, 410, 80, 60);
    g.fillRoundRect(271, 260, 120, 30, 10, 10);
    repaint();
    }
    if (c == 'n'){
    n = true;}
    if (c == 'm'){
    m = true;}
    if (c == 'b'){
    b = true;}
    if (c == 'o'){
    o = true;}
    if (c == 'k'){
    k = true;}
    if (c == 'h'){
    h = true;}

    if (m == true){
    /*Mouthpiece*/
    g.setColor(Color.BLACK);
    g.fillOval(217, 259, 70, 32);
    g.fillRect(238, 260, 50, 30);
    repaint();
    }
    if (b == true){
    /*Body of Sax*/
    g.setColor(Color.ORANGE);
    g.fillArc(350, 560, 120, 120, 180, 180);
    g.fillRoundRect(351, 260, 40, 380, 10, 10);
    g.fillRoundRect(415, 423, 55, 200, 10, 10);
    repaint();
    }
    if (o == true){
    /*Octave Key*/
    g.setColor(Color.ORANGE);
    g.drawArc(294, 249, 95, 30, 180, -180);
    g.drawArc(294, 248, 95, 30, 180, -180);
    g.drawArc(294, 247, 95, 30, 180, -180);
    g.setColor(Color.BLACK);
    g.drawArc(294, 250, 95, 30, 180, -180);
    g.drawArc(294, 246, 95, 30, 180, -180);
    repaint();
    }
    if (k == true){
    /*Keys*/
    g.setColor(Color.BLACK);
    g.fillOval(376, 350, 15, 15);
    g.fillOval(376, 366, 15, 15);
    g.fillOval(376, 382, 15, 15);
    g.fillOval(360, 500, 15, 15);
    g.fillOval(360, 516, 15, 15);
    g.fillOval(360, 532, 15, 15);
    repaint();
    }
    if (h == true){
    /*Hole In Bell and Outline of Bell*/
    g.setColor(Color.BLACK);
    g.fillOval(416, 417, 60, 40);
    g.drawArc(410, 431, 70, 37, 186, 150);
    repaint();
    }
    }

    }

  3. #3
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: This program won't show the graphics I want it to

    How are you passing the Graphics object? Why are you not using a JPanel? Creating the Demo Application (Step 1) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Performing Custom Painting) has a good demo.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need help with repaint();

    Welcome saxy
    Please do not double post your question. Threads merged.
    Please see the Announcements page for the use of code tags and other useful forum tips.
    You should not call repaint() from within the paint method

    i keep getting something wrong
    What do you get wrong?
    What does the program do now? What should it do instead? Where do things seem to go unexpected?
    Even if someone had the time to compile and run every code sample posted, how are we to guess what it should do if it does not do the right thing as it is?

Similar Threads

  1. repaint in a JButton while loop.
    By Sevv in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2012, 09:44 AM
  2. repaint() event does not work.
    By SagiIs in forum What's Wrong With My Code?
    Replies: 33
    Last Post: December 25th, 2011, 05:12 PM
  3. Help with repaint
    By AraHabs in forum AWT / Java Swing
    Replies: 5
    Last Post: November 5th, 2011, 04:40 PM
  4. Repaint,
    By Time in forum AWT / Java Swing
    Replies: 3
    Last Post: May 21st, 2010, 11:23 PM
  5. Repaint doesn't repaint?
    By PotataChipz in forum AWT / Java Swing
    Replies: 6
    Last Post: January 18th, 2010, 09:56 PM