Jpanel is not repainting properly - java

I'm making a pacman game using java swing.
in my code i use 2 jpanels in the component panel
the first is for the map and the second is for the pacman.
now i am trying to move pacman to other cell when a button is clicked.it is moved but the old picture is not deleted.image before clicking, image after clicking
as you can see the new pacman appears but the old didn't disappear. and some trash also appeared.
this is the code of creating the jpanel for the pacman
JLabel pacman = new JLabel("", new ImageIcon("pacman.png"), JLabel.CENTER);
player = new JPanel(new BorderLayout());
player.setBounds(n*1, n*1, n, n);
//pacman.setOpaque(true);
pacman.setBackground(new Color(0, 0, 0, 0));
player.add(pacman);
//player.setOpaque(true);
player.setBackground(new Color(0, 0, 0, 0));
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 1000, 1000);
panel.setLayout(null);
panel.add(player);
panel.setBackground(new Color(0, 0, 0,0));
contentPane.add(panel);
the code inside the button actionPreformed method is :
panel.remove(player);
player.setLocation(new Point(n*1, n*2));
panel.add(player);
panel.revalidate();
panel.repaint();
how can i make the old pacman disappears ?

player.setBackground(new Color(0, 0, 0, 0));
Don't use transparent colors. Swing does not handle transparency properly.
For full transparency there is a simple solution. Just make the component transparent:
player.setOpaque( false );
If you ever need partial transparency then check out Backgrounds With Transparency for a solution.

Related

When hovering over element, a copy of the parent JFrame appears inside it [duplicate]

I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent) as the background of the JLabel and I change the text several times using a button. The problem is, everytime the text is changed, the previous text still remains behind the new text. I change the text from "123456789" to "1234567", "12345" and "123". Here is the screenshot:
How do I get rid of this "shadow"?
I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent)
Swing does not support 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.
This related example also makes the JPanel translucent.
try this, maybe it will solve your problem:
In actionPeroformed..
public void actionPerformed(ActionEvent e) {
final JLabel tmpLabel = new JLabel(value[++i]); //change text
label.setFont(new Font("Times New Roman", 1, 36));
label.setForeground(new Color(255, 255, 255));
label.setBackground(new Color(0, 0, 0, .5f));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setOpaque(true);
label.setBounds(10, 10, 270, 70);
label = tmpLabel; //replace the entire label with a new label
}

JPanel losing focus and listeners aren't firing

So in my window, I set the JFrame to undecorated(true) and have my own custom header at the top (with closing and minimizing buttons). The only problem I'm having is making the window move when you drag this 'custom header'. The entire header is in a JPanel which is then added to the JFrame on the North side (BorderLayout.NORTH). I have a MouseListener and MouseMotionListener added to this JPanel, but it does not recognize any events. The only thing I can assume is how I have the layout figured out. Below is the code for the header, along with a visual to go along with it.
CODE:
private void addHeader()
{
headPane = new JPanel();
headPane.setLayout(new BoxLayout(headPane, BoxLayout.LINE_AXIS));
buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 2));
buttonPane.setBackground(mouseLineColor);
headPane.setBackground(Color.GREEN);
Font buttonFont = new Font("", Font.PLAIN, 18);
minimize.setFocusable(false);
minimize.setPreferredSize(new Dimension(30, 20));
minimize.setMargin(new Insets(0, 0, 0, 0));
minimize.setOpaque(false);
minimize.setBorder(null);
minimize.setForeground(Color.WHITE);
minimize.setOpaque(true);
minimize.setFont(buttonFont);
minimize.setBackground(buttonColor);
quit.setFocusable(false);
quit.setPreferredSize(new Dimension(30, 20));
quit.setMargin(new Insets(0, 0, 0, 0));
quit.setOpaque(false);
quit.setBorder(null);
quit.setForeground(Color.WHITE);
quit.setOpaque(true);
quit.setFont(buttonFont);
quit.setBackground(buttonColor);
back.setFocusable(false);
back.setPreferredSize(new Dimension(30, 20));
back.setMargin(new Insets(0, 0, 0, 0));
back.setOpaque(false);
back.setBorder(null);
back.setForeground(Color.WHITE);
back.setOpaque(true);
back.setFont(buttonFont);
back.setBackground(buttonColor);
if(screen != GAME_MENU)
buttonPane.add(back);
else
buttonPane.remove(back);
buttonPane.add(minimize);
buttonPane.add(quit);
headTitle = new JLabel("Bouncy Ball Version " + VERSION);
headTitle.setBorder(new EmptyBorder(0, 5, 0, 0));
headTitle.setFont(new Font("", Font.BOLD, 14));
headTitle.setForeground(Color.BLACK);
headTitle.setBackground(Color.YELLOW);
headTitle.setOpaque(true);
headTitle.setFocusable(false);
headPane.setFocusable(false);
buttonPane.setFocusable(false);
buttonPane.setBackground(Color.RED);
headPane.add(headTitle);
headPane.add(Box.createHorizontalGlue());
headPane.add(buttonPane);
if(callOnce)
{
minimize.addActionListener(this);
quit.addActionListener(this);
back.addActionListener(this);
minimize.addMouseListener(this);
quit.addMouseListener(this);
back.addMouseListener(this);
headPane.addMouseListener(this);
headPane.addMouseMotionListener(this);
callOnce = false;
}
headPane.setPreferredSize(new Dimension(headPane.getPreferredSize().width, 24));
frame.add(headPane, BorderLayout.NORTH);
}
LISTENERS:
MousePressed:
Object source = e.getSource();
if(source == headPane)
{
mouseX = e.getX();
mouseY = e.getY();
movingWindow = true;
}
MouseDragged:
Object source = e.getSource();
if(source == headPane)
{
if(movingWindow)
{
int x = e.getXOnScreen();
int y = e.getYOnScreen();
frame.setLocation(x - mouseX, y - mouseY);
}
}
I'll also add that when I click on the headPane, the JButtons will cease to work as well. I don't know why it's doing this, or if the answer is really simple and I'm just being stupid, but nothing I have tried has worked.
I'm fairly new to Java so thanks in advance for any help.
I see no need for the "callOnce" variable. The frame and the components added to the frame should only be created once when the class is created. If you are calling the "addHeader()" method more than once, then I suggest you have a design problem.
Also you should not be adding the ActionListeners to your buttons twice.
The only problem I'm having is making the window move when you drag this 'custom header'.
Check out Moving Windows for a general purpose class that allows you to drag any component. Typically you would drag a component within a panel.
However, the class also has a feature that allows you to drag a window on the desktop by dragging a component added to the window.

