开发者

JPanel Obscures BufferedImage

I'm having trouble finding out why the following problem happens: In a program that uses "extends Frame" to create a window, I can use BufferedImage to draw to the graphics context of the Frame (not JFrame), and it looks just fine. However, the moment I declare a JPanel, all of the text drawn by BufferedImage becomes obscured (not completely, but semi-transparent and hard to read), even if I don't add the JPanel to the JFrame.

Here's a simplified version of the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import java.awt.image.*;
import javax.swing.*;

public class MyProgram extends Frame {

    static Frame f;
    static Timer timer;

    public static void main(String[] args) {

        f = new Frame();
        f.setSize(400, 200);
        f.setResizable(false);
        f.setVisible(true);

        f.addKeyListener(new KeyAdapter() {

        public void keyPressed(KeyEvent e) {

            drawScreen();
        }
    });

    drawScreen();
}

public static void drawScreen() {

    BufferedImage off_i = new BufferedImage(f.getWidth(), f.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = off_i.createGraphics();

    g.setColor(new Color(50, 50, 50));
    g.drawString("Hit any key; why does this text change?", 15, f.getHeight() - 10);

    Graphics off_g = f.getGraphics();
    off_g.drawImage(off_i, 0, 0, null);

    JPanel panel = new JPanel();
    }
}

I could maybe understand seeing the problem arise if I had added the JPanel to the JFrame and didn't set any bounds to its visibility, but even creating the JPanel gives that issue, which seems weird to me.

Basically, what I'm trying to do here is take an existing program that I have that runs just fine without JPanel, and I want to add to it a JTextArea so that I can accept copy/paste text for modifying the displaying of the program.

My understanding of Java is kind of spotty, as I learned it mainly by hobby and not formally, but I'm always lookin开发者_运维百科g to learn more when I can. Thanks for the help!

Update: I discovered that this problem only happens when the draw function is called again, after the JPanel has been declared, though I still don't understand why it does that or how to get around it.


better would be put Image to the JLabel and how ot use Icon

please read Using Swing Components and how to LayoutManagers works

tons examples on java2s.com


  1. Don't mix AWT components with Swing component. That is you should use a JFrame NOT a Frame.

  2. Don't use getGraphics(). Custom Painting is done by overriding the paintComponent() method of a JPanel (or JComponent). You just use the Graphics object that is passed to the method. Then you add the panel to the frame.

  3. As already mentioned using a JLabel is simpler because you don't have to do any custom painting. The tutorial also has a section on "How to Use Icons".


I tried to run your code. And although the effect that you are describing does not happen on my system I can recommend you something.

First try to create your panel before it is visualized. In this case java does not have to re-arrange the components that are already on screen.

Second, if you have to draw things on visible frame call validate() of the container when you are done. This makes java to re-arrange stuff.

Third, when you are using drawXXX() methods create your own class that extends Component, JComponent, Canvas and override its `paint(Graphics) method. In this case the system will call this method every time it needs (e.g. when window is resized) and your UI will be painted again.

BTW I have 2 questions:

  1. why are you using drawText() instead of Label or JLabel? Use them and avoid such kind of problems.

  2. Why do you extend your class from Frame and do not use this fact but create yet another instance of Frame?


As an answer to my original question:

It seems that initializing JPanel alongside awt draw() commands causes the text to be antialiased, which makes the text look harder to read, partially obscured, thinner, etc. Although I tried setRenderingHint() with VALUE_TEXT_ANTIALIAS_OFF, it did not solve the problem. But as other posters pointed out it's not best practice to mix the two components.

While this doesn't exactly solve my problem, it does answer the question of what is going on, that being text antialiasing as some result of JPanel (does that sound right?). Ideally I wouldn't want to rewrite all of the code just to add a single JTextArea into an already existing codebase. But perhaps it's good every now and then to revisit old code and revamp it where it may be faulty.

Thanks everyone for the comments, information, and resource links!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