收缩JFrame导致图纸消失; Bullseye边界没有出现

时间:2014-03-21 03:54:05

标签: java jar jframe

我应该创建一个bullseye.jar文件。我有几个不同的问题。

  1. 我在牛眼里的圈子都需要它们的边界,但它们并没有出现。我认为g2.draw会照顾到这一点,但没有骰子。

  2. 当我运行程序并将框架拖得更小时,靶心尺寸也会缩小。但我必须确保较小的圆圈不会消失(当牛眼缩小时它们正在做的事情。)

  3. 我有两节课。第一个是组件类,第二个是主要的。

    此处还有link to an online IDE

    /**
     *
     * @author joe
     */
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.JComponent;
    
    public class BullseyeComponent extends JComponent{
    
        int count = 0;
    
        @Override
    
        public void paintComponent(Graphics g){
    
            Graphics2D g2 = (Graphics2D) g;
            //make circle fill screen
            int radius = Math.min(this.getHeight(),this.getWidth()) / 2;
    
    
            g2.draw(new Circle(this.getWidth()/ 2, this.getHeight() / 2, radius ));
            g2.setColor(Color.WHITE);
            g2.fill(new Circle(this.getWidth()/ 2, this.getHeight() / 2, radius));
    
    
            g2.draw(new Circle(this.getWidth()/ 2 , this.getHeight() / 2, radius - 25 ));
            g2.setColor(Color.WHITE);
            g2.fill(new Circle(this.getWidth()/ 2 , this.getHeight() / 2 , radius - 25));
    
    
            g2.draw(new Circle(this.getWidth()/ 2 , this.getHeight() / 2, radius - 50 ));
            g2.setColor(Color.BLACK);
            g2.fill(new Circle(this.getWidth()/ 2 , this.getHeight() / 2 , radius - 50));
    
    
            g2.draw(new Circle(this.getWidth()/ 2 , this.getHeight() / 2, radius - 75 ));
            g2.setColor(Color.BLACK);
            g2.fill(new Circle(this.getWidth()/ 2 , this.getHeight() / 2 , radius - 75));
    
            g2.draw(new Circle(this.getWidth()/ 2 , this.getHeight() / 2, radius - 100 ));
            g2.setColor(Color.CYAN);
            g2.fill(new Circle(this.getWidth()/ 2 , this.getHeight() / 2 , radius - 100));
    
    
            g2.draw(new Circle(this.getWidth()/ 2 , this.getHeight() / 2, radius - 125 ));
            g2.setColor(Color.CYAN);
            g2.fill(new Circle(this.getWidth()/ 2 , this.getHeight() / 2 , radius - 125));
    
            g2.draw(new Circle(this.getWidth()/ 2 , this.getHeight() / 2, radius - 150 ));
            g2.setColor(Color.RED);
            g2.fill(new Circle(this.getWidth()/ 2 , this.getHeight() / 2 , radius - 150));
    
    
            g2.draw(new Circle(this.getWidth()/ 2 , this.getHeight() / 2, radius - 175 ));
            g2.setColor(Color.RED);
            g2.fill(new Circle(this.getWidth()/ 2 , this.getHeight() / 2 , radius - 175));
    
            g2.draw(new Circle(this.getWidth()/ 2 , this.getHeight() / 2, radius - 200 ));
            g2.setColor(Color.YELLOW);
            g2.fill(new Circle(this.getWidth()/ 2 , this.getHeight() / 2 , radius - 200));
    
    
            g2.draw(new Circle(this.getWidth()/ 2 , this.getHeight() / 2, radius - 225 ));
            g2.setColor(Color.YELLOW);
            g2.fill(new Circle(this.getWidth()/ 2 , this.getHeight() / 2 , radius - 225));
    
        }
    }
    
    class Circle extends Ellipse2D.Double {
    
        public Circle(double x, double y, double radius) {
            super(x - radius, y - radius, 2 * radius, 2 * radius);
        }
    }
    
    /**
     *
     * @author joe
     */
    
    import javax.swing.*;
    
    public class BullseyeViewer {
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(500, 550);
            frame.setTitle("Bullseye");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            BullseyeComponent component = new BullseyeComponent();
            frame.add(component);
    
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    

1 个答案:

答案 0 :(得分:2)

修复1

你现在的绘画方式,

g2.draw(new Circle(this.getWidth()/ 2, this.getHeight() / 2, radius ));
g2.setColor(Color.WHITE);
g2.fill(new Circle(this.getWidth()/ 2, this.getHeight() / 2, radius));

会覆盖边框(draw之前的fill)。替换为

g2.fill(new Circle(this.getWidth()/ 2, this.getHeight() / 2, radius));
g2.setColor(Color.WHITE);
g2.draw(new Circle(this.getWidth()/ 2, this.getHeight() / 2, radius ));

更改颜色(边框应该有不同的颜色,否则它们会以适当的方式与外圈合并)。


修复2

您正在对半径差异进行硬编码。即使你施加了很多黑客攻击,这也会使内圈消失。

所以要修复它,你必须相应地缩放半径。

g2.fill(new Circle(this.getWidth()/ 2, this.getHeight() / 2, radius * 0.8));
g2.setColor(Color.WHITE);
g2.draw(new Circle(this.getWidth()/ 2, this.getHeight() / 2, radius * 0.8));

所有半径定义为外半径的百分比,你应该好好去。


希望这有帮助。
祝你好运。