GridBagLayout anchor & JScrollPane issues

I have the following class
public class Demo {
private JFrame mainFrame;
static public Color BGCOLOUR1 = new Color(240, 240, 240);
public Demo() {
mainFrame = new JFrame("Demo");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new BorderLayout());
mainFrame.setSize(900, 800);
JPanel centralPanel = new JPanel();
centralPanel.setOpaque(true);
centralPanel.setBackground(BGCOLOUR1);
centralPanel.setLayout(new GridBagLayout());
centralPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20),
BorderFactory.createTitledBorder("Panel")));
JPanel insidePanel = new JPanel();
insidePanel.setOpaque(true);
insidePanel.setBackground(BGCOLOUR1);
insidePanel.setLayout(new BorderLayout());
insidePanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Inside panel"),
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
JTextArea insidePanelText = new JTextArea(6, 50);
insidePanelText.setLineWrap(true);
insidePanel.add(insidePanelText);
centralPanel.add(insidePanel, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTH,
GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));
JScrollPane scrollPane = new JScrollPane(centralPanel);
mainFrame.add(scrollPane);
}
Why is the inside panel positioned in the centre of the centralPanel (vertically) when I set the GridBagConstraints anchor to NORTH? I would like it positioned at the top.
Also, if I add the centralPanel in a JScrollPane before adding it to the mainFrame as per the example I can resize the application larger just fine, but as soon as I resize it smaller (even though it is still larger than I originally started it) a scroll bar appears. How can I prevent that from happening?
Edit: To illustrate the scrolling problem (I packed the frame when I took these screens):
When I start the application it has no scrollbars
I resize the window larger, and then smaller again. As soon as I make it smaller, the scrollbar appears.
As far as GridBagLayout is concerned, based on the properties you've supplied, it is been laid out to the NORTH of the cell.
GridBagLayout works mostly to the preferred size of the components and calculates the positions of each component around the center of the parent container.
If, instead of:
new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTH,
GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0)
I use something like:
new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.NORTH,
GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0)
it will produce the desired result.
Basically, by using gridy and setting it to 1, we're asking GridBagLayout to give all the remaining vertical space to this cell, but because we're not filling the cell, the contents is pushed to the NORTH of the cell
Also, if I add the centralPanel in a JScrollPane before adding it to the mainFrame as per the example I can resize the application larger just fine, but as soon as I resize it smaller (even though it is still larger than I originally started it) a scroll bar appears. How can I prevent that from happening?
I couldn't really replicate this particular problem, I could use either pack or setSize and resized the window smaller to its "packed" size and the scroll bars would appear, but once I resized the window beyond the "packed" size the scroll bars would disappear

Custom layout in frame

When i add another JPanel into frame previous will dissapper (or probably overlaps the previous one). How can i stop this overlapping?
public Attack() {
JFrame frame = new JFrame("Oracle Padding Attack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.setLayout(null);
JLabel label1 = new JLabel("Inicialization vector:");
createTextField(10, 40, arrayIV, panel1, "HH", true, false);
label1.setBounds(10, 0, 120, 50);
panel1.add(label1);
panel2.setLayout(null);
JLabel label2 = new JLabel("Encrypted text:");
createTextField(400, 40, encryptedTextArray, panel2, "00", true, false);
label2.setBounds(400, 0, 120, 50);
panel2.add(label2);
frame.add(panel1);
frame.add(panel2);
frame.setSize(900, 400);
frame.setVisible(true);
}
It depends on how you want your two panels placed inside your frame. You will need a layout for your frame.
If you want to be able to "jump" from one to the other, cardLayout is your answer.
Otherwise if you want both side to side for example, you will have to use a layout inside your frame. I like the MigLayout, but GridLayout will do the job just fine.
http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html
All build in layout handlers can be found here: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Also it's not recommended to use null layout at all since it will break the look if the window is resized.

JPanel transparency problem

I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent) as the background of the JLabel and I change the text several times using a button. The problem is, everytime the text is changed, the previous text still remains behind the new text. I change the text from "123456789" to "1234567", "12345" and "123". Here is the screenshot:
How do I get rid of this "shadow"?
I have a dark-gray JPanel with a JLabel on it. I set new Color(0, 0, 0, .5f) (tranparent)
Swing does not support 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.
This related example also makes the JPanel translucent.
try this, maybe it will solve your problem:
In actionPeroformed..
public void actionPerformed(ActionEvent e) {
final JLabel tmpLabel = new JLabel(value[++i]); //change text
label.setFont(new Font("Times New Roman", 1, 36));
label.setForeground(new Color(255, 255, 255));
label.setBackground(new Color(0, 0, 0, .5f));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setOpaque(true);
label.setBounds(10, 10, 270, 70);
label = tmpLabel; //replace the entire label with a new label
}

Categories

Resources