gpt4 book ai didi

java - 在屏幕帮助上保留形状,无法弄清楚如何跟踪 X、Y 坐标

转载 作者:行者123 更新时间:2023-12-02 08:53:05 26 4
gpt4 key购买 nike

我已经尝试了很多次,我查找了很多将形状保留在屏幕上的示例,但似乎无法适应我的代码。在摘要中,左键单击打印正方形,右键单击打印圆形。我想用正方形(矩形)和圆形填充窗口。任何帮助和解释以便我可以学习这个概念都会很棒。我知道我必须跟踪坐标,也许是在循环中,但似乎可以让它工作。再次感谢。

import java.awt.*;
import javax.swing.JFrame;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class MouseButtonTester extends JFrame implements MouseListener
{
private int mouseX, mouseY;
private int mouseButton;
private boolean isFirstRun;
private static final int WIDTH = 640;
private static final int HEIGHT = 480;
private static final long serialVersionUID = 0; //use this if you do not like warnings

public MouseButtonTester() //constructor
{
super("Mouse Button Tester");

//set up all variables
mouseX = mouseY = 0;
mouseButton = 0;
isFirstRun = true;

//set up the Frame
setSize(WIDTH,HEIGHT);
setBackground(Color.WHITE);
setVisible(true);

//start trapping for mouse clicks
addMouseListener(this);
}

public void mouseClicked(MouseEvent e)
{
mouseX=e.getX(); //Tracks x coordinates
mouseY=e.getY(); //Tracker y coordinates

mouseButton = e.getButton(); //gets button number



repaint();


}

public void paint( Graphics window ) // Draws the Window
{
if(isFirstRun)
{
window.setColor(Color.WHITE);
window.fillRect(0,0,WIDTH, HEIGHT);

//change isFirstRun

}

window.setFont(new Font("TAHOMA",Font.BOLD,12));
window.setColor(Color.BLUE);
window.drawString("MOUSE BUTTON TESTER", 420,55);

draw(window);


}

public void draw(Graphics window)
{
if(mouseButton==MouseEvent.BUTTON1) //left mouse button pressed
{
//window.drawString("BUTTON1", 50,200); //debug code
window.setColor(Color.RED);
window.drawRect(mouseX,mouseY,10,10);



}
//right mouse button pressed
{
if (mouseButton == MouseEvent.BUTTON2)

window.setColor(Color.BLUE);
window.drawOval(mouseX,mouseY,10,10);


}


//any other mouse button pressed
{



}
}




public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) { }
}

------ Main Method --------------
public class MouseButtonTesterRunner
{
public static void main(String[] args)


{ MouseButtonTester prog = new MouseButtonTester();




}


}

最佳答案

首先,从通读开始:

这样您就可以了解 Swing 中的绘画是如何工作的、如何使用它以及这样做时您的责任。

接下来,通读一下:

为什么您应该避免覆盖顶级容器(例如 JFrame)的 paint

最后...

在 Swing 中绘制是破坏性的,也就是说,每次绘制组件时,您都应该从头开始完全重新绘制组件状态。

为了实现您的目标,您需要维护要绘制的项目的缓存。

这个概念本身非常困难,但过程中可能会遇到一些“陷阱”

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private List<Point> circles;
private List<Point> squares;

public TestPane() {
circles = new ArrayList<>();
squares = new ArrayList<>();

addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
circles.add(e.getPoint());
} else if (SwingUtilities.isRightMouseButton(e)) {
squares.add(e.getPoint());
}
repaint();
}
});
}

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

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// I'm picky
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
for (Point p : circles) {
g2d.drawOval(p.x, p.y, 10, 10);
}
g2d.setColor(Color.BLUE);
for (Point p : squares) {
g2d.drawRect(p.x, p.y, 10, 10);
}

g2d.setFont(new Font("TAHOMA", Font.BOLD, 12));
g2d.setColor(Color.BLUE);

FontMetrics fm = g2d.getFontMetrics();
String text = "MOUSE BUTTON TESTER";

int x = getWidth() - fm.stringWidth(text) - 10;
int y = getHeight() - (fm.getAscent() - fm.getHeight()) - 10;

g2d.drawString(text, x, y);
g2d.dispose();
}

}
}

关于java - 在屏幕帮助上保留形状,无法弄清楚如何跟踪 X、Y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60661980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com