M
Maicus
Gast
Guten Mittag,
ich sitze jetzt schon ziemlich lange an einer Aufgabe, ich will eine art Vereinfachung des Grafikladevorgangs schreiben. Habe aber eine Klasse die sich nicht lädt usw. seht einfach selbst:
Das ist bisher der gesamt Code: (Es geht um die Klasse ImageContainer)
[Java]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.border.TitledBorder;
import java.io.*;
import javax.imageio.*;
import java.net.*;
/*
* Marcus Janiks Engine zur einfach Erstellung von einfachen Grafischen Anwendung unter Bentzung von Swing
*/
public class Leinwand extends JFrame implements ActionListener
{
private JLabel imageLabel;
private JLabel textLabel;
private JLabel topLabel;
ImageContainer background[];
private JButton imageButton;
private int currentimage;
private ShowCanvas canvas;
public static void main() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Leinwand app = new Leinwand();
app.setVisible(true);
}
});
}
Leinwand()
{
BufferedImage help;
try {
URL url = new URL("images/sunw01.jpg");
help = ImageIO.read(url);
}
catch(MalformedURLException e)
{
}
catch (IOException e) {
System.out.println("What the Heck");
}
System.out.println("Probe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("4Gewinnt");
background = new ImageContainer[5];
for (int i = 0; i < background.length; i++)
{
String path = "images/sunw0" + (i+1) + ".jpg";
int b = 1;
background.loadImage(path);
}
//Container container = getContentPane();
canvas = new ShowCanvas();
imageButton = new JButton(background[0].getImageIcon());
currentimage = 0;
imageButton.setActionCommand("Change");
//imageButton.setOpaque(false);
imageButton.setContentAreaFilled(false);
imageButton.setBorderPainted(false);
/*
imageLabel = new JLabel(background,JLabel.CENTER);
imageLabel.setHorizontalAlignment(JLabel.LEFT);
imageLabel.setVerticalAlignment(JLabel.BOTTOM);
imageLabel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
*/
textLabel = new JLabel("Some sort of text");
textLabel.setVerticalTextPosition(JLabel.CENTER);
textLabel.setHorizontalTextPosition(JLabel.RIGHT);
textLabel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
topLabel = new JLabel("This the top area");
topLabel.setVerticalAlignment(JLabel.CENTER);
topLabel.setHorizontalAlignment(JLabel.CENTER);
topLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
imageButton.addActionListener(this);
imageButton.setToolTipText("Click this Button to change the picture");
setSize(400, 300);
setLocationRelativeTo(null);
//add(imageLabel, BorderLayout.CENTER);
add(imageButton, BorderLayout.WEST);
add(canvas, BorderLayout.CENTER);
add(textLabel, BorderLayout.EAST);
add(topLabel, BorderLayout.NORTH);
}
protected ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public void actionPerformed(ActionEvent e)
{
if ("Change".equals(e.getActionCommand())&¤timage<4)
{
currentimage++;
imageButton.setIcon(background[currentimage].getImageIcon());
}
else
{
currentimage = 0;
imageButton.setIcon(background[currentimage].getImageIcon());
}
}
}
class ImageContainer
{
String Description;
BufferedImage image, originalImage;
ImageIcon icon;
int width, height;
int type;
ImageContainer()
{
Description = "Standard";
image = null;
originalImage = null;
width = 0;
height = 0;
type = 0;
icon = null;
System.out.println("Hierher-1");
}
public void loadImage(String path)
{
System.out.println("Hierher0");
int i=0;
int b = 5;
int x = 3;
try
{
System.out.println("Hierher1");
URL url = new URL(path);
System.out.println("Hierher2");
this.image = ImageIO.read(url);
System.out.println("Hierher3");
}
catch(MalformedURLException e)
{
System.out.println("URLEXCEPTION!");
}
catch(IOException e)
{
System.out.println("Bild");
}
System.out.println("Hierher4");
width = image.getWidth();
height = image.getHeight();
type = image.getType();
originalImage = image;
}
public void resetImage()
{
image = originalImage;
}
public ImageIcon getImageIcon()
{
return icon;
}
public BufferedImage getBufferedImage()
{
return image;
}
public void rescaleImage(int w, int h)
{
BufferedImage resizedImage = new BufferedImage(w, h, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, w, h, null);
g.dispose();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
image = resizedImage;
}
}
class ShowCanvas extends JPanel
{
int x, y;
BufferedImage image;
ShowCanvas() {
setBackground(Color.white);
setSize(450, 400);
addMouseMotionListener(new MouseMotionHandler());
Image img = getToolkit().getImage("images/node.png");
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 1);
try {
mt.waitForAll();
} catch (Exception e) {
System.out.println("Image not found.");
}
image = new BufferedImage(img.getWidth(this), img.getHeight(this),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.drawImage(img, 0, 0, this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(image, x, y, this);
}
class MouseMotionHandler extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {
}
}
}
class GameTimer
{
private final int NANOSECONDS = 1000000000;
private boolean timerStarted;
private long beginTime, time, timeDifference;
public GameTimer()
{
timerStarted = false;
beginTime = 0;
time = 0;
timeDifference = 0;
}
public void start()
{
timerStarted = true;
beginTime = System.nanoTime();
}
public void stop()
{
timerStarted = false;
beginTime = 0;
}
public int get()
{
time = System.nanoTime();
timeDifference = time - beginTime;
beginTime = time;
return (int)timeDifference;
}
}
[/code]
Das ist die Fehler Ausgabe im Terminal:
Und im Ausgabefenster kommt nur "Probe raus" nicht einmal das im Konstruktor aufgerufene "Hierher-1" wird angezeigt. Ich würd darauf schließen das es nicht lädt und somit die NullPointerException aufgerufen wird bin mir aber nicht sicher, und seh auch nicht was falsch ist.
Ich hoffe das ihr was findet und danke euch schonmal im Voraus
Marcus
ich sitze jetzt schon ziemlich lange an einer Aufgabe, ich will eine art Vereinfachung des Grafikladevorgangs schreiben. Habe aber eine Klasse die sich nicht lädt usw. seht einfach selbst:
Das ist bisher der gesamt Code: (Es geht um die Klasse ImageContainer)
[Java]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.border.TitledBorder;
import java.io.*;
import javax.imageio.*;
import java.net.*;
/*
* Marcus Janiks Engine zur einfach Erstellung von einfachen Grafischen Anwendung unter Bentzung von Swing
*/
public class Leinwand extends JFrame implements ActionListener
{
private JLabel imageLabel;
private JLabel textLabel;
private JLabel topLabel;
ImageContainer background[];
private JButton imageButton;
private int currentimage;
private ShowCanvas canvas;
public static void main() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Leinwand app = new Leinwand();
app.setVisible(true);
}
});
}
Leinwand()
{
BufferedImage help;
try {
URL url = new URL("images/sunw01.jpg");
help = ImageIO.read(url);
}
catch(MalformedURLException e)
{
}
catch (IOException e) {
System.out.println("What the Heck");
}
System.out.println("Probe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("4Gewinnt");
background = new ImageContainer[5];
for (int i = 0; i < background.length; i++)
{
String path = "images/sunw0" + (i+1) + ".jpg";
int b = 1;
background.loadImage(path);
}
//Container container = getContentPane();
canvas = new ShowCanvas();
imageButton = new JButton(background[0].getImageIcon());
currentimage = 0;
imageButton.setActionCommand("Change");
//imageButton.setOpaque(false);
imageButton.setContentAreaFilled(false);
imageButton.setBorderPainted(false);
/*
imageLabel = new JLabel(background,JLabel.CENTER);
imageLabel.setHorizontalAlignment(JLabel.LEFT);
imageLabel.setVerticalAlignment(JLabel.BOTTOM);
imageLabel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
*/
textLabel = new JLabel("Some sort of text");
textLabel.setVerticalTextPosition(JLabel.CENTER);
textLabel.setHorizontalTextPosition(JLabel.RIGHT);
textLabel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
topLabel = new JLabel("This the top area");
topLabel.setVerticalAlignment(JLabel.CENTER);
topLabel.setHorizontalAlignment(JLabel.CENTER);
topLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
imageButton.addActionListener(this);
imageButton.setToolTipText("Click this Button to change the picture");
setSize(400, 300);
setLocationRelativeTo(null);
//add(imageLabel, BorderLayout.CENTER);
add(imageButton, BorderLayout.WEST);
add(canvas, BorderLayout.CENTER);
add(textLabel, BorderLayout.EAST);
add(topLabel, BorderLayout.NORTH);
}
protected ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public void actionPerformed(ActionEvent e)
{
if ("Change".equals(e.getActionCommand())&¤timage<4)
{
currentimage++;
imageButton.setIcon(background[currentimage].getImageIcon());
}
else
{
currentimage = 0;
imageButton.setIcon(background[currentimage].getImageIcon());
}
}
}
class ImageContainer
{
String Description;
BufferedImage image, originalImage;
ImageIcon icon;
int width, height;
int type;
ImageContainer()
{
Description = "Standard";
image = null;
originalImage = null;
width = 0;
height = 0;
type = 0;
icon = null;
System.out.println("Hierher-1");
}
public void loadImage(String path)
{
System.out.println("Hierher0");
int i=0;
int b = 5;
int x = 3;
try
{
System.out.println("Hierher1");
URL url = new URL(path);
System.out.println("Hierher2");
this.image = ImageIO.read(url);
System.out.println("Hierher3");
}
catch(MalformedURLException e)
{
System.out.println("URLEXCEPTION!");
}
catch(IOException e)
{
System.out.println("Bild");
}
System.out.println("Hierher4");
width = image.getWidth();
height = image.getHeight();
type = image.getType();
originalImage = image;
}
public void resetImage()
{
image = originalImage;
}
public ImageIcon getImageIcon()
{
return icon;
}
public BufferedImage getBufferedImage()
{
return image;
}
public void rescaleImage(int w, int h)
{
BufferedImage resizedImage = new BufferedImage(w, h, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, w, h, null);
g.dispose();
g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
image = resizedImage;
}
}
class ShowCanvas extends JPanel
{
int x, y;
BufferedImage image;
ShowCanvas() {
setBackground(Color.white);
setSize(450, 400);
addMouseMotionListener(new MouseMotionHandler());
Image img = getToolkit().getImage("images/node.png");
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 1);
try {
mt.waitForAll();
} catch (Exception e) {
System.out.println("Image not found.");
}
image = new BufferedImage(img.getWidth(this), img.getHeight(this),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
g2.drawImage(img, 0, 0, this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(image, x, y, this);
}
class MouseMotionHandler extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {
}
}
}
class GameTimer
{
private final int NANOSECONDS = 1000000000;
private boolean timerStarted;
private long beginTime, time, timeDifference;
public GameTimer()
{
timerStarted = false;
beginTime = 0;
time = 0;
timeDifference = 0;
}
public void start()
{
timerStarted = true;
beginTime = System.nanoTime();
}
public void stop()
{
timerStarted = false;
beginTime = 0;
}
public int get()
{
time = System.nanoTime();
timeDifference = time - beginTime;
beginTime = time;
return (int)timeDifference;
}
}
[/code]
Das ist die Fehler Ausgabe im Terminal:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Leinwand.<init>(Leinwand.java:64)
at Leinwand$1.run(Leinwand.java:27)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:671)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Ich hoffe das ihr was findet und danke euch schonmal im Voraus
Marcus