Background is not painted - java

I want to create a simple game menue for the game pong. In the background there is a ball that bounces of the edges just to look nice. Now I want to add Buttons to this menue screen. But then the Background isn't painted anymore. So i want to draw a live Background but the buttons should still stay in the front. This live Background is implemented with a loop. In the following are the GUI class and the DrawStartMenueClass.
I heard about Layers but everybody uses them with some kind of tool in NetBeans and I got eclipse so I don't have that opportunity. I guess you can put the buttons and the background in different containers but then again I am not sure how that would. Every help is welcome.
public class GUI {
public GUI() {
//Frame erzeugen
Var.frame = new JFrame("Pong");
Var.frame.setSize(Var.dimension);
Var.frame.setVisible(true);
Var.frame.setResizable(true);
Var.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Var.frame.setLocationRelativeTo(null);
//Komponenten erzeugen
Var.start = new JButton("Start");
Var.optionen = new JButton("Optionen");
Var.modus = new JButton("Modus");
Var.skins = new JButton("Skins");
Var.beenden = new JButton("Beenden");
//Komponenten anpassen
Var.start.setBounds((Var.screenWidth / 2) - (Var.buttonWidth / 2), 400, Var.buttonWidth, 50);
Var.optionen.setBounds((Var.screenWidth / 2) - (Var.buttonWidth / 2), 500, Var.buttonWidth, 50);
Var.modus.setBounds((Var.screenWidth / 2) - (Var.buttonWidth / 2), 600, Var.buttonWidth, 50);
Var.skins.setBounds((Var.screenWidth / 2) - (Var.buttonWidth / 2), 700, Var.buttonWidth, 50);
Var.beenden.setBounds((Var.screenWidth / 2) - (Var.buttonWidth / 2), 800, Var.buttonWidth, 50);
//Komponenten hinzufügen
Var.frame.getContentPane().add(new DrawStartMenue());
Var.frame.add(Var.start);
Var.frame.add(Var.optionen);
Var.frame.add(Var.modus);
Var.frame.add(Var.skins);
Var.frame.add(Var.beenden);
}
}
package main;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import javax.swing.JPanel;
public class DrawStartMenue extends JPanel{
private static final long serialVersionUID = 1L;
private GridLayout experimentLayout = new GridLayout(6,1);
public Graphics2D g2d;
public void paint(Graphics g) {
//Objekt g zum Zeichnen erzeugen
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//Hintergrund
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, Var.screenWidth, Var.screenHeight);
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(8));
g2d.drawRect(Var.whiteRectX1, Var.whiteRectY1 , Var.whiteRectWidth, Var.whiteRectHeight);
//Ball im Hintergrund
for(int i = 0; i < Var.ballList.size(); i++) {
g2d.fillOval(Var.ballList.get(i).ballX, Var.ballList.get(i).ballY,
Var.ballList.get(i).ballWidth, Var.ballList.get(i).ballHeight);
}
//Schriftzug
drawCenteredString(g, "PONG", Var.rect, new Font("NO CONTINUE", Font.PLAIN, 230));
repaint();
}

Var.frame.getContentPane().add(new DrawStartMenue());
Var.frame.add(Var.start);
Var.frame.add(Var.optionen);
Var.frame.add(Var.modus);
Var.frame.add(Var.skins);
Var.frame.add(Var.beenden);
The default layout for a JFrame is the BorderLayout. When you add a component to the frame without specifying a constraint the component is added to the CENTER. Only one component can be added to the CENTER. So only the last one added is visible.
If you want the button on the background then you need to add the buttons to background panel, not the frame:
JPanel background = new DrawStartMenue();
Var.frame.add(background, BorderLayout.CENTER);
background.add(Var.start);
background.add(Var.optionen);
background.add(Var.modus);
background.add(Var.skins);
background .add(Var.beenden);
Other problems with the code:
custom painting is done by overriding paintComopnent(...) not paint(...).
Never invoke repaint() in a painting method. Swing will determine when the component should be repainted.
components should be added to the frame BEFORE the frame is made visible.
don't attempt to setBounds(...) components. Swing was designed to be used with layout managers. Use the appropriate layout manager for your desired layout.
Edit:
So you know how to:
add components to a panel
do custom painting on a panel.
So to combine both concepts you modify the SwingPaintDemo2 code as follow:
//f.add(new MyPanel());
JPanel panel = new MyPanel();
panel.add( new JButton("Testing") );
f.add(panel);
So know that you know the basics, you use the knowledge on your real code.
And don't forget, you should NEVER invoke repaint in a painting method. I already showed you how to use a Swing Timer for the animation.
Next if you don't like the location of the buttons, then you use a different layout manager on the MyPanel class. If you need help with layout manager, then the Swing tutorial also has a section on layout manager with working example for you to download and test.

