我正在使用Swing制作应用程序,我想在主面板上添加一个带有叉形图标的按钮。但是当我在其上绘制图像时,该图像正在呈现奇怪的图像。
我已经尝试过几种方法,例如在应用程序外部调整图像大小,并且十字叉是用IllustratorCC制作的,所以我认为这不是源图像的质量。
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
public class ImageRenderingBadly extends JPanel
{
BufferedImage cross;
public ImageRenderingBadly()
{
try {
URL url = new URL("https://i.imgur.com/cWGntek.png");
cross = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g)
{
g.drawImage(cross,0,0,null);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(new Dimension(200,200));
frame.setBackground(new Color(0));
ImageRenderingBadly panel = new ImageRenderingBadly();
frame.setContentPane(panel);
frame.setVisible(true);
}
}
来源:
渲染效果很差
找到解决方案
在paintComponent:(https://docs.oracle.com/javase/tutorial/2d/advanced/quality.html)中使用抗锯齿
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(rh);
g2.drawImage(cross,0,0,null);
}
答案 0 :(得分:1)
它对我有用:
请注意,我已使用该图像创建了两张图像和图标,其中一个用于按钮,一个用于按下状态,以指示其已被按下。
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class XButtonTest {
private static final String X_IMG_PATH = "https://i.imgur.com/cWGntek.png";
public static void main(String[] args) {
try {
URL xImgUrl = new URL(X_IMG_PATH);
BufferedImage xImage = ImageIO.read(xImgUrl);
int w = xImage.getWidth();
int h = xImage.getHeight();
BufferedImage pressedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = pressedImg.createGraphics();
g2.setColor(Color.LIGHT_GRAY);
g2.fillRect(0, 0, w, h);
g2.drawImage(xImage, 0, 0, null);
g2.dispose();
Icon icon = new ImageIcon(xImage);
Icon pressedIcon = new ImageIcon(pressedImg);
JButton button = new JButton(icon);
button.setPressedIcon(pressedIcon);
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setContentAreaFilled(false);
JPanel panel = new JPanel();
panel.add(button);
JOptionPane.showMessageDialog(null, panel, "Test", JOptionPane.PLAIN_MESSAGE);
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:0)
我发现“ drawImage”渲染的图像很差,这就是我认为的问题:
public class Panel extends JPanel
{
BufferedImage image;
public Panel() {
super();
try {
image = ImageIO.read(new File("images/BMW-TA.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
}
来源:https://i.imgur.com/ebr17CV.jpg
渲染:https://i.imgur.com/Z01I7mn.png
找到解决方案
在paintComponent中使用抗锯齿:
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(rh);
g2.drawImage(cross,0,0,null);
}