Home>

This program draws a rectangle by creating a frame, setting the background color, obtaining coordinates with a mouse listener.
The error can be executed without any particular error, but even if the frame is enlarged, the drawing range does not change, and once it is minimized, the frame becomes black when redisplayed. Also, it will be white regardless of the drawing color.
I think that paint () is probably not processed well, but if I do the same program on my laptop (Windows10Home), it will be in the state I just wrote, but I remember only that it was a school computer (Windows) It was possible to execute both expansion and minimization without problems.
Please advise what causes paint () to go wrong.

package exercise 08;
import javax.swing.JFrame;
public class kadai08rame {
    public static void main (String [] args) {
        kadai08DS frame = new kadai08DS ();
        frame.setSize (360,400);
        frame.setTitle ("event processing");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setVisible (true);
    }
}
---
package exercise 08;
import java.awt.Color;
import java.awt.Graphics;
public class kadai08S {
    private int x1, y1, x2, y2;
    public kadai08S (int x1, int y1, int x2, int y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }
    public void draw (Graphics g) {
        g.setColor (Color.BLACK);
        g.drawRect (x1, y1, x2-x1, y2-y1);
    }
}
---
    package exercise 08;
    import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JFrame;
    public class kadai08DS extends JFrame implements MouseListener {
        private int x1, y1, x2, y2;
        ArrayList array;
        private int i;
        public kadai08DS () {
            x1 = 0;
            y1 = 0;
            x2 = 0;
            y2 = 0;
            setBackground (Color.CYAN);
            array = new ArrayList ();
            addMouseListener (this);
        }
        public void paint (Graphics g) {
            for (i = 0;i
  • Answer # 1

    There seem to be cases where it doesn't work if the environment you use changes, and even professional engineers often have trouble with it. It seems that this problem cannot be solved without understanding more about the various PCs.
    Thank you for answering.

  • Answer # 2

    As you said immediately after startup, once you draw a rectangle with the mouse, it will not turn black even if you change the size of the window.

  • Answer # 3

    There seems to be such an error often when the environment changes. I knew that I needed to be very familiar with computers.
    Thank you for answering.