Содержимое блока панели окон JFrame

1

Я пытаюсь нарисовать круг в левом верхнем углу. Часть круга перекрывается границей окна? Как этого избежать?

public class Yard extends JFrame {
    public static final int WIDTH = 15;
    public static final int HEIGHT = 15;
    private static final int BLOCK_SIZE = 30;

public void launch() {
    this.setLocation(200, 200);
    this.setSize(WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE);
    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    this.setVisible(true);
}

@Override
public void paint(Graphics g) {
    Color c = g.getColor();
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
    g.setColor(c);
    g.fillOval(0, 0, 100, 100);
}

    public static void main(String Args[]) {
        new Yard().launch();
    }
}
  • 2
    основы из учебника Oracle Graphics2D задаются здесь 10-15 раз в день
Теги:
frame
swing
paint

2 ответа

3
Лучший ответ

1) Узнайте больше о пользовательских картинах в качелях.

2) Для картин лучше использовать JPanel вместо JFrame и paintComponent() метод JComponent вместо paint().

Простой пример:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestFrame extends JFrame {

    public static void main(String... s){
        new TestFrame();
    }

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        add(new DrawPanel());
    }

    private class DrawPanel extends JPanel {

        public static final int WIDTH = 15;
        public static final int HEIGHT = 15;
        private static final int BLOCK_SIZE = 30;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Color c = g.getColor();
            g.setColor(Color.GRAY);
            g.fillRect(0, 0, HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
            g.setColor(c);
            g.fillOval(0, 0, 100, 100);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
        }
    }
}
  • 2
    плюс один, но g.fillRect (0, 0, ВЫСОТА * BLOCK_SIZE, ШИРИНА * BLOCK_SIZE); == getHeight / Weight
  • 0
    @mKorbel ты прав
2

Для создания компонента используйте JPanel вместо JFrame. Здесь пример кода, который вы можете использовать, запускается.

public class Yard extends JPanel {
    public static final int WIDTH = 15;
    public static final int HEIGHT = 15;
    private static final int BLOCK_SIZE = 30;

    public void launch() {
        JFrame frame = new JFrame("Yard");
        frame.setContentPane(this);
        frame.setLocation(200, 200);
        frame.setSize(WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Color c = g.getColor();
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, HEIGHT * BLOCK_SIZE, WIDTH * BLOCK_SIZE);
        g.setColor(c);
        g.fillOval(WIDTH, HEIGHT, 100, 100);
    }

    public static void main(String Args[]) {
        new Yard().launch();
    }
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню