//package mci;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.GeneralPath;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.*;
import java.awt.Color;
import java.awt.Robot;
public class Cross extends JFrame{
private static final long serialVersionUID = 4952421515715804191L;
static private int positionPointerX = 0; //Position X des Mauszeigers
static private int positionPointerY = 0; //Position Y des Mauszeigers
//Initialisierung
public Cross()
{
super("Kreuz");
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0, 0, 800, 600);
setLocation((int)dim.getWidth()/2 - getWidth()/2, (int)dim.getHeight()/2 - getHeight()/2);
setBackground(Color.white);
setForeground(Color.black);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
//Zeichnen
public void paint(Graphics g1D)
{
//try { //aufgabe d
Graphics2D g = (Graphics2D) g1D;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.clearRect(0, 0, getWidth(), getHeight());
//Position der Maus im Bildschirm
g.setFont(new Font("Tahoma", Font.BOLD,64));
g.drawString(positionPointerX+" "+positionPointerY, 250, 550);
Graphics2D g2d = (Graphics2D) g;//start a
GeneralPath path = new GeneralPath(GeneralPath.WIND_NON_ZERO);
path.moveTo(100, 50);
path.lineTo(150, 50);
path.lineTo(150, 100);
path.lineTo(200, 100);
path.lineTo(200, 150);
path.lineTo(150, 150);
path.lineTo(150, 200);
path.lineTo(100, 200);
path.lineTo(100, 150);
path.lineTo(50, 150);
path.lineTo(50, 100);
path.lineTo(100, 100);
path.closePath();//end a
GradientPaint redtowhite = new GradientPaint(100, 50, Color.red, 150, 200,
Color.blue);
g2d.setPaint(redtowhite); //B
g2d.fill(path);
g2d.setPaint(Color.green); // C
g2d.setStroke(new BasicStroke(4.0f));//C
g2d.draw(path);
/* Robot robot = new Robot(); //Aufgabe D
Color color = (robot.getPixelColor(positionPointerX, positionPointerY));
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
Color farbe = new Color(red,green,blue);
g2d.setPaint(farbe); // D
g2d.setStroke(new BasicStroke(4.0f));//D
g2d.draw(path);
*/
}
/* catch (AWTException e) { ((aufgabe D
e.printStackTrace();
}
}*/
//Hauptschleife (Update alle 1/60 Sekunden)
public static void main(String[] args)
{
Cross window = new Cross();
while(true)
{
try
{
Thread.sleep(1000/60);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
Point pointerPos = pointerInfo.getLocation();
positionPointerX = pointerPos.x;
positionPointerY = pointerPos.y;
window.repaint();
}
}
}
嘿伙计们, 所以对于一些学习练习,我想绘制一个十字架,边框根据鼠标指向的颜色进行绘制,但它仍保持绿色。让我的代码删除注释块。 不知怎的,我无法画出机器人得到的颜色,或者我无法更新它,我不知道。 如果我关闭窗口它实际上是从击中x按钮变红,但正如我说...我不知道。 我真的很感激一些评论的帮助。 亲切的问候