java- repaint() method is misbehaving - 2? - java

This question is an extension of java- repaint() method is misbehaving?
(Reading it, is optional)
I am working on a Music Player
I am using a JSlider as seek bar and using a JLabel to draw text on screen, such as song name.
I am new to Graphics2D
Here's the minimized code:
public class JSliderDemo extends JFrame
{
JLabel label;
JSlider seek = new JSlider();
int y = 10;
public JSliderDemo()
{
setSize(400, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
createWindow();
setVisible(true);
startThread();
}
public void createWindow()
{
JPanel panel = new JPanel(new BorderLayout());
panel.setOpaque(true);
panel.setBackground(Color.BLUE);
panel.setBorder(new LineBorder(Color.YELLOW));
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
label = new Component();
label.setSize(300, 300);
createSlider();
layeredPane.add(seek, new Integer(50));
layeredPane.add(label, new Integer(100));
panel.add(layeredPane);
add(panel);
}
protected void createSlider()
{
seek.setUI(new SeekBar(seek, 300, 10, new Dimension(20, 20), 5,
Color.DARK_GRAY, Color.RED, Color.RED));
seek.setOrientation(JProgressBar.HORIZONTAL);
seek.setOpaque(false);
seek.setLocation(10, 50);
seek.setSize(300, 20);
seek.setMajorTickSpacing(0);
seek.setMinorTickSpacing(0);
seek.setMinimum(0);
seek.setMaximum(1000);
seek.setBorder(new MatteBorder(5, 5, 5, 5, Color.CYAN));
}
protected void startThread()
{
Thread thread = new Thread(new Runnable(){
#Override
public void run()
{
try
{
while(true)
{
if(y == label.getHeight()){y = 1;}
label.repaint();
y += 1;
Thread.sleep(100);
}
}
catch(Exception ex){}
}
});
thread.start();
}
protected class Component extends JLabel
{
#Override
public void paintComponent(Graphics g)
{
Graphics2D gr = (Graphics2D) g;
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gr.setColor(Color.RED);
gr.setFont(new Font("Calibri", Font.PLAIN, 16));
gr.drawString("Song Name", 50, y);
gr.dispose();
}
}
public static void main(String[] args)
{
new JSliderDemo();
}
}
The problem is, when I call repaint() for JLabel it automatically repaints JSlider with it even though JSlider is not included in JLabel.
Output :
Slider re-painted
Slider re-painted
Slider re-painted
Slider re-painted
Slider re-painted
Slider re-painted.........
Now if I remove label.repaint() from the Thread, then the JSlider is not re-painted.
Output:
Slider re-painted
Slider re-painted
Is the repaint() method supposed to work like this?
In my last question, I was told to use Layout Manager and when I did use GridLayout just for checking if it's the solution, then it worked!
Only JLabel was repainted.
But I want to overlap JLabel on JSlider, so I thought of using JLayeredPane. And now, the problem is back.
How can I solve this?
Bottom Line : How can I overlap JLabel on JSlider without leading to repaint() method misbehave ?
OR
Does the repaint() method work like this?

As was already mentioned in the comments, the reason for your JSlider being repainted is that it has overlapping bounds with the JLabel. Even though your label doesn't paint over the area of the slider swing will still mark the overlapping area as dirty (i.e. the overlapping part of the slider will need to be repainted) because swing doesn't know that you are only painting in one part of the component.
To reduce the amount of repaints you will need to make the size of your JLabel smaller. Preferably only as large as it needs to be by invoking its getPreferredSize() method. You'll then be able to move the text by moving the location of the label.
Also you shouldn't be doing updates to the gui in a plain Thread. Use javax.swing.Timer instead. It ensures that all updates to the gui happen on the swing event thread, which is where they should be made.
After making these adjustments to your code the slider is only repainted while the label is actually visually over the slider.
public class JSliderDemo extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(JSliderDemo::new);
}
private final JLabel label = new CustomLabel();
public JSliderDemo() {
setSize(400, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
createWindow();
setVisible(true);
startTimer();
}
public void createWindow() {
JPanel panel = new JPanel(new BorderLayout());
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(300, 310));
label.setLocation(0, 0);
label.setBorder(new LineBorder(Color.RED));
label.setSize(label.getPreferredSize());
layeredPane.add(createSlider(), Integer.valueOf(50));
layeredPane.add(label, Integer.valueOf(100));
panel.add(layeredPane);
setContentPane(panel);
}
protected JSlider createSlider() {
JSlider seek = new CustomSlider();
seek.setOrientation(JProgressBar.HORIZONTAL);
seek.setOpaque(false);
seek.setLocation(10, 50);
seek.setSize(300, 20);
seek.setMajorTickSpacing(0);
seek.setMinorTickSpacing(0);
seek.setMinimum(0);
seek.setMaximum(1000);
seek.setBorder(new LineBorder(Color.BLUE));
return seek;
}
private void startTimer() {
new Timer(100, e -> {
int y = label.getY();
int maxY = label.getParent().getHeight();
if (y == maxY) {
y = -label.getHeight();
}
label.setLocation(label.getX(), y + 1);
label.repaint();
}).start();
}
private static class CustomLabel extends JLabel {
protected CustomLabel() {
setFont(new Font("Calibri", Font.PLAIN, 16));
setText("Song Name");
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Painting Label");
}
}
protected static class CustomSlider extends JSlider {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("Painting Slider");
}
}
}

Related

JButton responds in JFrame, but not in a JPanel

I'm new to java and I'm trying to figure out how action listeners and buttons work. I've found that I can get a working JButton if I put it directly into my JFrame object. But if I put it in a JPanel within that JFrame, it won't respond. Why is that?
Main.java
public class Main {
private static Frame f = new Frame();
public static void main(String[] args) {}
}
Frame.java
public class Frame extends JFrame {
private final int WIDTH = 640, HEIGHT = 480;
private Panel p = new Panel();
Frame() {
super("Java Program");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(WIDTH, HEIGHT);
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
p.paintComponent(g);
}
}
Panel.java
public class Panel extends JPanel {
JButton b = new JButton("Button");
Panel() {
b.setBounds(0, 0, 200, 100);
add(b);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
b.setText("Pressed");
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
b.paint(g);
}
}
I am not a Swing expert so I can't really explain why it does not work. It seems like an unresponsive button is painted on top of you button. I tweaked it a little and here are a few modifications to get it to work:
Add the panel to the Frame: add(p);
Remove the this.setLayout(null); line, it seems to mess up the frame
To set the size of the Frame, use setPreferredSize: this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
You also need to call pack() at then end of your Frame constructor.
And you need to remove b.paint(g) from your Panel.paintComponent(), this seems to be what paints the "unresponsive" button, (see image at the end of the answer).
Optionally, you can remove the paint() from the Frame, it does nothing more than the JFrame's one
Here is a modified working version:
class Frame extends JFrame {
private final int WIDTH = 640, HEIGHT = 480;
private Panel p = new Panel();
Frame() {
super("Java Program");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
this.setLocationRelativeTo(null);
this.setVisible(true);
// add the panel to the frame
add(p);
pack();
}
}
class Panel extends JPanel {
JButton b = new JButton("Button");
Panel() {
b.setBounds(0, 0, 200, 100);
add(b);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
b.setText("Pressed");
}
});
}
// You can also get rid of this method,
// I just leave it here to show that I removed the b.paint(g) line
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
Here is what the same code shows if you leave b.paint(g) in Panel.paintComponent(), as you can see there are 2 buttons, the one in the corner does not work.

No rectangle shown in JPanel

