fishingguy456
fishingguy456

Reputation: 13

How do I set background colour and drawString without cancelling the other out in Java?

I am trying to create a jframe and set a background colour, but also putting in a drawstring. It seems like whenever I use .add, the text appears, but not the background. Without the .add, the background changes, but not the text. `

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JPanel{
    public void paint(Graphics g){
        Graphics2D g2d=(Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        Font font = new Font("Serif", Font.PLAIN, 500);
        g.setFont(font);
        g.setColor(Color.red);
        g.drawString("Hello", 300, 900);
        }public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame jf=new JFrame();
        jf.getContentPane().setBackground(Color.YELLOW);
        jf.getContentPane().add(new Test());
        jf.setSize(1920,1024);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

`

Upvotes: 0

Views: 1074

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285440

Set the background color of the Test JPanel, not the JFrame.

Also:

  • Override paintComponent, not paint
  • Don't forget to call the super's painting method within your override
  • Learn and follow Java naming and code formatting conventions. Your code is hard to read and understand as it is currently written.

For example:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test3 {
    public static void main(String[] args) {
        JFrame jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.getContentPane().add(new MyTest("Hello", Color.RED));
        jf.pack();
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
    }
}

class MyTest extends JPanel {
    private static final int PREF_W = 1600;
    private static final int PREF_H = 900;
    private static final Color BG = Color.YELLOW;
    private static final Font FONT = new Font("Serif", Font.PLAIN, 500);;
    private String text;
    private Color color;

    MyTest(String text, Color color) {
        this.text = text;
        setBackground(BG);
        this.color = color;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setFont(FONT);
        g.setColor(color);
        g.drawString("Hello", 300, 600);

    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

}

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347334

There's a few ways you can do this...

You could..

Make the Test panel the content panel for the frame...

JFrame jf = new JFrame();
jf.setContentPane(new Test());
jf.getContentPane().setBackground(Color.YELLOW);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

You could...

Make the Test panel transparent

JFrame jf = new JFrame();
Test test = new Test();
test.setOpaque(false);
jf.getContentPane().setBackground(Color.YELLOW);
jf.add(test);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

You could...

Just set the background color of the Test panel

JFrame jf = new JFrame();
Test test = new Test();
test.setBackground(Color.YELLOW);
jf.add(test);
jf.setSize(1920, 1024);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Regardless of which you do...

You Should...

  • Call the super method of the paint method
  • Prefer paintComponent of paint - it's just a lot safer
  • Only manipulate the UI from the context of the Event Dispatching Thead

For example...

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Font font = new Font("Serif", Font.PLAIN, 500);
        g.setFont(font);
        g.setColor(Color.red);
        g.drawString("Hello", 300, 900);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame jf = new JFrame();
                jf.setContentPane(new Test());
                jf.getContentPane().setBackground(Color.YELLOW);
                jf.setSize(1920, 1024);
                jf.setVisible(true);
                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });
    }
}

Upvotes: 2

Related Questions