Results 1 to 6 of 6

Thread: Guidance

  1. #1
    gizdev is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default Guidance

    This is hw, so im only looking to be guided in the right direction i have to find and fix the bug. it is supposed to display a cross.
    Java Code:
    // Question 2-13-a
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     *  This program displays a red cross on a white
     *  background.
     */
    
    public class RedCross extends JPanel
    {
      public void paintComponent(Graphics g)
      {
        super.paintComponent(g);  // Call JPanel's paintComponent method
                                  //   to paint the background
        int xCenter = getWidth() / 2;
        int yCenter = getHeight() / 2;
        g.setColor(Color.RED);
        g.fillRect(xCenter, yCenter, 10, 50);
        g.fillRect(xCenter, yCenter, 100, 10);
        
      }
    
      public static void main(String[] args)
      {
        JFrame window = new JFrame("Red Cross");
        window.setBounds(300, 300, 200, 200);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        RedCross panel = new RedCross();
        panel.setBackground(Color.WHITE);
        Container c = window.getContentPane();
        c.add(panel);
        window.setVisible(true);
      }
    }

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Java Code:
    g.fillRect(xCenter, yCenter, 10, 50);
        g.fillRect(xCenter, yCenter, 100, 10);
    This won't display a cross because you start them at the same place (xCenter, yCenter). You should make vars with the width/height/etc of the cross so that you can calculate what x and y to use, and what w and h, instead of just random numbers (10, 50; 100, 10)

    Got my point?
    I die a little on the inside...
    Every time I get shot.

  3. #3
    gizdev is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    I know what you mean about starting at the same point. But I do not even know where to start about fixing it

    Thanks

  4. #4
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default I'm not a fan of xcenter and ycenter, but you could try

    g.fillRect(xCenter, yCenter-45, 10, 100);
    g.fillRect(xCenter-45, yCenter, 100, 10);

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    For example, yes.

    Java Code:
    final int length = 100; // The length of both rects
    final int width = 10;    // The width of the rects
    
    g.fillRect(xCenter-length/2, yCenter-width/2, length, width);
    g.fillRect(xCenter-width/2, yCenter-length/2, width, length);
    It could be something like that. You understand that, I suppose?
    I die a little on the inside...
    Every time I get shot.

  6. #6
    gizdev is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

Posting Permissions

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