1

JFrame の水平方向の半分を設定して、カーソルが置かれている色を表示しようとしています。これは私がこれまでに得たものです。

enter code here
package finalproject;

import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.PointerInfo;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.event.MouseMotionListener;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.*;
public class FinalProject {
    public static void main(String[] args) throws AWTException {
        JFrame frame = new JFrame();
        JLabel rgbLabel = new JLabel();
        rgbLabel.setVerticalAlignment(SwingConstants.NORTH);
        rgbLabel.setHorizontalAlignment(SwingConstants.CENTER);
        frame.setLocationRelativeTo(null);
        frame.add(rgbLabel);
        rgbLabel.setAlignmentX(50);
        rgbLabel.setAlignmentY(10);
        rgbLabel.setVisible(true);
        frame.setLocation(650, 350);
        frame.setVisible(true);
        frame.pack();
        frame.setTitle(display.TITLE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(display.WIDTH, display.HEIGHT);
        frame.setResizable(true);
        frame.setVisible(true);
        while (true){
            PointerInfo cursorLocation = MouseInfo.getPointerInfo();
            Point position = cursorLocation.getLocation(); 
            int x = (int)position.getX(); 
            int y = (int)position.getY();
            Robot robot = new Robot();
            Color pixelColor = robot.getPixelColor(x, y);
            int colorRed = pixelColor.getRed(); 
            int colorGreen = pixelColor.getGreen(); 
            int colorBlue = pixelColor.getBlue();
            rgbLabel.setText("Red: " + colorRed + " Green: " + colorGreen + " Blue: "     + colorBlue + "\n" );

            }
        }
    public static class display {
        public static final int WIDTH = 250;
        public static final int HEIGHT = 135;
        public static final String TITLE = "Colorblind Assistant";
    }
}

私はそれを置くことを知っています

frame.getContantPane().setBackground(pixelColor);

while ループの下では、フレーム全体がその色に変更されますが、フレームの下半分だけをその色にする必要があります。

いくつかの助けをいただければ幸いです。ありがとうございました。

4

1 に答える 1

2
  • while (true)Swing イベント スレッドを結び付ける ではなく、MouseListener を使用します。
  • すべてのコードをメイン メソッドから適切なクラスに取得します。main メソッドは、プログラムを起動する以外に何もするべきではありません。
  • JFrame に GridLayout に 2 つの JPanel を保持させ、JPanel の 1 つのバックグラウンドで MouseListener によって設定します。
于 2013-11-27T22:50:11.590 に答える