高分求代码,熟悉图形化界面的高手请!!分不够再加!!

smi_ling 2010-08-26 03:45:21
要求用:Graphics和Image画图
面板要求有背景图,另外还有一张位于面板背景图上可移动的小图
获取图片位置时用this.getClass().getResource("")来获得!!!
...全文
164 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqyangfeng 2010-08-26
  • 打赏
  • 举报
回复
java,我学习
彩票虫虫 2010-08-26
  • 打赏
  • 举报
回复
我是来接分的。
xingyue2006 2010-08-26
  • 打赏
  • 举报
回复
学习一下。
smi_ling 2010-08-26
  • 打赏
  • 举报
回复
回7#
必要的容器还是要用的噻!!
思路是用Graphics和Image画图再和this.getClass().getResource("")
hy158753228 2010-08-26
  • 打赏
  • 举报
回复
我叉, LZ的意思是说 不用另一个容器盛放在上面的那个图片?
cw_tkong 2010-08-26
  • 打赏
  • 举报
回复
关注中
china__java 2010-08-26
  • 打赏
  • 举报
回复
这个貌似我群里有人有QQ群96040430
smi_ling 2010-08-26
  • 打赏
  • 举报
回复
3#的哥!!
如何用Graphics和Image画图再和this.getClass().getResource("")联系一起的话可以不!!
closewbq 2010-08-26
  • 打赏
  • 举报
回复
刚试验了好一会,惭愧呀,你随便找个图片放在和你类一样的目录下。
然后就可以拖动图片了。
是不是这样的效果。
closewbq 2010-08-26
  • 打赏
  • 举报
回复

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;
/**
* @author closewbq
* 拖动图片
*/
public class DragImage extends JFrame {
private JLabel imageLabel;
private JPanel jpanel;

public DragImage() {
URL path = DragImage.class.getResource("xxxx.jpg");
ImageIcon image = new ImageIcon(path);
jpanel = new PanelImage(image);
imageLabel = new JLabel(image);
jpanel.setSize(900, 600);
jpanel.add(imageLabel);
setLayout(null);
this.getContentPane().add(jpanel);
DragListener listener = new DragListener();
imageLabel.addMouseListener(listener);
imageLabel.addMouseMotionListener(listener);
setTitle("图片拖动");
setSize(900, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

class DragListener extends MouseInputAdapter {
Point point = new Point(0, 0);
public void mouseDragged(MouseEvent e) {
Point newPoint = SwingUtilities.convertPoint(imageLabel, e
.getPoint(), imageLabel.getParent());
//新位置
imageLabel.setLocation(imageLabel.getX() + (newPoint.x - point.x),
imageLabel.getY() + (newPoint.y - point.y));
//更改坐标点
point = newPoint;
}

public void mousePressed(MouseEvent e) {
point = SwingUtilities.convertPoint(imageLabel, e.getPoint(),
imageLabel.getParent());
System.out.println(e.getPoint().x + " : " + e.getPoint().y);
System.out.println(point.x + " : " + point.y);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DragImage();
}
});

}

class PanelImage extends JPanel {
BorderLayout borderLayout = new BorderLayout();
private ImageIcon image = null;

public PanelImage(ImageIcon image) {
try {
this.image = image;
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
this.setLayout(borderLayout);
}

protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
g.drawImage(image.getImage(), 0, 0, null, null);

}
}
}
smi_ling 2010-08-26
  • 打赏
  • 举报
回复
改一下!!
只要获得图片位置时是用this.getClass().getResource("")实现的就行!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