Helpex - Trao đổi & giúp đỡ Đăng nhập

LỖI Caused by: java.lang.NullPointerException VÀ Exception in thread main java.lang.ExceptionInInitializerError MÀ KO BÍT LÀM SAO MONG ĐC GIÚP ĐỠ Ạ

package main.java.org.subdivision.client;

import java.awt.BorderLayout;

import java.awt.GraphicsEnvironment;

import java.awt.Rectangle;

import java.io.IOException;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import main.java.org.subdivision.DivideStrategy;//0

import main.java.org.subdivision.Image;//1

import main.java.org.subdivision.RenderMethod;//2

import main.java.org.subdivision.RenderProperties;//2

import main.java.org.subdivision.tool.Configuration;//4

import main.java.org.subdivision.tool.ImageDisplayComponent;//5

import main.java.org.subdivision.tool.Util;//6

public class ImageClient {// khách hàng hình ảnh


Image image;

public static void main(String[] args) throws Exception {

RenderProperties renderProperties = new RenderProperties(

0.4f,

1,

RenderMethod.STROKE,

DivideStrategy.CENTER,

false,

false,

true);

//tạo hình ảnh tiến bộ

//sai 34

progressiveImageGeneration("dc2.jpg", renderProperties);

}

private static void progressiveImageGeneration(String name, RenderProperties renderProperties) throws IOException {

//sai 38

Image img = new Image(Configuration.getInstance().getFullInputImageFileName(name), renderProperties);


generateImage(img, name);

}


private static void generateImage(Image img, String name) {

ImageDisplayComponent id = new ImageDisplayComponent(img);

ImageClient d = new ImageClient(img);

d.display(id);


Thread t = new Thread(() -> {

while (img.render(id))

;

id.repaint();

Util.saveImageTo(img, Configuration.getInstance().getFullOutputDir(name), name);

System.out.println("Finished");

});

t.start();

}


public ImageClient(Image image) {

super();

this.image = image;

}

public ImageClient() {

throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

}

public void display(ImageDisplayComponent id) {// thành phần hiện thị hình ảnh

JFrame w = new JFrame();

w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//tự chú thích

Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();

System.out.println(screenBounds);

w.setBounds(0, 0, (int) Math.min(screenBounds.getWidth(), image.width),

(int) Math.min(screenBounds.getHeight() * 0.1, image.height));


JScrollPane jsp = new JScrollPane(id, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);


w.getContentPane().add(jsp, BorderLayout.CENTER);

w.pack();

w.setVisible(true);

}

}

CLASS UTIL

public class Util {


public static String[] splitPath(String pathString) {

Path path = Paths.get(pathString);

return StreamSupport.stream(path.spliterator(), false).map(Path::toString).toArray(String[]::new);

}


public static JSONObject getJSONObjectFromClassPath(String name) throws ClassNotFoundException, ParseException, org.json.simple.parser.ParseException {

String configuration = getResourceFromClassPath(name);

//coi 33

System.out.println(configuration);

JSONParser parser = new JSONParser();

Object obj = (String) parser.parse(configuration);

JSONObject jsonObj = (JSONObject) obj;


return jsonObj;

}


public static void main(String[] args) throws ClassNotFoundException, ParseException, org.json.simple.parser.ParseException {


System.out.println(new Configuration());


}


/**

* Loads text file from the class path

*

* @param name the name of the file

* @return the {@link String} with the file's content

* @throws ClassNotFoundException

*/

public static String getResourceFromClassPath(String name) throws ClassNotFoundException {

InputStream resourceStream = Util.class.getClassLoader().getResourceAsStream(name);

BufferedReader br = new BufferedReader(new InputStreamReader(resourceStream));

String content = br.lines().collect(Collectors.joining("\r\n"));

return content;

}


public static String getFileNameFrom(String fileNameWithExtension) {

String[] tokens = fileNameWithExtension.split("\\.(?=[^\\.]+$)");

String[] fileNamePath = splitPath(tokens[0]);

return fileNamePath[fileNamePath.length - 1];

}


public static String getDateString() {

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS");

String dateStr = sdf.format(new Date());

return dateStr;

}


public static void saveImageTo(Image image, String path, String fileName) {

String dateStr = getDateString();

String fileNameWithoutExt = getFileNameFrom(fileName);

String outputFileName = path + "/" + fileNameWithoutExt + "_" + dateStr + ".png";

File f = new File(outputFileName);

f.getParentFile().mkdirs();

image.saveImage(outputFileName);

}


}

CLASS CONFIGURATION

public class Configuration {


private String imageDir;


private String outputDir;


private String videoDir;

private static Configuration INSTANCE = new Configuration();


public static Configuration getInstance() {

return INSTANCE;

}


public Configuration() {

super();


JSONObject map = new JSONObject();

try {

map = Util.getJSONObjectFromClassPath("config.json");

} catch (ClassNotFoundException | ParseException e) {

e.printStackTrace();

}


imageDir = (String) map.get("inputDir");

videoDir = (String) map.get("videoDir");

outputDir = (String) map.get("outputDir");

}


public String getImageDir() {

return imageDir;

}


public String getoutputDir() {

return outputDir;

}


public String getVideoDir() {

return videoDir;

}


public String getFullVideoFileName(String fileName) {

String fileNameString = Paths.get(getVideoDir(), fileName).toString();

File f = new File(fileNameString);

String absolutePath = f.getAbsolutePath();

System.out.println(absolutePath);

return absolutePath;

}

public String getFullInputImageFileName(String fileName) {

String fileNameString = Paths.get(getImageDir(), fileName).toString();

File f = new File(fileNameString);

String absolutePath = f.getAbsolutePath();

System.out.println(absolutePath);

return absolutePath;

}


public String getFullOutputDir(String fileName) {

String fileNameString = Paths.get(getoutputDir(), fileName).toString();

File f = new File(fileNameString);

String absolutePath = f.getParentFile().getAbsolutePath();

return absolutePath;

}

@Override

public String toString() {

return String.format("Configuration [imageDir=%s, videoDir=%s, outputDir=%s]", imageDir, videoDir, outputDir);

}


}


0 hữu ích 0 bình luận 757 xem chia sẻ

Có thể bạn quan tâm

loading