简体   繁体   中英

Frozen Java Jcomponent

I've got a problem with my Component in Java. The problem is that in my GUI i can't see any changes of my ovals colors. When the OVF flag is set to false they should be white, and when the flag OVF is set to true, they should be red. But when i starting my program, the flag OVF is set to fasle and all of ovals are white - that's good. When the flag is changing to true, the ovals are still white. I try to add repaint() function, but the white ovals are still blinking, without change color. Here is my code of class Komponent:

import java.awt.*;
import javax.swing.*;

public class Komponent extends JComponent {

    Counter counter3;

    public Komponent() {
        counter3 = new Counter();
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D dioda = (Graphics2D) g;
        int x1 = 85;
        int x2 = 135;
        int x3 = 35;
        int x4 = 185;
        int x5 = 235;
        int x6 = 88;
        int x7 = 138;
        int x8 = 38;
        int x9 = 188;
        int x10 = 238;
        int y_ob = 0;
        int y = 3;
        int width_ob = getSize().width / 9;
        int height_ob = getSize().height - 1;
        int width = (getSize().width / 9) - 6;
        int height = (getSize().height - 1) - 6;
        if (counter3.OVF == true) {
            dioda.setColor(Color.BLACK);
            dioda.fillOval(x1, y_ob, width_ob, height_ob);
            dioda.fillOval(x2, y_ob, width_ob, height_ob);
            dioda.fillOval(x3, y_ob, width_ob, height_ob);
            dioda.fillOval(x4, y_ob, width_ob, height_ob);
            dioda.fillOval(x5, y_ob, width_ob, height_ob);
            dioda.setColor(Color.RED);
            dioda.fillOval(x6, y, width, height);
            dioda.fillOval(x7, y, width, height);
            dioda.fillOval(x8, y, width, height);
            dioda.fillOval(x9, y, width, height);
            dioda.fillOval(x10, y, width, height);
            repaint();
        }
        if (counter3.OVF == false) {
            dioda.setColor(Color.BLACK);
            dioda.fillOval(x1, y_ob, width_ob, height_ob);
            dioda.fillOval(x2, y_ob, width_ob, height_ob);
            dioda.fillOval(x3, y_ob, width_ob, height_ob);
            dioda.fillOval(x4, y_ob, width_ob, height_ob);
            dioda.fillOval(x5, y_ob, width_ob, height_ob);
            dioda.setColor(Color.WHITE);
            dioda.fillOval(x6, y, width, height);
            dioda.fillOval(x7, y, width, height);
            dioda.fillOval(x8, y, width, height);
            dioda.fillOval(x9, y, width, height);
            dioda.fillOval(x10, y, width, height);
            repaint();
        }
    }

    public static void main(String[] arg) {
        new Komponent();
    }
}

Please, help (sorry for my English) ;)

  1. override getPreferredSize , then coordinates will be getHeight / Weight

  2. Custom painting in Swing is done by using public void paintComponent(Graphics g) { instead of public void paint(Graphics g) {

  3. first code line inside paintComponent should be super.paintComponent(g);

  4. start Swing Timer for periodical repaint() if needed

  5. no reason to create all elements on runtime, put all element to the array (two arrays in your case), inside paintComponent only loop inside array

EDIT (rest is up to you, point 1st and 5th)

在此处输入图片说明 . . 在此处输入图片说明

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class Komponent {

    private boolean counter3 = false;
    private JFrame frame = new JFrame();
    private Timer timer;
    private CustomComponents cc1 = new CustomComponents();

    public Komponent() {
        counter3 = false;
        frame = new JFrame();
        frame.setLayout(new GridLayout(1, 1, 10, 10));
        frame.add(cc1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
        timer = new javax.swing.Timer(1500, updateCol());
        timer.setRepeats(true);
        timer.start();
    }

    private Action updateCol() {
        return new AbstractAction("Hello World") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                boolean bol = counter3;
                if (bol) {
                    counter3 = false;
                    cc1.repaint();
                } else {
                    counter3 = true;
                    cc1.repaint();
                }
            }
        };
    }

    class CustomComponents extends JComponent {

        private static final long serialVersionUID = 1L;

        @Override
        public Dimension getMinimumSize() {
            return new Dimension(100, 100);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 200);
        }

        @Override
        public Dimension getMaximumSize() {
            return new Dimension(300, 300);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D dioda = (Graphics2D) g;
            int x1 = 85;
            int x2 = 135;
            int x3 = 35;
            int x4 = 185;
            int x5 = 235;
            int x6 = 88;
            int x7 = 138;
            int x8 = 38;
            int x9 = 188;
            int x10 = 238;
            int y_ob = 0;
            int y = 3;
            int width_ob = getSize().width / 9;
            int height_ob = getSize().height - 1;
            int width = (getSize().width / 9) - 6;
            int height = (getSize().height - 1) - 6;
            if (counter3) {
                dioda.setColor(Color.BLACK);
                dioda.fillOval(x1, y_ob, width_ob, height_ob);
                dioda.fillOval(x2, y_ob, width_ob, height_ob);
                dioda.fillOval(x3, y_ob, width_ob, height_ob);
                dioda.fillOval(x4, y_ob, width_ob, height_ob);
                dioda.fillOval(x5, y_ob, width_ob, height_ob);
                dioda.setColor(Color.RED);
                dioda.fillOval(x6, y, width, height);
                dioda.fillOval(x7, y, width, height);
                dioda.fillOval(x8, y, width, height);
                dioda.fillOval(x9, y, width, height);
                dioda.fillOval(x10, y, width, height);
            } else {
                dioda.setColor(Color.BLACK);
                dioda.fillOval(x1, y_ob, width_ob, height_ob);
                dioda.fillOval(x2, y_ob, width_ob, height_ob);
                dioda.fillOval(x3, y_ob, width_ob, height_ob);
                dioda.fillOval(x4, y_ob, width_ob, height_ob);
                dioda.fillOval(x5, y_ob, width_ob, height_ob);
                dioda.setColor(Color.WHITE);
                dioda.fillOval(x6, y, width, height);
                dioda.fillOval(x7, y, width, height);
                dioda.fillOval(x8, y, width, height);
                dioda.fillOval(x9, y, width, height);
                dioda.fillOval(x10, y, width, height);
            }
            dioda.dispose();
        }
    }

    public static void main(String[] arg) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Komponent komponent = new Komponent();
            }
        });
    }
}

You call repaint() inside your paint()-method? which calls the paint() method again, which may call repaint() again, and so on....

Not sure, if this is your problem, but i think you call repaint at the wrong place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM