请帮忙做一个java的简单的窗口程序

写一个读取图片的小程序显示一个jpg格式的图片,图片从本地电脑上读取把本地电脑上的账户的姓名资料,产生浮水印加在图片上题不难的,就想要个大致的模板。写得好的话追加!只要求... 写一个读取图片的小程序
显示一个jpg格式的图片,图片从本地电脑上读取
把本地电脑上的账户的姓名资料,产生浮水印加在图片上
题不难的,就想要个大致的模板。写得好的话追加!只要求能够在窗口中显示图片即可。急!
展开
 我来答
百度网友845f74e61
2012-01-04 · TA获得超过6929个赞
知道大有可为答主
回答量:4050
采纳率:50%
帮助的人:1629万
展开全部
显示图片
水印我还真不知道怎么做,如果你知道可以提供一下方法。
我现在去查一下。
--------------------------------------------------------------------------------------------------
import java.awt.Graphics;

public class AppImg extends JFrame implements ActionListener {

File file = null;

public AppImg() {

setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);

JPanel panel = new ImagePanel();
panel.setBounds(12, 40, 370, 218);
getContentPane().add(panel);

JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12, 9, 91, 21);
getContentPane().add(btnBrowse);
setVisible(true);
}

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

public void actionPerformed(ActionEvent e) {

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
// 设置文件类型
chooser.setFileFilter(filter);
// 打开选择器面板
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
repaint();
}
}

class ImagePanel extends JPanel {

public void paint(Graphics g) {
super.paint(g);

if (file != null) {
ImageIcon icon = new ImageIcon(file.getPath());
g.drawImage(icon.getImage(), 0, 0, 200, 200, this);
}
}
}
}
hardneedl
推荐于2016-03-11 · TA获得超过921个赞
知道小有建树答主
回答量:1353
采纳率:57%
帮助的人:607万
展开全部
import javax.imageio.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
/**
* @author Hardneedl
*/
final class WaterMarkDemo extends JFrame {
public String getTitle() {return "WaterMarkDemo";}
static private final Dimension size = new Dimension(600, 400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize() {return size;}

private MarkBrush markBrush;
private JTextField textField;
private ChangeObservable observable;
private _Canvas canvas;
WaterMarkDemo() throws HeadlessException {
init();
handleEvents();
lay();
}

private void init(){
canvas = new _Canvas();
observable = new ChangeObservable();
observable.addObserver(new BrushObserver());

canvas.addBrush( markBrush = new MarkBrush() );
textField=new JTextField(40);

}

private void lay(){
JPanel p = new JPanel();
p.add(new JButton(new ImageChooseAction()));
p.add(textField);
Container container = getContentPane();
container.add(p,BorderLayout.NORTH);
container.add(canvas,BorderLayout.CENTER);
pack();
setVisible(true);
}

private void handleEvents(){
textField.getDocument().addDocumentListener(new TextChangeProcessor());
}

/*画布*/
private class _Canvas extends JComponent{
private java.util.List<Brush>brushList=new ArrayList<Brush>();
private void addBrush(Brush b){
if(b!=null)
brushList.add(b);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(Brush b:brushList)
b.doPaint(g,this);
}
public Border getBorder() {return BorderFactory.createLineBorder(Color.BLACK,2);}
}

/*画笔接口*/
private interface Brush{void doPaint(Graphics g,ImageObserver imgObserver);}

/*水印画笔*/
private class MarkBrush implements Brush{
private Image img;
private String s1;
private void setImage(Image g){img=g;}
private void setString(String s){s1=s;}
public void doPaint(Graphics g,ImageObserver imgObserver) {
Graphics gg = g.create();
if (img!=null)
gg.drawImage(img,0,0,imgObserver);

if (s1!=null){
gg.setColor(Color.YELLOW);
gg.setFont(gg.getFont().deriveFont(20f));
gg.drawString(s1,100,100);
}
gg.dispose();
}
}

/*文件选择动作*/
private class ImageChooseAction extends AbstractAction {
private JFileChooser chooser;
private ImageChooseAction() {
super("Select JPEG ...");
putValue(Action.MNEMONIC_KEY,new Integer('S'));
chooser = new JFileChooser(".");
chooser.addChoosableFileFilter(new FileNameExtensionFilter("图片文件", "jpg","png"));
}
public void actionPerformed(ActionEvent e) {
if (chooser.showOpenDialog((Component)e.getSource())==JFileChooser.APPROVE_OPTION) {
File f= chooser.getSelectedFile();
if (f==null)return;
try {
markBrush.setImage(ImageIO.read(f));
observable.notifyObservers(markBrush);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}

private class TextChangeProcessor implements DocumentListener{
private void process(DocumentEvent e) throws BadLocationException {
javax.swing.text.Document d=e.getDocument();
markBrush.setString(d.getText(0,d.getLength()));
observable.notifyObservers(markBrush);
}
public void changedUpdate(DocumentEvent e) {
try {
process(e);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}

public void insertUpdate(DocumentEvent e) {
try {
process(e);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}

public void removeUpdate(DocumentEvent e) {
try {
process(e);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}

private class ChangeObservable extends Observable{
public void notifyObservers() {
setChanged();
super.notifyObservers();
}

public void notifyObservers(Object arg) {
setChanged();
super.notifyObservers(arg);
}
}
/*画笔内容改变的观察者*/
private class BrushObserver implements Observer{
public void update(Observable o, Object arg) {
if (arg instanceof MarkBrush){
canvas.repaint();
}
}
}

public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {new WaterMarkDemo();}
});
}
}
追问
这个跟我自己写的差别比较大,能不能看看我写的有什么问题,我想先显示图片,水印还不用加上去,现在的问题是我在JFrame中加入了滚动条: scroll = new JScrollPane(panel); this.add(scroll); 为什么不显示这个滚动条呢?加分不是问题,谢谢了!
看消息。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式