我想使用图形在 JLabel 中绘制椭圆形图像。这是我的代码,但不了解Graphics。
int[] arr1 = new int[5] { 2, 5, 1, 7, 4 };
int[] arr2 = new int[5] { 10, 16, 13, 17, 15 };
int[] arr3 = arr1.Concat(arr2).OrderBy(x => x).ToArray();
答案 0 :(得分:0)
您需要here和here提及的setClip()方法的帮助。
当涉及到代码时,它应该看起来像这样
public class OvalImageLabel extends JLabel {
private Image image;
public OvalImageLabel(URL imageUrl) throws IOException {
image = ImageIO.read(imageUrl);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setClip(new java.awt.geom.Ellipse2D.Float(0f,0f, getWidth(),getHeight()/2));
g.drawImage(image, 0, 0, this);
}
}
以及使用此类的正在运行的应用程序
public class UsageExample extends JPanel {
public UsageExample() {
super(new BorderLayout());
OvalImageLabel l;
try {
l = new OvalImageLabel(new File("/path/to/image.png").toURI().toURL());
} catch (Exception e) {
e.printStackTrace();
return;
}
add(l, BorderLayout.CENTER);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setContentPane(new UsageExample());
frame.setSize(200, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}