Here is the MRE. I finally got it to work. However it doesn't work in my project.
package main;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI {
static JFrame frame;
static JPanel buttons;
static JPanel background;
static JButton test1;
static JButton test2;
public static void main(String[] args) {
frame = new JFrame();
frame.setResizable(false);
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Initzialize Components
buttons = new JPanel();
buttons.setBackground(Color.BLACK);
background = new JPanel();
background.setBackground(Color.BLACK);
test1 = new JButton("Test1");
test2 = new JButton("Test2");
//Adding the Buttons
buttons.add(test1);
buttons.add(test2);
//Adding panels to JFrame
frame.add(background);
//frame.add(buttons);
background.add(buttons);
frame.setVisible(true);
}
}

Related

Why does the alpha in this timer draw on top of itself in this Java Swing Panel?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(new Dimension(100, 100));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TestPanel panel = new TestPanel();
panel.setPreferredSize(new Dimension(50,50));
frame.add(panel);
frame.setVisible(true);
}
static class TestPanel extends JPanel implements ActionListener{
private static final long serialVersionUID = 8518959671689548069L;
public TestPanel() {
super();
Timer t = new Timer(1000, this);
t.setRepeats(true);
t.start();
}
int opacity = 10;
#Override
public void actionPerformed(ActionEvent e) {
if(opacity >= 250) {
opacity = 0;
}
else {
this.setBackground(new Color(255, 212, 100, opacity));
this.repaint();
opacity+=10;
System.out.println("opacity is " + opacity);
}
}
}
}
The rate the alpha changes is faster than it should be. After it reaches a certain point, the opacity drops, while the the opacity printed in the console is less than 250. Resizing the window "resets" it, making the alpha correct.
How do I make it actually draw the alpha correctly?
this.setBackground(new Color(255, 212, 100, opacity));
Swing does not support semi transparent backgrounds.
Swing expects a component to be either:
opaque - which implies the component will repaint the entire background with an opaque color first before doing custom painting, or
fully transparent - in which case Swing will first paint the background of the first opaque parent component before doing custom painting.
The setOpaque(...) method is used to control the opaque property of a component.
In either case this makes sure any painting artifacts are removed and custom painting can be done properly.
If you want to use tranparency, then you need to do custom painting yourself to make sure the background is cleared.
The custom painting for the panel would be:
JPanel panel = new JPanel()
{
protected void paintComponent(Graphics g)
{
g.setColor( getBackground() );
g.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
panel.setOpaque(false); // background of parent will be painted first
Similar code would be required for every component that uses transparency.
Or, you can check out Background With Transparency for custom class that can be used on any component that will do the above work for you.

JPanel Graphics not drawing anything (Java) [duplicate]

This question already has an answer here:
Can't draw to JPanel with getGraphics
(1 answer)
Closed 4 years ago.
I'm having trouble getting the graphics of my JPanel to work. It refuses to draw anything, regardless of anything I've tried and anything I can find on the internet.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.*;
import java.io.*;
public class Mandelbrot{
public static void main(String[] args){
JFrame win=new JFrame();
JPanel dis=new JPanel();
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setResizable(false);
win.setVisible(true);
win.add(dis);
dis.setPreferredSize(new Dimension(1000,500));
win.pack();
Graphics g=dis.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, 100, 100);
}
}
Posting as an answer because I ran out of comment room:
Note:
If you need to be constantly changing things, then a JPanel is probably not your best option. I recommend you rethink what you are trying to do because you should probably use a Canvas or paint to a bunch of different labels/glass panes and overlay them however you want, this will allow you to have moving components/animations in a foreground item, and make different changes to the background item.
Alternatively, you can make the JPanel draw a buffered image, or you can store a list of items to paint, and you can paint them each time. For the buffered image method you can directly edit and draw to the buffered image every time you need to make a change.
Below is an example of how to use the buffered image method.
First create a custom JPanel in a new class:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class DrawPanel extends JPanel{
public BufferedImage canvas = new BufferedImage(panelWidth, panelHeight, BufferedImage.TYPE_INT_ARGB);
#Override
public void paintComponent(Graphics g){
//Draw the canvas
g.drawImage(canvas, 0, 0, this);
}
}
Now in your main method you can replace JPanel dis=new JPanel() with this:
DrawPanel dis = new DrawPanel();
Graphics g=dis.canvas.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, 100, 100);
Note how I use dis.canvas to get the graphics of the bufferedImage instead of the graphics of the JPanel.
It's as simple as that.
As per Andrews comment. You should consider extending a JLabel instead of a JPanel, it is much more lightweight, and easier to update using label.repaint();.
public static void main(String... args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel() {
#Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.fillRect(0, 0, 100, 100);
}
};
panel.setPreferredSize(new Dimension(640, 480));
frame.add(panel);
frame.setVisible(true);
frame.pack();
}
Just an example - you should create a new Class subclassing JPanel, see Painting in AWT and Swing.

