public class SendPressEnterListener implements KeyListener {
@Override
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == 16) {
shiftFLAG = 1;
}
if (shiftFLAG == 0) {
if (arg0.getKeyCode() == KeyEvent.VK_ENTER & writingFLAG == 1) {
sendTextMessage();
TAentered.setText("");
TAentered.setCaretPosition(0);
}
} else if (shiftFLAG == 1) {
if (arg0.getKeyCode() == KeyEvent.VK_ENTER & writingFLAG == 1) {
String s = TAentered.getText() + "\n";
TAentered.setText(s);
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == 16) {
shiftFLAG = 0;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
}
}
public class textlistener implements ActionListener {
public GUI g;
public textlistener(GUI b) {
g = b;
}
@Override
public void actionPerformed(ActionEvent e) {
g.refreshButton();
}
}
public class GUIACTION implements Runnable {
// private int iterator=0;
@Override
public void run() {
while (refreshFLAG == 1) {
refreshButton();
testresize();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
///* public class Rendertime implements Runnable//Hier muss noch gearbeitet werden um die Leistung zu optimieren!
// {
//
// private int ms;
//
// public Rendertime(int time) {
// ms = time;
// }
//
// @Override
// public void run() {
//
// renderFLAG = 1;
// try {
// Thread.sleep(ms);
// } catch (InterruptedException ex) {
// Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
// }
//
// renderFLAG = 0;
//
// }
//
// }*/
class Surface extends JPanel
implements ActionListener {
private Timer timer;
private int i;
private BufferedImage background;
private Graphics2D work;
private BufferedImage img;
private Image SP1;
private Image SP2;
public Surface() {
initSurface();
initTimer();
}
private void initSurface() {
setBackground(Color.black);
try {
background = ImageIO.read(GUI.class.getResource("/img/physik.jpg"));
img = ImageIO.read(GUI.class.getResource("/img/physik.jpg"));
} catch (Exception e) {
System.out.println("Datei nicht gefunden");
}
work = (Graphics2D) img.getGraphics();
work.drawImage(background, null, 0, 0);
work.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
//
RenderingHints rh
= new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
work.setRenderingHints(rh);
}
private void initTimer() {
timer = new Timer(30, this);
timer.setInitialDelay(190);
timer.start();
}
public void imgflash() {
//BufferedImage flashimg = new BufferedImage(mPanel.getHeight(),mPanel.getWidth(),BufferedImage.TYPE_INT_ARGB);
//Versuch einen Weissen hintergrund zu schaffen
//Background Paper
work.drawImage(background, null, 0, 0);
}
public void render() {
imgflash();
renderConversation();
renderScroll();
}
public void renderScroll()
{
int XOffset =0;
// System.out.println(scrollAlpha);
double multi =scrollAlpha;
if(scrollAlpha < 0)
{
multi = (-1)*scrollAlpha;
}
int screenHeight =mPanel.getHeight()-95;
int scrollHeight=screenHeight;
if(lastBlockHeight > screenHeight)
{
double Ueberstand = lastBlockHeight-screenHeight;
double q = (lastBlockHeight /(double)screenHeight);
if (q <1)
{
q =1;
}
scrollHeight =(int) (screenHeight/q);
}
double alphad=multi*114+26;
int alpha = (int)alphad;
Color color = new Color(0,119,255,alpha);
double animatedX = multi*4.0;
int xPosition = (int)Math.round(mPanel.getWidth()-3-animatedX-XOffset);//-11 ist der Maximalwert!
double Spielraum = mPanel.getHeight()-95-scrollHeight;
// double yPositiond = (mPanel.getHeight()-(105))*scrollPercent;
double yPositiond = Spielraum*scrollPercent;
double inverty = Spielraum-yPositiond;
int yPosition = (int)inverty+3;
BufferedImage scrollIMG = new BufferedImage(40,screenHeight,BufferedImage.TYPE_INT_ARGB);
Graphics2D scrollG=(Graphics2D)scrollIMG.getGraphics();
scrollG.setColor(color);
scrollG.fillRoundRect(1, 1, 10, scrollHeight, 5, 5);
BufferedImage scrollBack = new BufferedImage(40,screenHeight,BufferedImage.TYPE_INT_ARGB);
Graphics2D BackG=(Graphics2D)scrollBack.getGraphics();
RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
qualityHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
BackG.setRenderingHints(qualityHints);
double alphaBackd = multi*80+20;
Color WhiteBack = new Color (0,119,255,60);
BackG.setColor(WhiteBack);
BackG.fillRoundRect(0, 0, 11, screenHeight, 10, 10);
work.drawImage(scrollBack, null, xPosition, 2);
work.drawImage(scrollIMG, null, xPosition, yPosition);
// System.out.println("x: "+xPosition+" y: "+yPosition);
}
public void renderConversation() {
int Height = (int) (mPanel.getHeight() + scroll);
int ScreenHeight = mPanel.getHeight();
int Width = mPanel.getWidth();
size dim = new size(Width, Height); //Die Dimension des Graphicfensters wird hier in ein Objekt der Klasse size gepackt!
int Anzahl = Conversation.getLength();
int x = 0;
//Es werden Maximal 100 Gui Elemente wie Nachriten oder Anderes Angezeigt bwz. Gerendert, daher muss die Anzahl normiert werden!
int MaximalElements = 100 * MessageBlockID;
int start = 0; //StartID der Iterationen!
if (Anzahl >= MaximalElements) {
start = Anzahl - MaximalElements+1;
}
int MessageBlockStop = Anzahl;
int fifty = 1;
for (int j = start; j < Anzahl; j++) {
if (Conversation.findID(j).islast()) {
MessageBlockStop = j;
}
if (fifty == MaximalElements) {
MessageBlockStop = j;
}
fifty++;
}
int MaxHeight = 0; //MaxHeight ist die gesammte Höhe der Gerenderten GuiElemente in Pixeln
for (int i = start; i < MessageBlockStop; i++) {
MaxHeight = MaxHeight + Conversation.findID(i).getGuiE().getHeight(dim);
}
lastBlockHeight=MaxHeight;
maxScroll=MaxHeight-ScreenHeight+100;
// System.out.println(MaxHeight);
//Wir suchen nun das Elment (id) mit der zu rendern angefangen werden soll (DIe effektive Höhe des bildes)
int notvisible = MaxHeight - Height; //Nicht sichtbarer Bereich
if (Height > MaxHeight) {
notvisible = 0;
}
int MessageScreen = ScreenHeight -120;//Wegen der unteren Leiste
if(MaxHeight > MessageScreen)
{
scrollLock =0;
}
else
{
scrollLock =1;
}
scrollPercent = scroll/maxScroll;
// System.out.println("The Scroll Percent is: "+scrollPercent);
//
//Ermitteln der ID mit der Angefangen werden soll und die Höhe des zu renderenden Bildes
int startID = 0; //Id mit der Später zu rendern angefangen werden soll
int effektivHeight = 0;
int iterationHeight = 0; //Starte natürlich bei 0
int Plus = 0;
for (int i = start; i < Anzahl; i++) {
Plus = Conversation.findID(i).getGuiE().getHeight(dim);
if (iterationHeight + Plus > notvisible) {
startID = i;
effektivHeight = MaxHeight - iterationHeight;
break;
}
iterationHeight = iterationHeight + Plus;
}
lastStartID = startID;
//ermitteln der Scrollabhängigen Stop_ID
int inStopID =Anzahl;
int inStopfound=0;
int stopID = Anzahl;
int renderAnzahl;
int off = effektivHeight-Height;
int toFILL = off + ScreenHeight ; //80 ist der Überstand über die GUI leiste!
int toFillScreen = toFILL +80;
int screenfilled = 0;
// int off = Height - effektivHeight;
// int toFILL = off + ScreenHeight ;
for (int k = startID; k < Anzahl; k++) {
Plus = Conversation.findID(k).getGuiE().getHeight(dim);
screenfilled = screenfilled + Plus;
if (screenfilled > toFILL) {
if(inStopfound==0)
{
inStopID = k;
inStopfound =1;
}
}
if(screenfilled > toFillScreen)
{
stopID = k;
// System.out.println("inStopID found: "+inStopID+" Stop ID is: "+stopID);
break;
}
}
renderAnzahl = stopID-startID;
lastinStopID=inStopID;
lastStopID = stopID;
//Wir rendern nun ein Buffered Image mit all den GUI elementen!
int renderheight = effektivHeight; //Renderhöhe des Bildes von der STartID bis ID = 0 (neueste Nachricht = 0)
// System.out.println(iterationHeight);
int startheight = Height - renderheight;
iterationHeight = startheight;
//GUI OFFSET FÜR DIE UNTERE GUI LEISTE:
int offset = 120;
iterationHeight = iterationHeight - offset;
String thisuser = username.getText();
BufferedImage ChatImg = new BufferedImage(mPanel.getWidth(),ScreenHeight-offset+35,BufferedImage.TYPE_INT_ARGB);
Graphics2D ChatG = (Graphics2D)ChatImg.getGraphics();
for (int i = startID; i < stopID; i++)//von Anzahl auf StopID umgestellt
{
Element e = Conversation.findID(i);
GuiElement GuiE = e.getGuiE();
BufferedImage iterationImage = GuiE.getImage(0, 0);
String MessageUsername = GuiE.getUsername();
int Modus = GuiE.getModus(); //Wenn 1 , dann ist die Nachricht von einem Selbst geschrieben
if (Modus == 1) //Wenn man diese Nachricht selbst geschrieben hat:
{
double multi = scrollAlpha;
int leftoffset =(int) (40-8); //WICHTIGES LEFTOFFSET!!!
int Startpoint = Width - GuiE.getWidth(dim) - leftoffset;
ChatG.drawImage(iterationImage, null, Startpoint, iterationHeight);
iterationHeight = iterationHeight + Conversation.findID(i).getGuiE().getHeight(dim);
} else {
ChatG.drawImage(iterationImage, null, 0, iterationHeight);
iterationHeight = iterationHeight + Conversation.findID(i).getGuiE().getHeight(dim);
}
}
work.drawImage(ChatImg, null, 0, 0);
//Nach der Objekt ID suchen, mit der zu rendern be
}
private void doDrawing(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Dimension size = getSize();
render();
g2.drawImage(img, null, 0, 0);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}