JLabel absorbs background from my mouse pointer coordinates JAVA

64 views Asked by At

The code is above. This is an class with score etc. on the left on the window. Each time i move an cursor it shows different "background"under timer i don't know why. This Timer Label is in JPanel no the left which is in some part transparent .How to get rid of this trashes under timer.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.Timer;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

 public class TablePointPanel extends JPanel implements ActionListener{

private int score;
 private static final long serialVersionUID = 1L;
 final public JLabel Score;
final public JLabel PointLabel;
final public JLabel LevelLabel;
private JLabel TimeLabel;
int second=0;
int sets=0;
int minutes=0;
int time=0;
String BufferedTime="";
String Bufferedminutes="";
String Bufferedseconds="";
Timer timeMeasure;

 public TablePointPanel() {



    ImageIcon iconLogo = new ImageIcon("res\\score.png");
    Score = new JLabel();
    Score.setForeground(Color.BLACK);
    Score.setHorizontalAlignment(JLabel.CENTER);
    Score.setVerticalAlignment(JLabel.CENTER);
    Score.setFont(Score.getFont().deriveFont(10.0f));
    //Score.setText("PUNKTY");
    Image img = iconLogo.getImage();
       Image newimg = img.getScaledInstance(80,80, java.awt.Image.SCALE_SMOOTH);
       iconLogo = new ImageIcon(newimg);
    Score.setIcon(iconLogo);

     PointLabel = new JLabel();

    PointLabel.setForeground(Color.BLACK);
    PointLabel.setHorizontalAlignment(JLabel.CENTER);
    PointLabel.setVerticalAlignment(JLabel.CENTER);
    PointLabel.setFont(PointLabel.getFont().deriveFont(16.0f));
    PointLabel.setText(score+"");

    LevelLabel = new JLabel();


         LevelLabel.setForeground(Color.BLACK);

         LevelLabel.setFont(new Font("Tahoma", Font.BOLD, 10));

         LevelLabel.setText(" Runda I");
    SpringLayout springLayout = new SpringLayout();
    springLayout.putConstraint(SpringLayout.NORTH, LevelLabel, 30, SpringLayout.SOUTH, PointLabel);
    springLayout.putConstraint(SpringLayout.SOUTH, LevelLabel, -200, SpringLayout.SOUTH, this);
    springLayout.putConstraint(SpringLayout.EAST, LevelLabel, 8, SpringLayout.EAST, Score);
    springLayout.putConstraint(SpringLayout.NORTH, PointLabel, 6, SpringLayout.SOUTH, Score);
    springLayout.putConstraint(SpringLayout.WEST, PointLabel, -7, SpringLayout.WEST, Score);
    springLayout.putConstraint(SpringLayout.SOUTH, PointLabel, 45, SpringLayout.SOUTH, Score);
    springLayout.putConstraint(SpringLayout.EAST, PointLabel, 5, SpringLayout.EAST, Score);
    springLayout.putConstraint(SpringLayout.WEST, LevelLabel, 5, SpringLayout.WEST, Score);
    springLayout.putConstraint(SpringLayout.NORTH, Score, -3, SpringLayout.NORTH, this);
    springLayout.putConstraint(SpringLayout.EAST, Score, 70, SpringLayout.WEST, this);
    springLayout.putConstraint(SpringLayout.WEST, Score, 6, SpringLayout.WEST, this);
    springLayout.putConstraint(SpringLayout.SOUTH, Score, 80, SpringLayout.NORTH, this);
    setLayout(springLayout);
    this.add(Score);
    this.add(PointLabel);
    this.add(LevelLabel);
    timeMeasure = new Timer(1000,this);
    TimeLabel = new JLabel("00:00");
    TimeLabel.setHorizontalAlignment(SwingConstants.CENTER);
    springLayout.putConstraint(SpringLayout.NORTH, TimeLabel, 6, SpringLayout.SOUTH, LevelLabel);
    springLayout.putConstraint(SpringLayout.WEST, TimeLabel, 0, SpringLayout.WEST, Score);
    springLayout.putConstraint(SpringLayout.SOUTH, TimeLabel, 26, SpringLayout.SOUTH, LevelLabel);
    springLayout.putConstraint(SpringLayout.EAST, TimeLabel, -3, SpringLayout.EAST, Score);
    TimeLabel.setOpaque(false);
    add(TimeLabel);



}

public void setPoint(int x)
    {
score=x;
}
public void resetPoint()
{
    score=0;
}
public void resetTime()
{

    minutes=0;
    second=-1;
    time=0;
}
public int GetTime(){return time;}
 public void updateDisplay(int millis)
    {
    // repaint();
        try
        {
            Thread.sleep(millis);
        }
        catch(InterruptedException e)
        {
        }
    }


 public void paintComponent(Graphics g)
    {

     Image left = new ImageIcon("res\\transparent\\ROCK.png").getImage();
     g.drawImage(left, 0, 0, this.getWidth(), this.getHeight(), null);



    }

@Override
public void actionPerformed(ActionEvent e) {

    time+=1;
    second+=1;
    if(second==60)
    {

        second=0;
        minutes=1;
    }
    if(second<10)
    Bufferedseconds="0"+second;
    else
    Bufferedseconds=second+"";
    if(minutes<10)
    Bufferedminutes="0"+minutes;
    else
    Bufferedminutes=minutes+"";
    BufferedTime=Bufferedminutes+":"+Bufferedseconds;
    TimeLabel.setText(BufferedTime);

}

}

1

There are 1 answers

2
camickr On BEST ANSWER
  1. Variable names should NOT start with an upper case character. Some of your variables are correct and others are not. Be consistent!!!

  2. Painting methods are for painting only. Don't do I/O in a painting method. The image should be read when the class is constructed.

  3. Don't create an ImageIcon just to read an Image. Instead you can use ImageIO.read(...)

  4. Don't forget to invoke super.paintComponent() when doing custom painting. This will make sure the background is cleared before you paint the image to you don't get painting artifacts.