Need to set frame.setResizable(false) to repaint() [duplicate]

This question already has answers here:
JFrame not presenting any Components
(4 answers)
Closed 5 years ago.
I am trying to buff my java skills (been about 10 years since I coded). Currently I am just trying to make a basic program that will have balls bouncing off the edges of the JFrame. However, as a starter in this program I tried drawing a line and box on the JPanel.
The issue I am finding is I have to call frame.setResizable(false) in order or the screen to paint my box and line. It will paint them if I resize the JFrame after it comes up. However, I would like it to paint as soon as the JFrame opens.
Putting in:
frame.setResizable(false);
frame.setResizable(true);
seems redundant. Is there a cleaner way to do this so it paints when the JFrame opens?
Below is my code if this helps:
MAIN CLASS
package bbs;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class BouncingBalls {
public static void main(String[] args) {
//Create the basic frame, set its size, and tell it to be visible
JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setVisible(true);
//Get a icon for the Program
ImageIcon logoicon = new ImageIcon("ball.jpg");
Image logo = logoicon.getImage();
frame.setIconImage(logo);
frame.setResizable(false);
frame.setResizable(true);
//find the center of the screen and where the frame should go
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = frame.getSize().width;
int h = frame.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
//Move the window
frame.setLocation(x, y);
//Tell the program to stop when the X button is selected
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Draw object = new Draw();
frame.add(object);
object.drawing();
}
}
PAINTING CLASS
package bbs;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Draw extends JPanel {
/**
* This is added to handle the serialization warning and is of the type Long to accommodate the warning
*/
private static final long serialVersionUID = 1L;
public void drawing(){
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(10, 20, 300, 200);
g.setColor(Color.BLUE);
g.fillRect(300, 200, 150, 200);
}
}
frame.setVisible(true);
This should be the last statement executed AFTER all components have been added to the frame.
Then all the components will paint normally.

Java JTabbedPane Inset Color

