deeksha
deeksha

Reputation: 23

Java Graphics AWT

When I run the below code, I am unable to see the background color as red. It's showing default one. Is there anything that I have to add to these lines?

import java.awt.*;
import java.awt.Graphics;
import javax.swing.*;
public class gfix extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
       super.paintComponent(g);
       g.setColor(Color.red);
       g.fillRect(80, 100, 150, 75);
    }
    public static void main(String[] args){
       gfix gg=new gfix();
       JFrame frame = new JFrame("RISK");
       frame.setSize(800, 600);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       JPanel panel = new JPanel();
       panel.setLayout(null);
       frame.add(panel);
       JButton button = new JButton("test");
       button.setBounds(100, 100, 150, 150);
       panel.add(button);
       frame.setVisible(true);
    }
}

Upvotes: 1

Views: 83

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168815

enter image description here

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

public class gfix {

    public static void main(String[] args) {
        JFrame frame = new JFrame("RISK");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setBackground(Color.RED);
        frame.add(panel);
        JButton button = new JButton("test");
        // adjust numbers as needed
        button.setMargin(new Insets(20,40,20,40));
        panel.add(button);
        // adjust numbers as needed
        panel.setBorder(new EmptyBorder(10,40,50,200));
        frame.pack();
        frame.setVisible(true);
    }
}

Other tips:

  1. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
  2. Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used.
  3. Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.

Upvotes: 0

ajayg2808
ajayg2808

Reputation: 375

Your are overriding painGraphics() in gfix class so add gfix class object into your frame not Java provided JPanel class object.

       gfix gg=new gfix();
       JFrame frame = new JFrame("RISK");
       frame.setSize(800, 600);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       //JPanel panel = new JPanel();      Not needed
       //panel.setLayout(null);
       frame.add(gg);
       JButton button = new JButton("test");
       button.setBounds(100, 100, 150, 150);
       gg.add(button);
       frame.setVisible(true);

for g.fillRect(80, 100, 150, 75); give proper panel bounds to fill complete panel background. OR use int width = getWidth(); int height = getHeight(); in paintGraphics to get actual height and width.

Upvotes: 1

Related Questions