java简单程序代码照片 java编程图片
JAVA中如何插入图片?要用什么代码?不用小程序实现的!!!!
//本程序演示媒体跟踪器的用法,从图像中剪切(抓取)部分图像的方法。
我们提供的服务有:网站设计、网站建设、微信公众号开发、网站优化、网站认证、铜鼓ssl等。为成百上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的铜鼓网站制作公司
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
class ImagePanelB extends JPanel{
private Image image, blockImage;
public ImagePanelB(){
MediaTracker mt = new MediaTracker(this); //创建媒体跟踪器mt
image = Toolkit.getDefaultToolkit().getImage("resource.gif");
mt.addImage(image,0); //将image列入0号编组并跟踪image的下载过程
try{ mt.waitForID(0); //等待图像组编号为0的所有图像下载完成
}catch(InterruptedException e){e.printStackTrace();}
int imageW =image.getWidth(this); //获取图像宽度,单位为像素
int imageH =image.getHeight(this); //获取图像高度,单位为像素
System.out.println("图像的宽="+imageW+" 图像的高="+imageH);
//准备从image中剪出宽为w,高为h的一块图像
int w=imageW/2; int h=imageH/2;
PixelGrabber pg=null; //创建像素抓取器
MemoryImageSource ip=null; //创建内存图像源
Toolkit tool = getToolkit(); int pixels[]= new int[w*h];
/*建立像素抓取器pg。从image中抓取矩形区域中的像素。矩形区域左上角顶点坐标为(0,0),矩形宽w,矩形高h。w,h的单位是像素数。抓取到的像素存于整数数组pixels中。第7个参数是数组pixels中存储第1个像素的下标,通常为0。第8个参数是所抓取的矩形区域的一行包含的像素个数。*/
pg = new PixelGrabber(image,0,0,w,h,pixels,0,w);
try{
pg.grabPixels(); //开始图像的生产(抓取像素并存入pixels)
ip=new MemoryImageSource(w,h,pixels,0,w);
blockImage=tool.createImage(ip);
} catch(InterruptedException ie){}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image, 40, 20, this);
g.drawImage(blockImage, 240, 20, this);
}
}
class ImageFrameB extends JFrame{
public ImageFrameB(){
setTitle("LoadingImageB"); setSize(440, 200);
addWindowListener(new WindowAdapter(){//内部类的匿名表示形式
public void windowClosing(WindowEvent e){ System.exit(0); }
} );
Container contentPane = getContentPane();
contentPane.add(new ImagePanelB());
}
}
public class LoadingImageB{
public static void main(String[] args){
JFrame frame = new ImageFrameB();
frame.setVisible(true);
}
}
求一个用JAVA写的简单的记事本源代码程序 要有运行结果的截图和源代码,在线等
这个是我以前写着玩的一个例子,可以运行起来看看,有保存,撤销,复制,剪切功能,自己研究研究
package test;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class MyNote {
private JFrame frame = new JFrame("记事本");
private JTextArea text = new JTextArea();
private static boolean flag = false; // 判断是否保存
public MyNote() {
JMenuBar bar = new JMenuBar();
JMenu edit = new JMenu("check");
JMenu check = new JMenu("edit");
JMenu help = new JMenu("help");
JMenuItem term = new JMenuItem("copy");
JMenuItem term1 = new JMenuItem("paste");
JMenuItem term2 = new JMenuItem("cut");
JMenuItem term3 = new JMenuItem("backout");
JMenuItem term4 = new JMenuItem("import");
JMenuItem term5 = new JMenuItem("open");
JMenuItem term6 = new JMenuItem("exit");
JMenuItem term7 = new JMenuItem("save to");
JMenuItem term8 = new JMenuItem("about");
JMenuItem term9 = new JMenuItem("save");
JMenuItem term10 = new JMenuItem("new");
Font font = new Font(null, JFrame.ABORT, 24);
text.setFont(font);
JScrollPane scroll = new JScrollPane(text);
frame.setJMenuBar(bar);
bar.add(edit);
bar.add(check);
bar.add(help);
edit.add(term7);
edit.add(term4);
edit.add(term5);
edit.add(term6);
check.add(term);
check.add(term1);
check.add(term2);
check.add(term3);
help.add(term8);
check.add(term9);
edit.add(term10);
frame.add(scroll);
text.setVisible(false);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setLocation(500, 500);
// 事件注册
MenuListener m = new MenuListener();
term5.addActionListener(m);
NewListener n = new NewListener();
term4.addActionListener(n);
SaveListener s = new SaveListener();
term7.addActionListener(s);
CopyListener c = new CopyListener();
term.addActionListener(c);
PasteListener p = new PasteListener();
term1.addActionListener(p);
CutListener c1 = new CutListener();
term2.addActionListener(c1);
HelpListener h = new HelpListener();
term8.addActionListener(h);
ExitListener e = new ExitListener();
term6.addActionListener(e);
CloseListener c2 = new CloseListener();
frame.addWindowListener(c2);
BackOutListener b = new BackOutListener();
term3.addActionListener(b);
SaveActionListener s1 = new SaveActionListener();
term9.addActionListener(s1);
NewFileListener n1 = new NewFileListener();
term10.addActionListener(n1);
}
private class MenuListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.setVisible(true);
}
}
// 打开新文件
private class NewListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.setText("");
JFileChooser fileChooser = new JFileChooser();
int r = fileChooser.showOpenDialog(frame);
if (r == JFileChooser.APPROVE_OPTION) {
try {
File file = fileChooser.getSelectedFile();
FileReader fr = new FileReader(file);
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
text.append(new String(buf, 0, len));
}
fr.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
// 另存为
private class SaveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser filechoose = new JFileChooser();
int r = filechoose.showSaveDialog(frame);
if (r == JFileChooser.APPROVE_OPTION) {
File file = filechoose.getSelectedFile();
try {
FileWriter writer = new FileWriter(file);
writer.write((String) text.getText());
writer.close();
// 下面方法也可以
/*
* PrintWriter print=new PrintWriter(file);
* print.write(text.getText()); print.close();
*/
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
// 复制
private class CopyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.copy();
}
}
// 粘贴
private class PasteListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.paste();
}
}
// 剪切
private class CutListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
text.cut();
}
}
// 关于主题
private class HelpListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "汪雄辉的无敌记事本");
}
}
private class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "谢谢!");
try {
Thread.sleep(2000);
System.exit(0);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
private class CloseListener implements WindowListener {
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
// 关闭窗口
public void windowClosing(WindowEvent e) {
StringBuffer sb = new StringBuffer();
try {
FileReader fr = new FileReader("未命名文件.txt");
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
sb.append(new String(buf, 0, len));
}
fr.close();
} catch (Exception e1) {
e1.getStackTrace();
}
String s = sb.toString();
if (flag == false || !(text.getText().equals(s))) {
int r = JOptionPane.showConfirmDialog(frame, "你还没有保存,要保存吗?");
if (r == JOptionPane.OK_OPTION) {
JFileChooser filechoose = new JFileChooser();
int r1 = filechoose.showSaveDialog(frame);
if (r1 == JFileChooser.APPROVE_OPTION) {
File file = filechoose.getSelectedFile();
try {
FileWriter writer = new FileWriter(file);
writer.write((String) text.getText());
writer.close();
System.exit(0);
// 下面方法也可以
/*
* PrintWriter print=new PrintWriter(file);
* print.write(text.getText()); print.close();
*/
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
}
} else if (r == JOptionPane.NO_OPTION) {
System.exit(0);
} else {
}
} else
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
}
// 撤销
private class BackOutListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (flag == false)
text.setText("");
else {
try {
FileReader fr = new FileReader("未命名文件.txt");
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
text.setText(new String(buf, 0, len));
}
fr.close();
} catch (IOException e1) {
e1.getStackTrace();
}
}
}
}
// 保存文件
private class SaveActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
flag = true;
FileWriter writer;
try {
writer = new FileWriter("未命名文件.txt");
writer.write((String) text.getText());
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class NewFileListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
new MyNote();
}
}
public static void main(String[] args) {
new MyNote();
}
}
怎么为Java程序添加背景图片代码?
仅仅是给窗口添加背景的话是很简单的,添加上以下语句(自己去添加变量哈):
label = new JLabel(background); //background为ImageIcon
// 把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0, 0, this.getWidth(), this.getHeight());
//添加图片到frame的第二层(把背景图片添加到分层窗格的最底层作为背景)
this.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
jPanel=(JPanel)this.getContentPane();
//设置透明
jPanel.setOpaque(false);
然后你上面那个JPanel p也设置成透明就可以了
java 编一个程序按下按钮(button)实现换下一张照片 我写的 帮改一下
加了这些代码
private static final long serialVersionUID = 1L;
Image imgs[]=new Image[5];
repaint();
改后代码如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class PlayAudio extends Applet implements ActionListener {
private static final long serialVersionUID = 1L;
Image imgs[]=new Image[5];
Button b2;
public void init() {
setLayout(new FlowLayout()); // 使用布局管理器
imgs[0]=getImage(getCodeBase(),"1.jpg");
imgs[1]=getImage(getCodeBase(),"2.jpg");
imgs[2]=getImage(getCodeBase(),"3.jpg");
imgs[3]=getImage(getCodeBase(),"4.jpg");
imgs[4]=getImage(getCodeBase(),"5.jpg");
b2=new Button("下一张"); // 定义Play按钮
b2.addActionListener(this); // 给Play按钮添加一个监听事件
add(b2);
}
private static int count = 0;
public void actionPerformed(ActionEvent e)
{
//if(e.getSource() == b2)
//{
count=++count%5;
repaint();
//}
}
public void paint(Graphics g)
{
g.drawImage(imgs[count],0,40,this);
}
}
==============================
g.drawImage("m"+count,0,40,this);
这个地方需要传进image类型的对象:m"+count是字符串类型
标题名称:java简单程序代码照片 java编程图片
分享URL:http://hbruida.cn/article/doeoeei.html