Bilbo Asdfasd
Bilbo Asdfasd

Reputation: 1

Java isn't displaying rectangle?

I'm trying it to display a rectangle at the specified location but it isn't showing up. The background is magenta but the rectangle is not there.

Also: How can I access more colors besides the "Color.(insert very few options here)"

import javax.swing.*;
import java.awt.*;


class Screensaver {
    private final static int FRAME_HEIGHT = 600;
    private final static int FRAME_WIDTH = 600;
    public static void main(String[] args){
        JFrame win;
        Container contentPane;
        Graphics g;


        win = new JFrame();
        win.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        win.setVisible(true);
        contentPane = win.getContentPane();
        contentPane.setBackground(Color.MAGENTA);
        g = contentPane.getGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(80, 350, 400, 250);

    }
}

Upvotes: 0

Views: 2062

Answers (4)

Alya'a Gamal
Alya'a Gamal

Reputation: 5638

Try this :

import javax.swing.*;
import java.awt.*;

class Screensaver {

    private final static int FRAME_HEIGHT = 600;
    private final static int FRAME_WIDTH = 600;

    public static void main(String[] args) {
        JFrame win;
        Container contentPane;
        win = new JFrame();
        win.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        win.setVisible(true);
        Component comp = new Component();
        contentPane = (Container) win.getContentPane().add(comp);


    }
    }

    class Component extends JComponent {

    @Override
   public void paintComponent(Graphics g) {
    super.paintComponent(g);
        g.setColor(Color.magenta);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.BLACK);
        g.fillRect(80, 350, 400, 250);
    }
}

and about the color , you van create new Color and set the RED,GREEN,BLUE as you want , Try this :

g.setColor(new Color(red, green, blue));

Upvotes: 0

Kevin Bigler
Kevin Bigler

Reputation: 266

If you're going to draw stuff create a class that inherits from a Swing container, JComponent, JPanel, etc. and override the paint(Graphics g) method. If you see the magenta background then contentPane must have been added. What's probably happening is its painting the magenta background over your rectangle, but that's just a guess. Try this...

public class ContentPane extends JComponent {
     public ContentPane() {

     }

     @Override
     public void paint(Graphics g) {
          g.setColor(Color.MAGENTA);
          g.fillRect(0, 0, getWidth(), getHeight());

          g.setColor(Color.BLACK);
          g.fillRect(80, 350, 400, 250);

     }
}

Then in your main class instantiate an object of the ContentPane class and add it to your JFrame. The call to repaint() is probably unnecessary but that will make sure it gets painted. You can also try messing around with the paintComponent(Graphics g) method, there is a difference between the two, I believe its the order they're called from the update method but I'm probably wrong about that, however that should solve your problem.

As for colors, consult the API. You can pass RGB values into the Color constructor to create all sorts of colors. Color color = new Color(int red, int green, int blue). I think that is the easiest way to create custom colors but like I said its all in the API. Hope this helps.

Upvotes: 0

ug_
ug_

Reputation: 11440

The rectangle is drawn once, however every time the JFrames repaint() method is called it erases it and draws the basic Components. To add custom drawing in JFrames you have to override the paint method. Here I improved your code slightly to get you started down that path. As you can see you want to draw the box in the Paint method. I made a Container element that does your drawing and removed the background color, adding it to the paint method as well.

try this

import javax.swing.*;
import java.awt.*;

public class JavaApplication10 {
    private final static int FRAME_HEIGHT = 600;
    private final static int FRAME_WIDTH = 600;
    public static void main(String[] args){
        JFrame win = new JFrame();
        win.setContentPane(new MyBoxContainer());
        win.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        win.setVisible(true);
    }

    private static class MyBoxContainer extends Container {
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.MAGENTA);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.BLACK);
            g.fillRect(80, 350, 400, 250);
        }
    }
}

Upvotes: -1

edwga
edwga

Reputation: 190

You shouldn't be painting in main(); it would be better to extend JPanel, change paintComponent(), and add the panel to the JFrame.

public class PaintPanel extends JPanel {

    public PaintPanel() {
        setBackground(Color.MAGENTA);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g); // This paints the background

        g.setColor(Color.BLACK);
        g.fillRect(80, 350, 400, 250);
    }
}

And in main():

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.add(new PaintPanel());
    frame.setVisible(true);
}

If you want to make your own Colors, you can use the new Color(int red, int green, int blue) constructor.

Upvotes: 3

Related Questions