Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Java Graphics (make a 2x2 pxl rectangle out of one pxl)

  1. #1
    cups is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Java Graphics (make a 2x2 pxl rectangle out of one pxl)

    Hey!!

    What I have to do:
    Given is a picture. I have to run through all the pixels and for each pixel find out it's RGB value and draw a 2x2 pixel-rectangle in that color.
    now I know how to find out that color, and I can draw a rectangle in it, but:
    I've got the color, I make a new frame. This is the code for the frame:

    Java Code:
    public OriFrame(String title) {
            super(title);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JButton exit = new JButton("exit");
            JPanel panel = new JPanel();
            add(panel);
            setLayout(new FlowLayout());
            add(exit);
            pack();
            setVisible(true);
        }
    
        public void paint(Graphics g, int i, int j, Color c) {
            g.setColor(c);
            g.fillRect(i, j, 2, 2);
        }
    }
    it has got that method, b/c I obviously want to draw on it. Now I tried to call that method in 2 different ways:
    Java Code:
    Graphics g = null;
    frame.paint(g, i, j, color);
    but I'm getting a null pointer exception.
    and I tried to leave the Graphics argument here (and deleted it in the head as well of course) and
    then did the same thing in the method, but there's null pointer exception as well.
    But if I don't set it equals null, it is not initialized and that isn't working either.
    I cannot create a new Graphics Object, b/c it is an abstract method. What do I do then?
    I don't know anything else to try anymore!
    Google didn't help either, b/c when someone shows how to draw a rectangle, they always put the paint method in the constructor and I cannot set the coordinates that way.
    Any ideas?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,301
    Rep Power
    20

    Default Re: Java Graphics (make a 2x2 pxl rectangle out of one pxl)

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    cups is offline Member
    Join Date
    Dec 2011
    Posts
    14
    Rep Power
    0

    Default Re: Java Graphics (make a 2x2 pxl rectangle out of one pxl)

    I'm sorry, but that doesn't help at all. I went through the whole tutorial,
    but I don't see how it's gonna help me. They still use the paint() method the
    way I described before and the way I can not use it or I don't understand how.
    The only thing they do with coordinates is repaint() a component, but I don't understand
    how that's gonna help me either.
    Any ideas on my problem someo

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,301
    Rep Power
    20

    Default Re: Java Graphics (make a 2x2 pxl rectangle out of one pxl)

    Quote Originally Posted by cups View Post
    I'm sorry, but that doesn't help at all. I went through the whole tutorial,
    but I don't see how it's gonna help me. They still use the paint() method
    They don't.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Java Graphics (make a 2x2 pxl rectangle out of one pxl)

    I suggest that you draw within a BufferedImage, and then display the BufferedImage in your GUI, either in an ImageIcon held by a JLabel or painted in the background of a JComponent in its paintComponent(...) method, not its paint method.

Similar Threads

  1. make a rectangle over JLabel
    By kumar.dinesh7 in forum AWT / Swing
    Replies: 6
    Last Post: 05-26-2012, 06:03 PM
  2. Replies: 0
    Last Post: 04-11-2011, 05:28 PM
  3. How can I make the graphics visible?
    By AcousticBruce in forum New To Java
    Replies: 9
    Last Post: 03-01-2011, 03:53 AM
  4. Replies: 3
    Last Post: 10-20-2010, 06:24 AM
  5. To make slider resize rectangle and to move pictures
    By mneskovic in forum New To Java
    Replies: 11
    Last Post: 05-21-2010, 11:07 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
  •