重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
最简单的java代码肯定就是这个了,如下:
创新互联建站一直秉承“诚信做人,踏实做事”的原则,不欺瞒客户,是我们最起码的底线! 以服务为基础,以质量求生存,以技术求发展,成交一个客户多一个朋友!为您提供成都网站设计、做网站、成都外贸网站建设公司、成都网页设计、小程序制作、成都网站开发、成都网站制作、成都软件开发、重庆App定制开发是成都本地专业的网站建设和网站设计公司,等你一起来见证!
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!
//Subject.java
/**
* 科目类
*/
public class Subject {
/** 名称 */
private String name;
/** 成绩 */
private int score;
public Subject(String name,int score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
import java.util.List;
//Student.java
/**
* 学生类
*/
public class Student{
/** 姓名 */
private String name;
/** 学科 */
private ListSubject subjects;
/** 总分 */
private int totalScore = 0;
/** 平均分 */
private int avgScore = 0;
public Student(String name,ListSubject subjects){
this.name = name;
this.subjects = subjects;
if(subjects != null subjects.size() 0){
for(Subject s : subjects){
totalScore += s.getScore();
}
avgScore = totalScore / subjects.size();
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ListSubject getSubjects() {
return subjects;
}
public void setSubjects(ListSubject subjects) {
this.subjects = subjects;
}
public int getTotalScore() {
return totalScore;
}
public int getAvgScore() {
return avgScore;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
//StudentScore.java
/**
* 主测试程序
*/
public class StudentScore {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入课程数");
int subjectNum = scanner.nextInt();
System.out.println("请输入人数");
int stuNum = scanner.nextInt();
String[] subjectNames = new String[subjectNum];
for(int i = 0; i subjectNum; i++){
System.out.println("请输入第" + (i + 1) + "门科目名称");
subjectNames[i] = scanner.next();
}
String[] stuNames = new String[stuNum];
for(int i = 0; i stuNum; i++){
System.out.println("请输入第" + (i + 1) + "个学员名称");
stuNames[i] = scanner.next();
}
Student[] stuArr = new Student[stuNum];
for(int i = 0; i stuNum; i++){
ListSubject subjects = new ArrayListSubject();
for(int j = 0; j subjectNum; j++){
System.out.println("输入" + stuNames[i] + "的" + subjectNames[j] + "成绩");
int score = scanner.nextInt();
Subject subject = new Subject(subjectNames[j], score);
subjects.add(subject);
}
stuArr[i] = new Student(stuNames[i], subjects);
}
//按照总成绩从高到底排序
Arrays.sort(stuArr, new ComparatorStudent() {
@Override
public int compare(Student stu1, Student stu2) {
if(stu1.getTotalScore() stu2.getTotalScore()){
return 1;
} else if(stu1.getTotalScore() stu2.getTotalScore()){
return -1;
} else {
return 0;
}
}
});
//输出标题头
final String FORMAT = "\t";
System.out.print("姓名" + FORMAT);
for(int i = 0;i subjectNum; i++){
System.out.print(subjectNames[i] + FORMAT);
}
System.out.println("总分" + FORMAT + "平均分" + FORMAT + "排名");
//按照排名输出
for(int i = 0; i stuArr.length; i++){
System.out.print(stuArr[i].getName() + FORMAT);
for(int j = 0; j subjectNum; j++){
System.out.print(stuArr[i].getSubjects().get(j).getScore() + FORMAT);
}
System.out.println(stuArr[i].getTotalScore() + FORMAT + stuArr[i].getAvgScore() + FORMAT + "第" + (i + 1) + "名");
}
}
}
文字水印
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
public class WaterSet {
/**
* 给图片添加水印
*
* @param filePath
* 需要添加水印的图片的路径
* @param markContent
* 水印的文字
* @param markContentColor
* 水印文字的颜色
* @param qualNum
* 图片质量
* @return
*/
public boolean createMark(String filePath, String markContent,
Color markContentColor, float qualNum) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
g.drawString(markContent, width / 5, height / 5); // 添加水印的文字和设置水印文字出现的内容
g.dispose();
try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
return false;
}
return true;
}
}
图片水印
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public final class ImageUtils {
public ImageUtils() {
}
/*
* public final static String getPressImgPath() { return ApplicationContext
* .getRealPath("/template/data/util/shuiyin.gif"); }
*/
/**
* 把图片印刷到图片上
*
* @param pressImg --
* 水印文件
* @param targetImg --
* 目标文件
* @param x
* --x坐标
* @param y
* --y坐标
*/
public final static void pressImage(String pressImg, String targetImg,
int x, int y) {
try {
//目标文件
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
//水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.drawImage(src_biao, (wideth - wideth_biao) / 2,
(height - height_biao) / 2, wideth_biao, height_biao, null);
//水印文件结束
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打印文字水印图片
*
* @param pressText
* --文字
* @param targetImg --
* 目标图片
* @param fontName --
* 字体名
* @param fontStyle --
* 字体样式
* @param color --
* 字体颜色
* @param fontSize --
* 字体大小
* @param x --
* 偏移量
* @param y
*/
public static void pressText(String pressText, String targetImg,
String fontName, int fontStyle, int color, int fontSize, int x,
int y) {
try {
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// String s="";
g.setColor(Color.RED);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.drawString(pressText, wideth - fontSize - x, height - fontSize
/ 2 - y);
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
pressImage("F:/logo.png", "F:/123.jpg", 0, 0);
}
}
//defaultSuffix是jpg
public static final boolean resizeImage(String fileName, String suffix) throws Exception {
boolean uploaded = false;
BufferedImage input;
if(suffix.equalsIgnoreCase("tif")||suffix.equalsIgnoreCase("tiff")||suffix.equalsIgnoreCase("png")) {
RenderedImage image = JAI.create("fileload", TurbineServlet.getRealPath(imageRoot + fileName+"."+suffix));
WritableRaster raster = image.copyData(null);
BufferedImage bi = new BufferedImage( image.getColorModel(), raster, true, null);
BufferedImage bi2 = new BufferedImage( maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi2.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setBackground(java.awt.Color.WHITE);//把tiff和png转换后的背景设置为白色
g2.fillRect(0, 0, maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth());
g2.drawImage(bi, 0, 0, maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth(), null);
PlanarImage pi = PlanarImage.wrapRenderedImage(bi2);
JAI.create("FileStore", pi, TurbineServlet.getRealPath(resizeRoot + fileName+"."+defaultSuffix).replaceAll("\\\\","\\\\\\\\"), "JPEG", new JPEGEncodeParam());
input = pi.getAsBufferedImage();
int w = maxThumbDimension, h = maxThumbDimension;
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = output.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(input,0,0,w,h, null);
ImageIO.write(output, defaultSuffix, new File(TurbineServlet.getRealPath(thumbRoot + fileName+"."+defaultSuffix)));
}
else {
input = ImageIO.read(new File(TurbineServlet.getRealPath(imageRoot + fileName+"."+suffix)));
int w = maxThumbDimension, h = maxThumbDimension;
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = output.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(input,0,0,w,h, null);
ImageIO.write(output, defaultSuffix, new File(TurbineServlet.getRealPath(thumbRoot + fileName+"."+defaultSuffix)));
BufferedImage output2 = new BufferedImage(maxResizeDimension, input.getHeight()*maxResizeDimension/input.getWidth(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2 = output2.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(input,0,0,maxResizeDimension, input.getHeight()*maxResizeDimension/input.getWidth(), null);
ImageIO.write(output2, defaultSuffix, new File(TurbineServlet.getRealPath(resizeRoot + fileName+"."+defaultSuffix)));
}
uploaded = true;
return uploaded;
}
%@ page language="java" pageEncoding="UTF-8"%
%@page import="java.awt.image.BufferedImage"%
%@page import="javax.imageio.ImageIO"%
%@page import="java.io.File"%
%
out.clear();
response.setContentType("image/png");
BufferedImage image = ImageIO.read(new File("D:\\mark.png"));
ImageIO.write(image, "png", response.getOutputStream());
%
我这样写没有问题,你试试。我来解释一下楼上的疑问,html标签不可能引用到本地的文件,比如C:\\xx.jpg你指定的路径得是web目录下的,动态生成图片一般用servlet实现,比如验证码之类的。
以前写的一个比较粗糙的例子。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Tupian extends JFrame implements ActionListener{
JLabel jl=new JLabel("图片");
JMenuBar jmb=new JMenuBar();
JMenu jm=new JMenu("文件");
JMenuItem jmi=new JMenuItem("选择图片");
JPanel jp=new JPanel(new FlowLayout(FlowLayout.CENTER));
JFileChooser chooser=new JFileChooser();
public Tupian() {
super("浏览图片");
jmb.add(jm);
jm.add(jmi);
jp.add(jl);
jmi.addActionListener(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.add(jmb,BorderLayout.NORTH);
this.add(jp,BorderLayout.CENTER);
this.setSize(800,600);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
int i=chooser.showOpenDialog(this);
if(i==chooser.APPROVE_OPTION){
Image image=new ImageIcon(chooser.getSelectedFile().getPath()).getImage();
image=image.getScaledInstance(400, 400, Image.SCALE_DEFAULT );//调整图像大小400,400
jl.setIcon(new ImageIcon(image));
jl.setText("");
}
if(i==chooser.CANCEL_OPTION)return;
}
public static void main (String[] args) {
new Tupian();
}
}