有代码,一个是动物园,另一个是动物(老虎和鹰现在是相同的代码,所以我只写Tiger.java)。 我想在背景图像上绘制可拖动的JComponent。可拖动的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());
}
}