Ich versuche gerade ein eigenes Steuerelement zu erstellen. Das sollte so etwas sein wie ein Zeitstrahl, bei dem man Termine einträgt, die sich von der Dauer her unterscheiden. Bei der Darstellung des Termins sollte ein kleiner Infotext erscheinen.
Hier ist meine erste Version:
Kann man vielleicht was verbessern oder vereinfachen?
Hier ist meine erste Version:
Code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class Main extends JFrame{
private Timeline timeline;
public Main(){
setLayout(new BorderLayout());
timeline = new Timeline();
timeline.setDate(new DateCode("Meeting Herr Schmitt. Projektbesprechung: neue Version von gBase Version 17. Die Details müssen noch besprochen werden","0630", 1, 2));
timeline.setDate(new DateCode("Projektfertigstellung DBBase", "0930", 7, 10));
timeline.setDate(new DateCode("Termin Hr. Peters. Kalkulation des Auftrags: neue Bürosoftware", "0800", 4, 6));
JScrollPane sptimeline = new JScrollPane(timeline);
sptimeline.setPreferredSize(new Dimension(500,600));
sptimeline.setMinimumSize(new Dimension(500,600));
add(sptimeline, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//timeline.setDate(new DateCode("", "", 4, 7));
}
public static void main(String[] args){
Main window = new Main();
window.setVisible(true);
window.pack();
}
}
Code:
public class DateCode {
private String description;
private String timecode;
private int timestart;
private int timeend;
public DateCode(String adescription, String atimecode, int atimestart, int atimeend){
this.description = adescription;
this.timecode = atimecode;
this.timestart = atimestart;
this.timeend = atimeend;
}
public void setDescription(String adescription){
this.description = adescription;
}
public String getDescription(){
return this.description;
}
public void setTimecode(String atimecode){
this.timecode = atimecode;
}
public String getTimecode(){
return this.timecode;
}
public void setTimestart(int atimestart){
this.timestart = atimestart;
}
public int getTimestart(){
return this.timestart;
}
public void setTimeend(int atimeend){
this.timeend = atimeend;
}
public int getTimeend(){
return this.timeend;
}
}
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JComponent;
public class Timeline extends JComponent{
private DateCode dates[] = new DateCode[36];
private Font font = new Font("Sans", Font.PLAIN, 12);
public Timeline(){
for(int a=0;a<36;a++){
dates[a] = new DateCode("", "",a ,a+1);
}
}
public void paint(Graphics g){
Color black = new Color(0,0,0);
g.setColor(black);
g.drawLine(100, 0, 400, 0);
g.drawLine(100, 1800, 400, 1800);
g.drawLine(99, 0, 99, 1800);
g.drawLine(400, 0, 400, 1800);
paintTimeCodes(g);
paintRectangles(g);
}
public void paintTimeCodes(Graphics g){
Color black = new Color(0,0,0);
g.drawLine(100, 0, 50, 0);
g.drawString("06:00", 50, 20);
g.drawLine(100, 100, 50, 100);
g.drawString("07:00", 50, 120);
g.drawLine(100, 200, 50, 200);
g.drawString("08:00", 50, 220);
g.drawLine(100, 300, 50, 300);
g.drawString("09:00", 50, 320);
g.drawLine(100, 400, 50, 400);
g.drawString("10:00", 50, 420);
g.drawLine(100, 500, 50, 500);
g.drawString("11:00", 50, 520);
g.drawLine(100, 600, 50, 600);
g.drawString("12:00", 50, 620);
g.drawLine(100, 700, 50, 700);
g.drawString("13:00", 50, 720);
g.drawLine(100, 800, 50, 800);
g.drawString("14:00", 50, 820);
g.drawLine(100, 900, 50, 900);
g.drawString("15:00", 50, 920);
g.drawLine(100, 1000, 50, 1000);
g.drawString("16:00", 50, 1020);
g.drawLine(100, 1100, 50, 1100);
g.drawString("17:00", 50, 1120);
g.drawLine(100, 1200, 50, 1200);
g.drawString("18:00", 50, 1220);
g.drawLine(100, 1300, 50, 1300);
g.drawString("19:00", 50, 1320);
g.drawLine(100, 1400, 50, 1400);
g.drawString("20:00", 50, 1420);
g.drawLine(100, 1500, 50, 1500);
g.drawString("21:00", 50, 1520);
g.drawLine(100, 1600, 50, 1600);
g.drawString("22:00", 50, 1620);
g.drawLine(100, 1700, 50, 1700);
g.drawString("23:00", 50, 1720);
}
public void paintRectangles(Graphics g){
Color red = new Color(255,0,0);
Color grey = new Color(200,200,200);
Color black = new Color(0,0,0);
String strlastdate = "";
String strdate = "";
int pxheight = 50;
for(int a=0;a<36;a++){
strdate = dates[a].getTimecode();
if(!strdate.equals("") && !strdate.equals(strlastdate)){
g.setColor(red);
g.fillRect(100, (a*pxheight), 300, 50);
g.setColor(black);
g.drawLine(100, (a*pxheight), 400, (a*pxheight));
g.setFont(font);
StringBuilder description[] = formatDescription(g, dates[a].getDescription());
g.drawString(description[0].toString(), 110, ((a*pxheight)+20));
g.drawString(description[1].toString(), 110, ((a*pxheight)+40));
}
if(!strdate.equals("") && strdate.equals(strlastdate)){
g.setColor(red);
g.fillRect(100, (a*pxheight), 300, 50);
}
else if(strdate.equals("") && !strdate.equals(strlastdate)){
g.setColor(grey);
g.fillRect(100, (a*pxheight), 300, 50);
g.setColor(black);
g.drawLine(100, (a*pxheight), 400, (a*pxheight));
}
else if(strdate.equals("") && strdate.equals(strlastdate)){
g.setColor(grey);
g.fillRect(100, (a*pxheight), 300, 50);
}
strlastdate = strdate;
}
}
public Dimension getPreferredSize(){
return new Dimension(300,1800);
}
public void setDate(DateCode adatecode){
for(int a=adatecode.getTimestart();a<adatecode.getTimeend();a++){
dates[a] = adatecode;
repaint();
}
}
public StringBuilder[] formatDescription(Graphics g, String adescription){
int width = 280;
int linewidth = 0;
StringBuilder builder[] = new StringBuilder[2];
builder[0] = new StringBuilder();
builder[1] = new StringBuilder();
String[] words = adescription.split(" ");
boolean isFirstLine = false;
int builderversion = 0;
for(int a=0;a<words.length;a++){
int wordwidth = getWordWidth(g, words[a]);
int spacewidth = getWordWidth(g, " ");
linewidth = linewidth + wordwidth;
if(linewidth < width){
builder[builderversion].append(words[a]);
builder[builderversion].append(" ");
linewidth = linewidth + spacewidth;
}
else{
if(isFirstLine == false){
builderversion = 1;
linewidth = wordwidth;
isFirstLine = true;
builder[builderversion].append(words[a]);
builder[builderversion].append(" ");
linewidth = linewidth + spacewidth;
}
}
}
return builder;
}
public int getWordWidth(Graphics g, String aword){
FontMetrics fontmetrics = g.getFontMetrics(font);
int wordwidth = fontmetrics.stringWidth(aword);
return wordwidth;
}
}
Kann man vielleicht was verbessern oder vereinfachen?