Java 在屏幕帮助中保留形状,可以';我不知道如何跟踪X,Y坐标
我试了又试,我查了很多在屏幕上保持形状的例子,但似乎不能适应我的代码。总之,左键单击打印正方形,右键单击打印圆形。我想用正方形(矩形)和圆形填充窗口。任何帮助和解释,使我可以学习的概念将是伟大的。我知道我必须跟踪坐标,也许在一个循环中,但似乎可以让它工作。再次感谢Java 在屏幕帮助中保留形状,可以';我不知道如何跟踪X,Y坐标,java,swing,Java,Swing,我试了又试,我查了很多在屏幕上保持形状的例子,但似乎不能适应我的代码。总之,左键单击打印正方形,右键单击打印圆形。我想用正方形(矩形)和圆形填充窗口。任何帮助和解释,使我可以学习的概念将是伟大的。我知道我必须跟踪坐标,也许在一个循环中,但似乎可以让它工作。再次感谢 import java.awt.*; import javax.swing.JFrame; import java.awt.event.MouseListener; import java.awt.event.MouseEvent;
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();
}
}
我建议创建两个类
1) Circle class
2) Square Class
这些类将存储您需要的信息,如X、y等
初始化存储这些对象的数组列表&在paint方法中从中读取,然后像在代码中一样继续绘制它们
(在单击事件中,您只需创建新对象(圆/正方形)并将其添加到数组列表中即可)因此,我了解到目前为止您的代码是如何工作的:用户左键单击,记录这些坐标,并在屏幕上以这些坐标呈现一个正方形。 当我们单击时,坐标将更新,并且在下一次绘制时,正方形将移动到新位置 你在正确的轨道上需要一个环路 以下是您需要实现的逻辑: 创建一个ArrayList作为成员变量。类型可以是
对
对象。所以这个arraylist将保存一个X,Y坐标的列表。此arraylist将如下所示:
ArrayList<pair<int,int>> myRightClickCoords;
myRightClickCoords.insert(new pair<int,int>(e.getX(),e.getY()));
ArrayList myRightClickCoords;
创建该列表后,每次用户单击时,记录单击坐标并将其插入arraylist。看起来是这样的:
ArrayList<pair<int,int>> myRightClickCoords;
myRightClickCoords.insert(new pair<int,int>(e.getX(),e.getY()));
myRightClickCoords.insert(新对(e.getX(),e.getY());
然后,将其添加到代码中后,在draw函数中,您可以查看整个myRightClickCoords
列表,并为每组坐标运行drawRect
一旦你开始工作,你可以做同样的事情左键点击和圆圈。祝你好运 首先,从通读开始:
JFrame
最后
在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.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Font;
导入java.awt.FontMetrics;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Point;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
JFrame=新JFrame();
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
私人名单圈;
私人名单广场;
公共测试窗格(){
圆圈=新的ArrayList();
squares=新的ArrayList();
addMouseListener(新的MouseAdapter(){
@凌驾
公共无效MouseEvent(MouseEvent e){
if(SwingUtilities.isLeftMouseButton(e)){
添加(例如getPoint());
}else if(摆动装置。isRightMouseButton(e)){
添加(例如getPoint());
}
重新油漆();
}
});
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200200);
}
@凌驾
受保护组件(图形g){
超级组件(g);
//我很挑剔
Graphics2D g2d=(Graphics2D)g.create();
g2d.setColor(Color.RED);
对于(点p:圆){
g2d.drawOval(p.x,p.y,10,10);
}
g2d.setColor(Color.BLUE);
用于(点p:正方形){
g2d.drawRect(p.x,p.y,10,10);
}
g2d.setFont(新字体(“TAHOMA”,Font.BOLD,12));
g2d.setColor(Color.BLUE);
FontMetrics fm=g2d.getFontMetrics();
String text=“鼠标按钮测试仪”;
intx=getWidth()-fm.stringWidth(text)-10;
int y=getHeight()-(fm.getAscent()-fm.getHeight())-10;
g2d.抽绳(文本,x,y);
g2d.dispose();
}
}
}
在用鼠标左键或右键单击的位置以及paint()
/draw()
方法中保留一个列表,您可以阅读此列表并绘制圆/正方形。draw方法中的if-else分支可能无法达到预期效果。括号放错地方了。第二个if
语句仅适用于window.setColor
,而window.drawOval
每次都被调用。