I trying to draw a rectangle in JPanel but the rectangle not showing. What did I missed ?
Here is what I have tried so far.
public class selectSeat extends JFrame {
JPanel panel = new JPanel();
public static void main(String[] args) {
selectSeat frameTabel = new selectSeat("","","");
}
public selectSeat(String title, String day, String time)
{
super("Select Seat");
setSize(350,350);
setLocation(500,280);
panel.setLayout(null);
RectDraw rect= new RectDraw();
panel.add(rect);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private static class RectDraw extends JPanel
{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(230,80,10,10);
g.setColor(Color.RED);
g.fillRect(230,80,10,10);
}
public Dimension getPreferredSize() {
return new Dimension(50, 20); // appropriate constants
}
}
}
You're drawing the rectangle, but it's located at 280, 80, which is way outside the confines of your visible JPanel. Understand that the drawing location is relative to the coordinates within the JPanel itself.
Noticed that you are using Absolute layout (null layout). Component.setbounds are needed in order to position the object in place.
public Test(String title, String day, String time)
{
super("Select Seat");
setSize(350,350);
setLocation(500,280);
panel.setLayout(null);
RectDraw rect= new RectDraw();
rect.setBounds(0, 0, 100, 100);
panel.add(rect);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
Check out the details:
https://docs.oracle.com/javase/tutorial/uiswing/layout/problems.html
Note: Try Ctrl+Shift+F1 to get debug message from AWT as well.

Weird shadowing while changing JLabel text

I am working on a little menu program with clickable buttons and an image that changes based on button clicks. If I click a button I get a shadow of the button at the bottom where I change the JLabel text. I cannot figure it out for the life of me. Any advice would be greatly appreciated. Visuals below...thanks
public class SampleGUI
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
System.out.println("Created GUI on EDT? " +
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Robert's VICI Prototype");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.pack();
f.setVisible(true);
}
}
class MyPanel extends JPanel
{
// Fields
Image imageDisplayed;
JLabel status;
// Methods
public MyPanel()
{
setBorder(BorderFactory.createLineBorder(Color.BLACK));
setLayout(null);
JLabel title = new JLabel("CS380 TEAM 5 VICI Prototype");
title.setFont(new Font("Tahoma", Font.BOLD, 20));
title.setBounds(425, 10, 400, 40);
add(title);
status = new JLabel("Please click an option above.");
status.setFont(new Font("Tahoma", Font.BOLD, 14));
status.setBounds(425, 740, 400, 40);
add(status);
JButton choice1 = new JButton("Search Class");
choice1.setBounds(50, 50, 150, 40);
add(choice1);
JButton choice2 = new JButton("Add Class");
choice2.setBounds(225, 50, 150, 40);
add(choice2);
JButton choice3 = new JButton("Drop Class");
choice3.setBounds(400, 50, 150, 40);
add(choice3);
JButton choice4 = new JButton("Verify Reg Hold");
choice4.setBounds(575, 50, 150, 40);
add(choice4);
JButton choice5 = new JButton("Verify Reg Date");
choice5.setBounds(750, 50, 150, 40);
add(choice5);
JButton choice6 = new JButton("Schedule Advisor");
choice6.setBounds(925, 50, 150, 40);
add(choice6);
choice6.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Schedule Advisor button pressed.");
status.setText("Choose a date.");
imageDisplayed = new ImageIcon("C:\\Temp\\sa01.jpg").getImage();
}
});
JButton exit = new JButton("EXIT");
exit.setBounds(940, 750, 150, 40);
add(exit);
exit.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
imageDisplayed = new ImageIcon("C:\\Temp\\main.jpg").getImage();
}
#Override
public Dimension getPreferredSize()
{
return new Dimension(1100, 800);
}
#Override
public void paintComponent(Graphics g)
{
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
}
You've broken the paint chain...
#Override
public void paintComponent(Graphics g)
{
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
The first thing you should be calling is super.paintComponent(g)
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(imageDisplayed, 100, 120, 900, 600, this);
}
The Graphics context is shared resource, meaning that the Graphics context you get has also been used to paint the other components that have also been painted. One of the jobs of paintComponent is to clear the Graphics context ready for the component to be painted (fill the background)
See Painting in AWT and Swing and Performing Custom Painting for more details
You should, also, avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. See Why is it frowned upon to use a null layout in SWING? for more details...
Before you go all defensive over getting your screen laid out just nicely, you shouldn't be doing it this way. You controls should be laid out on separate containers (using appropriate layout managers) and you "drawing surface" should be it's own component. See Laying Out Components Within a Container for more details...
If you're still not convicned, take a look at Why setting setPreferredSize() on JFrame is bad? and java expandable JDialog for examples of differences between Mac and Windows

When I set the window to non-opaque,the font look like changed

When I set the window to non-opaque,the font look like changed!Who can tell me why and help me ,thanks!
I guess this is affected by the "RenderingHints.KEY_ANTIALIASING",but I test in many way,and ther is no my desired result.
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setUndecorated(true);
AWTUtilities.setWindowOpaque(frame, false);
JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 10));
mainPanel.add(new JLabel("Why this changed?"));
JLabel lbl2 = new JLabel() {
#Override
public void paint(Graphics g) {
// super.paint(g);
// ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawString("There is no change", 0, 15);
}
};
lbl2.setPreferredSize(new Dimension(240, 22));
mainPanel.add(lbl2);
JPanel toolPanel = new JPanel(new FlowLayout());
final JCheckBox ckxWindowOpaque = new JCheckBox("WindowOpaque");
ckxWindowOpaque.setSelected(!AWTUtilities.isWindowOpaque(frame));
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
boolean b = AWTUtilities.isWindowOpaque(frame);
if (b == !ckxWindowOpaque.isSelected()) return;
if (b) {
AWTUtilities.setWindowOpaque(frame, false);
} else {
AWTUtilities.setWindowOpaque(frame, true);
}
}
};
ckxWindowOpaque.addActionListener(al);
toolPanel.add(ckxWindowOpaque);
toolPanel.add(new JButton(new AbstractAction("Exit") {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}));
frame.getContentPane().add(toolPanel, BorderLayout.NORTH);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
((JComponent) frame.getContentPane()).setBorder(BorderFactory.createLineBorder(Color.black));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBounds(200, 200, 200, 200);
frame.setVisible(true);
}
This will have to do with how Swing/AWT deals with the different requirements between an opaque and transparent window and changes being made internally to the anti aliasing.
For example, if I use
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
To render the text, I can get it to appear the same way when it's made transparent
I also get the same result from VALUE_TEXT_ANTIALIAS_DEFAULT, VALUE_TEXT_ANTIALIAS_GASP
But if I use VALUE_TEXT_ANTIALIAS_LCD_HBGR or VALUE_TEXT_ANTIALIAS_LCD_HRGB it will act the same as the JLabel in both modes
These are system level decisions and I don't think you can effect them (easily).
You might like to take a look at LCD Text: Anti-Aliasing on the Fringe, which is an interesting read, but I'm not sure it will help much...
I have been debugging and tracing my program,and I found the key code here:SwingUtilities2.drawString/drawChars/drawTextAntialiased.
So,I modified the JRE's code in the "SwingUtilities2.drawString/drawChars",I add code like this :
if (UIManager.getBoolean("MYLAF.AATextInfo.Disable")) {
g.drawChars(data, offset, length, x, y);
return nextX;
}
Finally,on the begin of my program,I add the setting "UIManager.put("MYLAF.AATextInfo.Disable",true)".
If you wan't modify the SwingUtilities2,you can use "myJComponent.setClientProperty(AA_TEXT_PROPERTY_KEY,null)".

