Jak zrobić tło

0

Witam, mam pewien problem, napisałem bardzo prosty program, który ma w wyświetlić 2 figury, chciałbym dodać tło, lecz nie działa.
Proszę o pomoc.

public class MyComponent extends JComponent {
	
	public void paintComponent(Graphics g)
	{
		g.drawString("Witraj", 300, 75);
		Graphics2D g2 = (Graphics2D) g;
		
		Rectangle2D rec = new Rectangle2D.Double(150,100 , 350, 350);
		g2.setPaint(Color.BLUE);
		g2.fill(rec);
		
		Ellipse2D ellipse = new Ellipse2D.Double();
		ellipse.setFrame(rec);
		g2.setPaint(Color.RED);
		g2.fill(ellipse);
		
		g2.setPaint(new Color(66, 125, 17));
		g2.drawString("Uwaga", 300, 500);
		
		MyComponent p = new MyComponent();
		p.setForeground(Color.WHITE);
		p.setBackground(Color.BLACK);
	}
} 
0
  1. Absurdem jest tworzenie nowych obiektów w metodzie paintComponent.
  2. Co ma być tłem? Z zamieszczonego kodu to nie wynika.
  3. Co to jest MyComponent?
0

Tłem, ma być kolor czarny, właśnie nie wiem jak to napisać. A MyComponent to nazwa taj klasy

1
public class MyComponent extends JPanel {
    public MyComponent()
    {
         setPreferredSize(new Dimension(szerokość, wysokość));
         setBackground(Color.BLACK);
    }
 
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g); 
        g.drawString("Witaj", 300, 75);
        Graphics2D g2 = (Graphics2D) g;
 
        Rectangle2D rec = new Rectangle2D.Double(150,100 , 350, 350);
        g2.setPaint(Color.BLUE);
        g2.fill(rec);
 
        Ellipse2D ellipse = new Ellipse2D.Double();
        ellipse.setFrame(rec);
        g2.setPaint(Color.RED);
        g2.fill(ellipse);
 
        g2.setPaint(new Color(66, 125, 17));
        g2.drawString("Uwaga", 300, 500);
 
    }
}
0

Dzięki :D

1 użytkowników online, w tym zalogowanych: 0, gości: 1