栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我如何平滑我的JFrame形状

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我如何平滑我的JFrame形状

呈现内容的方式将有很多,但是基本概念是为要绘制的Graphics上下文提供呈现提示。

例如,如果我要绘画组件,则可能会使用类似…的方法。

// Create a "copy" of the graphics context so we don't modify any of it's existing// settings.  This makes it easier to manage the graphics context as we // may not want to effect anything else that might be using this graphics context// into the futureGraphics2D g2d = (Graphics2D)g.create();RenderingHints hints = new RenderingHints(    RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);g2d.setRenderingHints(hints);//... continue drawing.// Dispose of our "copy" of the graphics contextg2d.dispose();

查看控制渲染质量以获取更多详细信息

更新了示例

public class AATest {    public static void main(String[] args) {        new AATest();    }    public AATest() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {     }     Jframe frame = new Jframe();     frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     frame.setLayout(new BorderLayout());     frame.add(new PaintPane());     frame.pack();     frame.setLocationRelativeTo(null);     frame.setVisible(true); }        });    }    public class PaintPane extends JPanel {        @Override        public Dimension getPreferredSize() { return new Dimension(215, 110);        }        @Override        protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); RenderingHints hints = new RenderingHints(         RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHints(hints); g2d.setColor(Color.RED); drawShape(g2d, 5, 5); g2d.dispose(); g2d = (Graphics2D) g.create(); g2d.setColor(Color.BLUE); drawShape(g2d, 110, 5); g2d.dispose();        }        protected void drawShape(Graphics2D g2d, int x, int y) { g2d.draw(new Ellipse2D.Float(x, y, 100, 100));        }    }}

更新了新示例

我使用的技巧之一是,不使用“ setShape”,而是简单地创建一个透明窗口,并使用自定义面板为我提供要
使用的形状。

这样做的主要问题是,您现在有责任确保将内容绘制在形状中……

public class ShapedWindow {    public static void main(String[] args) {        new ShapedWindow();    }    public ShapedWindow() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {     }     JWindow frame = new JWindow();     frame.setBackground(new Color(0, 0, 0, 0));     frame.setContentPane(new ShapedPane());     frame.pack();     frame.setLocationRelativeTo(null);     frame.setVisible(true);     frame.setAlwaysonTop(true); }        });    }    public class ShapedPane extends JPanel {        public ShapedPane() { setOpaque(false); setLayout(new GridBagLayout()); JButton button = new JButton("Close"); button.addActionListener(new ActionListener() {     @Override     public void actionPerformed(ActionEvent e) {         System.exit(0);     } }); add(button);        }        @Override        public Dimension getPreferredSize() { return new Dimension(200, 200);        }        @Override        protected void paintComponent(Graphics g) { super.paintComponent(g); //To change body of generated methods, choose Tools | Templates. Graphics2D g2d = (Graphics2D) g.create(); RenderingHints hints = new RenderingHints(         RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHints(hints); g2d.setColor(getBackground()); g2d.fill(new Ellipse2D.Float(0, 0, getWidth(), getHeight())); g2d.dispose();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/486173.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号