I was wondering how you would get the color of the inset of a JTabbedPane. I cannot seem to get this color. Every attempt I make I get 236,236,236 which is the outside frame color, where the inside frame color is about 10 darker, 227,227,227 (using the built in apple color meter).
I am setting the look and feel using UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
You can see this in an image that I found on the internet. http://pagesofinterest.net/wordpress/wp-content/uploads/2009/06/Quaqua-Maven-Netbeans.jpg Where the words "Panel's Title" is the area that I am getting the lighter color that is not useful to me. Inside the round corners is the darker color I am trying to obtain. I tried getting the color of the content pane to no avail.
Thanks for all your help!
**EDIT:**Added code! As you see, I am trying to get the color of the area inside the rounded corners(if your on a mac) not the color of the frame or the tabs that say "1" "2". I have attached a photo and I am trying to get the background color of the portion that says "Here" Thanks!
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
public class main {
JFrame frame;
Container c1 = new Container();
Container c2 = new Container();
JTabbedPane top = new JTabbedPane();
static main GUI;
public void createGUI(){
frame = new JFrame();
Container c = frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
top = new JTabbedPane(JTabbedPane.TOP);
top.setFocusTraversalKeysEnabled(false);
top.setFocusable(false);
top.addTab("1", c1);
top.addTab("2", c2);
frame.setSize(315,450);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setResizable(true);
c.add(top);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {}
GUI = new main();
GUI.createGUI();
}
}
EDIT: camickr, Here is a screenshot of the UIManager Defaults. Unfortunately none of there colors in the are the correct color that the inset is.
You might be able to use UIMangaer Defaults to find the color.
You can override paintComponent() to use a GradientPaint in the tab's background, as shown below. A few notes,
Let the content adopt the preferred size of it contents, as shown here.
Construct the GUI in the event dispatch thread.
Use conventional Java names.
Addendum: the elements are not always in the same spot, so I do not know what place to get the color.
It sounds like you want to match a color used internally by the TabbedPaneUI delegate. One approach would be as follows:
Render a BufferedImage of the component, as shown here.
Determine the coordinates of a Point in top relative to the top of c1.
Point p = SwingUtilities.convertPoint(c1, 0, -1, top);
Obtain the color using getRGB(), as shown here; use Zoom to verify the result.
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
/** #see https://stackoverflow.com/a/16625260/230513 */
public class Main {
JFrame frame;
Container c1 = new GradientPanel();
Container c2 = new GradientPanel();
JTabbedPane top = new JTabbedPane();
private static class GradientPanel extends JPanel {
public GradientPanel() {
this.add(new JLabel("Here"));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint p = new GradientPaint(0, 0, Color.white,
getWidth(), getHeight(), Color.gray);
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
public void createGUI() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
top = new JTabbedPane(JTabbedPane.TOP);
top.addTab("1", c1);
top.addTab("2", c2);
frame.add(top);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Main().createGUI();
}
});
}
}

Custom JComponent only Displays on JFrame not JPanel

Can somebody please help me understand why my custom JComponent 'Bar', only displays when added directly to the JFrame, and not when added to a JPanel (which is then added to the JFrame)?
Thanks!
package main;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Board {
public void start(){
JFrame frame = new JFrame();
JButton button1 = new JButton("Button 1");
Bar statusbar = new Bar();
JLabel status = new JLabel("Status: ");
JPanel topPanel = new JPanel();
topPanel.add(status);
topPanel.add(statusbar);
JPanel mainPanel = new JPanel();
mainPanel.add(button1);
mainPanel.add(statusbar);
frame.getContentPane().add(BorderLayout.NORTH, topPanel);
frame.getContentPane().add(BorderLayout.SOUTH, mainPanel);
frame.getContentPane().add(BorderLayout.CENTER, statusbar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,100);
frame.setVisible(true);
}
}
Here is my Bar Class...
package main;
import java.awt.Graphics;
import javax.swing.JComponent;
public class Bar extends JComponent{
public void paint(Graphics g){
g.fillRect(0, 0, 100, 10);
}
}
You're adding statusbar to several different places in the component tree, Swing doesn't deal with that well (or at all).
Create a separate Bar instances each time you use it, if you want their display to be synchronized, they should share the same model.
Edit
Ah, on a second glance, the problem here is that you never set a size (or preferred size) for the Bar components, so they get squished to 0 by the layout manager.
Try:
public static class Bar extends JComponent {
private Bar() {
setPreferredSize(new Dimension(25, 5));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, 100, 10);
}
}
You should also add a frame.pack() before display.
(the multiple references to the same component thing is still true, too)
The dimension of the custom component is (0, 0).
When added to a container with a BorderLayout layout manager, it will expand to fill the available space, and therefore become visible.
When added to a container with a FlowLayout layout manager, it will not expand and will instead remain at its preferred size (i.e. (0, 0)). And therefore, will not become visible, albeit it is still there.
This explains why the custom component is only displayed when added directly to the JFrame, since it uses a BorderLayout layout manager, whereas a JPanel uses a FlowLayout layout manager.

Categories