P
Philip_A
Gast
Hallo liebe Leute,
ich muss eine Hausaufgabe abgeben bei welcher es um Java Applets geht. Wir sollen Histogramme programmieren, die so aussehen und auch diese Funktion haben.
Link zum Applet: Histogramm-Applet
Für das erste Histogramm habe ich folgendes programmiert nur bekomme ich nach wie vor diese beiden Fehlermeldungen:
*.java uses unchecked or unsafe operations
Recompile with -Xlint: unchecked for details.
Hier mein Programmcode:
[Java]
package blatt6;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
public class Histogramm extends Applet
implements MouseListener
{
private static final String PARAMS = "4.37 3.87 4.00 4.03 3.50 4.08 2.25 4.70 1.73 4.93 1.73 4.62 3.43 4.25 1.68 3.92 3.68 3.10 4.03 1.77 4.08 1.75 3.20 1.85 4.62 1.97 4.50 3.92 4.35 2.33 3.83 1.88 4.60 1.80 4.73 1.77 4.57 1.85 3.52 4.00 3.70 3.72 4.25 3.58 3.80 3.77 3.75 2.50 4.50 4.10 3.70 3.80 3.43 4.00 2.27 4.40 4.05 4.25 3.33 2.00 4.33 2.93 4.58 1.90 3.58 3.73 3.73 1.82 4.63 3.50 4.00 3.67 1.67 4.60 1.67 4.00 1.80 4.42 1.90 4.63 2.93 3.50 1.97 4.28 1.83 4.13 1.83 4.65 4.20 3.93 4.33 1.83 4.53 2.03 4.18 4.43 4.07 4.13 3.95 4.10 2.27 4.58 1.90 4.50 1.95 4.83 4.12 "
;
private static final int LEFT_MARGIN = 40;
private static final int RIGHT_MARGIN = 15;
private static final int DOWN_MARGIN = 60;
private static final int TOP_MARGIN = 10;
private static final int KLICK_BAR_HEIGHT = 30;
private static final int APP_WIDTH = 700;
private static final int APP_HEIGHT = 500;
private static final int MIN_PERCENT = 3;
private static final int MAX_PERCENT = 47;
private double minParam;
private double maxParam;
private double intervalWidth;
private double maxFreq;
private double arithmeticMean;
private double pseudoMean;
private double relative_freqs[];
private double data[];
private int ceilValue;
private int k;
private int dataSize;
public Histogramm()
{
k = 10;
}
public void init()
{
setSize(700, 500);
addMouseListener(this);
String[] params =
"4.37 3.87 4.00 4.03 3.50 4.08 2.25 4.70 1.73 4.93 1.73 4.62 3.43 4.25 1.68 3.92 3.68 3.10 4.03 1.77 4.08 1.75 3.20 1.85 4.62 1.97 4.50 3.92 4.35 2.33 3.83 1.88 4.60 1.80 4.73 1.77 4.57 1.85 3.52 4.00 3.70 3.72 4.25 3.58 3.80 3.77 3.75 2.50 4.50 4.10 3.70 3.80 3.43 4.00 2.27 4.40 4.05 4.25 3.33 2.00 4.33 2.93 4.58 1.90 3.58 3.73 3.73 1.82 4.63 3.50 4.00 3.67 1.67 4.60 1.67 4.00 1.80 4.42 1.90 4.63 2.93 3.50 1.97 4.28 1.83 4.13 1.83 4.65 4.20 3.93 4.33 1.83 4.53 2.03 4.18 4.43 4.07 4.13 3.95 4.10 2.27 4.58 1.90 4.50 1.95 4.83 4.12 "
.split("\\s");
if(params != null)
{
data = new double[params.length];
dataSize = data.length;
double sum = 0.0D;
for(int i = 0; i < dataSize; i++)
{
double d = Double.parseDouble(params.trim());
data = d;
sum += d;
}
Arrays.sort(data);
minParam = data[0];
maxParam = data[data.length - 1];
ceilValue = (int)Math.ceil(maxParam);
arithmeticMean = sum / (double)dataSize;
computeData();
}
}
private void computeData()
{
intervalWidth = (maxParam - minParam) / (double)k;
relative_freqs = new double[k];
pseudoMean = 0.0D;
for(int i = 0; i < k; i++)
{
int count = 0;
java.util.List interval = new ArrayList();
for(int j = 0; j < data.length; j++)
{
if(data[j] >= minParam + intervalWidth * (double)i && data[j] < minParam + intervalWidth * (double)(i + 1))
{
count++;
interval.add(Double.valueOf(data[j]));
}
}
double median = 0.0D;
if(!interval.isEmpty())
{
median = ((Double)interval.get(interval.size() / 2)).doubleValue();
}
relative_freqs = (double)count / (double)data.length;
pseudoMean += relative_freqs * median;
}
maxFreq = Math.ceil(max(relative_freqs) * 10D) / 10D;
}
public void paint(Graphics g)
{
Point n = new Point(40, getBounds().height - 60);
Point ul = new Point(40, 10);
Point dr = new Point(getBounds().width - 15, getBounds().height - 60);
double histogramHeight = n.y - ul.y;
double histogramWidth = dr.x - n.x;
g.setFont(new Font("Dialog", 1, 12));
g.drawLine(n.x, n.y, ul.x, ul.y);
g.drawLine(n.x, n.y, dr.x, dr.y);
g.drawString("0", n.x - 18, n.y + 18);
int xPins = (ceilValue + 1) * 2;
double xPinWidth = histogramWidth / (double)xPins;
for(int i = 1; i <= xPins; i++)
{
g.drawLine(Math.round((float)((double)n.x + (double)i * xPinWidth)), n.y, Math.round((float)((double)n.x + (double)i * xPinWidth)), n.y + 5);
g.drawString((new StringBuilder()).append((double)i * 0.5D).append("").toString(), (n.x + (int)((double)i * xPinWidth)) - 5, n.y + 20);
}
int yPins = 12;
int yPinWidth = (int)histogramHeight / yPins;
double yPinStep = maxFreq / (double)(yPins - 1);
for(int i = 1; i <= yPins; i++)
{
g.drawLine(n.x, n.y - i * yPinWidth, n.x - 5, n.y - i * yPinWidth);
g.drawString(String.format("%1.2f", new Object[] {
Double.valueOf((double)i * yPinStep)
}), n.x - 35, (n.y - i * yPinWidth) + 5);
}
double pxWidthPerParam = histogramWidth / (double)(ceilValue + 1);
double start = pxWidthPerParam * minParam;
double end = pxWidthPerParam * maxParam;
double rectDistance = end - start;
double rectWidth = rectDistance / (double)k;
double pxHeightPerRelaltionPoint = histogramHeight / maxFreq;
g.setColor(Color.DARK_GRAY);
for(int i = 0; i < k; i++)
{
int barHeight = (int)(pxHeightPerRelaltionPoint * relative_freqs);
g.fillRect((int)(start + rectWidth * (double)i) + n.x, n.y - barHeight, (int)rectWidth, barHeight);
}
g.setColor(Color.LIGHT_GRAY);
g.fillRect(n.x, getBounds().height - 30, getBounds().width - 15 - 40, (int)histogramWidth);
g.setColor(Color.GREEN);
g.fillRect((int)(pxWidthPerParam * arithmeticMean + (double)n.x), 10, 2, (int)histogramHeight);
g.setColor(Color.RED);
g.fillRect((int)(pxWidthPerParam * pseudoMean + (double)n.x), 10, 2, (int)histogramHeight);
}
public void mouseClicked(MouseEvent e)
{
Point klick = e.getPoint();
if(klick.y <= getBounds().height && klick.y >= getBounds().height - 30 && klick.x >= 40 && klick.x <= getBounds().width - 15)
{
double p = (double)klick.x / (double)(getBounds().width - 15 - 40);
p = (p * 47D + 3D) / 100D;
k = (int)(p * (double)dataSize);
computeData();
repaint();
}
}
public void mousePressed(MouseEvent mouseevent)
{
}
public void mouseReleased(MouseEvent mouseevent)
{
}
public void mouseEntered(MouseEvent mouseevent)
{
}
public void mouseExited(MouseEvent mouseevent)
{
}
private static double max(double array[])
{
if(array == null)
{
throw new IllegalArgumentException("Array nicht initialisiert!");
}
double max = array[0];
for(int i = 1; i < array.length; i++)
{
if(array > max)
{
max = array;
}
}
return max;
}
}
[/Java]
Er compiliert zwar aber beim appviewer sagt er mir dann Applet nicht initialisiert.
Wäre toll wenn mir wer weiterhelfen könnte, danke!
ich muss eine Hausaufgabe abgeben bei welcher es um Java Applets geht. Wir sollen Histogramme programmieren, die so aussehen und auch diese Funktion haben.
Link zum Applet: Histogramm-Applet
Für das erste Histogramm habe ich folgendes programmiert nur bekomme ich nach wie vor diese beiden Fehlermeldungen:
*.java uses unchecked or unsafe operations
Recompile with -Xlint: unchecked for details.
Hier mein Programmcode:
[Java]
package blatt6;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
public class Histogramm extends Applet
implements MouseListener
{
private static final String PARAMS = "4.37 3.87 4.00 4.03 3.50 4.08 2.25 4.70 1.73 4.93 1.73 4.62 3.43 4.25 1.68 3.92 3.68 3.10 4.03 1.77 4.08 1.75 3.20 1.85 4.62 1.97 4.50 3.92 4.35 2.33 3.83 1.88 4.60 1.80 4.73 1.77 4.57 1.85 3.52 4.00 3.70 3.72 4.25 3.58 3.80 3.77 3.75 2.50 4.50 4.10 3.70 3.80 3.43 4.00 2.27 4.40 4.05 4.25 3.33 2.00 4.33 2.93 4.58 1.90 3.58 3.73 3.73 1.82 4.63 3.50 4.00 3.67 1.67 4.60 1.67 4.00 1.80 4.42 1.90 4.63 2.93 3.50 1.97 4.28 1.83 4.13 1.83 4.65 4.20 3.93 4.33 1.83 4.53 2.03 4.18 4.43 4.07 4.13 3.95 4.10 2.27 4.58 1.90 4.50 1.95 4.83 4.12 "
;
private static final int LEFT_MARGIN = 40;
private static final int RIGHT_MARGIN = 15;
private static final int DOWN_MARGIN = 60;
private static final int TOP_MARGIN = 10;
private static final int KLICK_BAR_HEIGHT = 30;
private static final int APP_WIDTH = 700;
private static final int APP_HEIGHT = 500;
private static final int MIN_PERCENT = 3;
private static final int MAX_PERCENT = 47;
private double minParam;
private double maxParam;
private double intervalWidth;
private double maxFreq;
private double arithmeticMean;
private double pseudoMean;
private double relative_freqs[];
private double data[];
private int ceilValue;
private int k;
private int dataSize;
public Histogramm()
{
k = 10;
}
public void init()
{
setSize(700, 500);
addMouseListener(this);
String[] params =
"4.37 3.87 4.00 4.03 3.50 4.08 2.25 4.70 1.73 4.93 1.73 4.62 3.43 4.25 1.68 3.92 3.68 3.10 4.03 1.77 4.08 1.75 3.20 1.85 4.62 1.97 4.50 3.92 4.35 2.33 3.83 1.88 4.60 1.80 4.73 1.77 4.57 1.85 3.52 4.00 3.70 3.72 4.25 3.58 3.80 3.77 3.75 2.50 4.50 4.10 3.70 3.80 3.43 4.00 2.27 4.40 4.05 4.25 3.33 2.00 4.33 2.93 4.58 1.90 3.58 3.73 3.73 1.82 4.63 3.50 4.00 3.67 1.67 4.60 1.67 4.00 1.80 4.42 1.90 4.63 2.93 3.50 1.97 4.28 1.83 4.13 1.83 4.65 4.20 3.93 4.33 1.83 4.53 2.03 4.18 4.43 4.07 4.13 3.95 4.10 2.27 4.58 1.90 4.50 1.95 4.83 4.12 "
.split("\\s");
if(params != null)
{
data = new double[params.length];
dataSize = data.length;
double sum = 0.0D;
for(int i = 0; i < dataSize; i++)
{
double d = Double.parseDouble(params.trim());
data = d;
sum += d;
}
Arrays.sort(data);
minParam = data[0];
maxParam = data[data.length - 1];
ceilValue = (int)Math.ceil(maxParam);
arithmeticMean = sum / (double)dataSize;
computeData();
}
}
private void computeData()
{
intervalWidth = (maxParam - minParam) / (double)k;
relative_freqs = new double[k];
pseudoMean = 0.0D;
for(int i = 0; i < k; i++)
{
int count = 0;
java.util.List interval = new ArrayList();
for(int j = 0; j < data.length; j++)
{
if(data[j] >= minParam + intervalWidth * (double)i && data[j] < minParam + intervalWidth * (double)(i + 1))
{
count++;
interval.add(Double.valueOf(data[j]));
}
}
double median = 0.0D;
if(!interval.isEmpty())
{
median = ((Double)interval.get(interval.size() / 2)).doubleValue();
}
relative_freqs = (double)count / (double)data.length;
pseudoMean += relative_freqs * median;
}
maxFreq = Math.ceil(max(relative_freqs) * 10D) / 10D;
}
public void paint(Graphics g)
{
Point n = new Point(40, getBounds().height - 60);
Point ul = new Point(40, 10);
Point dr = new Point(getBounds().width - 15, getBounds().height - 60);
double histogramHeight = n.y - ul.y;
double histogramWidth = dr.x - n.x;
g.setFont(new Font("Dialog", 1, 12));
g.drawLine(n.x, n.y, ul.x, ul.y);
g.drawLine(n.x, n.y, dr.x, dr.y);
g.drawString("0", n.x - 18, n.y + 18);
int xPins = (ceilValue + 1) * 2;
double xPinWidth = histogramWidth / (double)xPins;
for(int i = 1; i <= xPins; i++)
{
g.drawLine(Math.round((float)((double)n.x + (double)i * xPinWidth)), n.y, Math.round((float)((double)n.x + (double)i * xPinWidth)), n.y + 5);
g.drawString((new StringBuilder()).append((double)i * 0.5D).append("").toString(), (n.x + (int)((double)i * xPinWidth)) - 5, n.y + 20);
}
int yPins = 12;
int yPinWidth = (int)histogramHeight / yPins;
double yPinStep = maxFreq / (double)(yPins - 1);
for(int i = 1; i <= yPins; i++)
{
g.drawLine(n.x, n.y - i * yPinWidth, n.x - 5, n.y - i * yPinWidth);
g.drawString(String.format("%1.2f", new Object[] {
Double.valueOf((double)i * yPinStep)
}), n.x - 35, (n.y - i * yPinWidth) + 5);
}
double pxWidthPerParam = histogramWidth / (double)(ceilValue + 1);
double start = pxWidthPerParam * minParam;
double end = pxWidthPerParam * maxParam;
double rectDistance = end - start;
double rectWidth = rectDistance / (double)k;
double pxHeightPerRelaltionPoint = histogramHeight / maxFreq;
g.setColor(Color.DARK_GRAY);
for(int i = 0; i < k; i++)
{
int barHeight = (int)(pxHeightPerRelaltionPoint * relative_freqs);
g.fillRect((int)(start + rectWidth * (double)i) + n.x, n.y - barHeight, (int)rectWidth, barHeight);
}
g.setColor(Color.LIGHT_GRAY);
g.fillRect(n.x, getBounds().height - 30, getBounds().width - 15 - 40, (int)histogramWidth);
g.setColor(Color.GREEN);
g.fillRect((int)(pxWidthPerParam * arithmeticMean + (double)n.x), 10, 2, (int)histogramHeight);
g.setColor(Color.RED);
g.fillRect((int)(pxWidthPerParam * pseudoMean + (double)n.x), 10, 2, (int)histogramHeight);
}
public void mouseClicked(MouseEvent e)
{
Point klick = e.getPoint();
if(klick.y <= getBounds().height && klick.y >= getBounds().height - 30 && klick.x >= 40 && klick.x <= getBounds().width - 15)
{
double p = (double)klick.x / (double)(getBounds().width - 15 - 40);
p = (p * 47D + 3D) / 100D;
k = (int)(p * (double)dataSize);
computeData();
repaint();
}
}
public void mousePressed(MouseEvent mouseevent)
{
}
public void mouseReleased(MouseEvent mouseevent)
{
}
public void mouseEntered(MouseEvent mouseevent)
{
}
public void mouseExited(MouseEvent mouseevent)
{
}
private static double max(double array[])
{
if(array == null)
{
throw new IllegalArgumentException("Array nicht initialisiert!");
}
double max = array[0];
for(int i = 1; i < array.length; i++)
{
if(array > max)
{
max = array;
}
}
return max;
}
}
[/Java]
Er compiliert zwar aber beim appviewer sagt er mir dann Applet nicht initialisiert.
Wäre toll wenn mir wer weiterhelfen könnte, danke!
Zuletzt bearbeitet von einem Moderator: