gpt4 book ai didi

java - 我想在背景图像上绘制可拖动的 JComponent

转载 作者:行者123 更新时间:2023-12-01 12:52:55 24 4
gpt4 key购买 nike

有代码,一个是动物园,另一个是动物(老虎和鹰现在是相同的代码,所以我只写Tiger.java)。我想在背景图像上绘制可拖动的 JComponent。 Draggable JComponent 是在框架上工作的,而不是图像上。首先,我单击图像然后绘制我的动物,其次,在我绘制许多动物后,我可以将我的动物拖放到图像上的任何位置。请教我如何画画。

<小时/>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

class BgPanel extends JPanel
{
Image bg = new ImageIcon("c:\\back.jpg").getImage();
public void paintComponent(Graphics g)
{
g.drawImage(bg,0,0,getWidth(),getHeight(),this);
}
}

public class Zoo extends JFrame implements ActionListener
{
Container c;
JFrame frame = new JFrame();
JComboBox comBox = new JComboBox();
drawCanvas can = new drawCanvas();
saving_drawing saving=new saving_drawing();
Tiger tiger;
Eagle eagle;
Point point;

ArrayList<saving_drawing> arr_saving = new ArrayList();

Zoo()
{
JPanel bgPanel = new BgPanel();
bgPanel.setLayout(new BorderLayout());
comBox.addItem("Tiger");
comBox.addItem("Eagle");
comBox.addActionListener(this);

frame.setContentPane(bgPanel);

bgPanel.add(comBox, BorderLayout.SOUTH);
bgPanel.add(can, BorderLayout.CENTER);


frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Zoo");
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if("Tiger".equals(comBox.getSelectedItem()))
{
saving.drawChoice=1;
}
else if("Eagle".equals(comBox.getSelectedItem()))
{
saving.drawChoice=2;
}

}
class drawCanvas extends JComponent
{
public drawCanvas()
{
this.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
count++;
if(count!=0)
{
saving.point=e.getPoint();

arr_saving.add(new saving_drawing(saving.drawChoice,saving.point));
repaint();
System.out.println("click");
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
});
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;

for(int i=0;i<count;i++)
{
saving.drawChoice=arr_saving.get(i).drawChoice;
saving.point=arr_saving.get(i).point;

if(saving.drawChoice==1)
{
tiger=new Tiger(saving.point);
//frame.add(tiger);
//frame.setVisible(true);
//tiger.draw(g2);
//can.add(tiger);
tiger.paintComponent(g2);
//I'don't know what to do for drawing my tiger
}
if(saving.drawChoice==2)
{
eagle=new Eagle(saving.point);
eagle.draw(g2);
}

}

}
}
class saving_drawing
{
int drawChoice;

Point point;
saving_drawing(){}
saving_drawing(int dc, Point p)
{
this.drawChoice = dc;
this.point=p;
}
}
public static void main(String [] args)
{
Zoo test = new Zoo();
}
}
<小时/>
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
public class Tiger extends JComponent implements MouseListener
{
private boolean draggable = true;

protected Cursor draggingCursor = Cursor
.getPredefinedCursor(Cursor.HAND_CURSOR);
protected boolean overbearing = false;

save_drawing save = new save_drawing();

Tiger(Point point)
{

save.point=point;


addMouseListener(this);
addDragListeners();
setOpaque(true);

}
private void addDragListeners()

{


final Tiger handle = this;

addMouseMotionListener(new MouseAdapter()

{
public void mouseMoved(MouseEvent e)

{

save.point = e.getPoint();

setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

}
public void mouseDragged(MouseEvent e)

{

int anchorX = save.point.x;

int anchorY = save.point.y;
Point parentOnScreen = getParent().getLocationOnScreen();

Point mouseOnScreen = e.getLocationOnScreen();

Point position = new Point(mouseOnScreen.x - parentOnScreen.x

- anchorX, mouseOnScreen.y - parentOnScreen.y - anchorY);

setLocation(position);

if (overbearing)

{

getParent().setComponentZOrder(handle, 0);
System.out.println("check");
repaint();

}

}

});

}

public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);

if (isOpaque())

{
g.setColor(Color.black);
g.fillRect(save.point.x, save.point.y, 50, 50);
}

}
class save_drawing
{
Point point;


}
public void mouseClicked(MouseEvent e)
{

save.point=e.getPoint();

repaint();
}
public void mouseEntered(MouseEvent arg0){}
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
private void removeDragListeners()

{
for (MouseMotionListener listener : this.getMouseMotionListeners())
{
removeMouseMotionListener(listener);
}
setCursor(Cursor.getDefaultCursor());
}


}

最佳答案

要在面板周围拖动组件,您可以使用 Component Mover .

关于java - 我想在背景图像上绘制可拖动的 JComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24087108/

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