repaint doesnt clear the panel before repainting

I got a JPanel where a triangle is painted.
When someone clicks on a button the triangle should be repainted with new parameters. The problem is the old triangle is still there and the new one is messed up with part of the textfield underneath.
public class Vermessung {
private static void eingabe(){
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(screen.height/2, screen.height/4*3);
JPanel jp = new JPanel();
jp.setLayout(new BoxLayout(jp, BoxLayout.PAGE_AXIS));
//Eingabebereich
JPanel eingabebereich = new JPanel(new GridLayout(3, 1));
JPanel abc = new JPanel(new GridLayout(4, 2));
abc.add(new JLabel("Strecke"));
abc.add(new JLabel("Gemessener Wert in [m]"));
abc.add(new JLabel("a:"));
abc.add(tfa);
abc.add(new JLabel("b:"));
abc.add(tfb);
abc.add(new JLabel("c:"));
abc.add(tfc);
//AusgabeBereich
JPanel ausgabe = new JPanel(new GridLayout(2, 3));
ausgabe.add(new JLabel("p [m]"));
ausgabe.add(new JLabel("q [m]"));
ausgabe.add(new JLabel("h [m]"));
ausgabe.add(P);
ausgabe.add(Q);
ausgabe.add(H);
P.setEditable(false);
Q.setEditable(false);
H.setEditable(false);
//Buttons mit Listenern
JPanel buttons = new JPanel(new FlowLayout());
JButton ok = new JButton("OK");
JButton cancel = new JButton("beenden");
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//Textfelder auslesen
TextfelderAuslesen();
//bei gueltiger Eingabe Höhe Berechnen
if(berechenbar){
berechnungPQ();
berechnungH();
P.setText(String.valueOf(p));
Q.setText(String.valueOf(q));
H.setText(String.valueOf(h));
sketch.update(vec);
sketch.repaint();
}else{
}
}
};
ok.addActionListener(al);
ActionListener beenden = new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
};
cancel.addActionListener(beenden);
buttons.add(ok);
buttons.add(cancel);
//Fensteraufbau
sketch.setPreferredSize(new Dimension(screen.height/2, screen.height/2));
jp.add(sketch);
eingabebereich.add(abc);
eingabebereich.add(ausgabe);
eingabebereich.add(buttons);
eingabebereich.setPreferredSize(new Dimension(screen.height/4, screen.height/4));
jp.add(eingabebereich);
f.add(jp);
f.setVisible(true);
}
}
public class Zeichnung extends JPanel{
public void paint(Graphics g){
zeichneDreieck(g);
}
private void zeichneDreieck(Graphics g){
berechneLaengen();
g.setColor(new Color(255,0,0));
g.drawLine(30, 30, ca, 30);
g.drawString("c", ca/2, 20);
g.drawLine(ca, 30, qa, ha);
g.drawString("a", (ca-pa/2), ha/2);
g.drawLine(qa, ha, 30, 30);
g.drawString("b", (qa/2), ha/2);
g.setColor(new Color(0,0,0));
g.drawLine(qa, ha, qa, 30);
g.drawString("h", qa+5, ha/2);
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
zeichneDreieck(g);
}
For JPanel, override paintComponent() instead. As the first line, call the super method.
Call
yourFrame.invalidate();
yourFrame.validate();
yourFrame.repaint();
See more information about invalidate() and validate() here.
Per the documentation: The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
Create a new method clear(Graphics g) in your Zeichnung class and clear all the lines from it by adding the body as:
super.paintComponent(g);
this.removeAll();
this.updateUI();
Call this method in your zeichneDreieck method at the first line.

Categories