Quelltext vom Applet

  • Themenstarter Themenstarter Missy
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
Veilleicht hilft dir dies weiter:
Code:
//package applet;
/*
 * Anipolygon2.java
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Anipolygon2 extends JApplet implements ActionListener{
    private JPanel animation;
    private Timer timer;
    private int appWidth;
    private int appHeight;
    private AnimatedPoints ani1, ani2, ani3, ani4, ani5;
    /** Initializes the applet Anipolygon2 */
    public void init() {
        ani1 = new AnimatedPoints(10, 10, 2, 3, 1, 1, 5);
        ani2 = new AnimatedPoints(300, 100, 3, 2, -1, 1, 10);
        ani3 = new AnimatedPoints(30, 100, 3, 2, -1, 1, 10);
        ani4 = new AnimatedPoints(300, 10, 2, 2, 1, 1, 15);
        ani5 = new AnimatedPoints(500, 300, 4, 3, 1, 1, 7);
        animation = new JPanel(){
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D)g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setPaint(new GradientPaint(30,50,Color.blue,100,100,Color.red,true));
                int[] xx = new int[]{ani1.getx(),ani2.getx(),ani3.getx(),ani4.getx(),ani5.getx()};
                int[] yy = new int[]{ani1.gety(),ani2.gety(),ani3.gety(),ani4.gety(),ani5.gety()};
                g2d.fillPolygon(xx,yy,5);
            }
        };
        getContentPane().add(animation);
        appWidth = 350;
        appHeight = 198;
        timer = new Timer(2, this);
        timer.start();
    }
    public void actionPerformed(ActionEvent e) {animation.repaint();}
    class AnimatedPoints extends JLabel implements ActionListener{
        private int deltaX = 2;
        private int deltaY = 3;
        private int directionX = 1;
        private int directionY = 1;
        private int nextX;
        private int nextY;
        public AnimatedPoints( int startX, int startY, int deltaX, int deltaY,
                int directionX, int directionY, int delay){
            this.deltaX = deltaX;
            this.deltaY = deltaY;
            this.directionX = directionX;
            this.directionY = directionY;
            setLocation(startX, startY);
            new Timer(delay, this).start();
        }
        public int getx(){return nextX;}
        public int gety(){return nextY;}
        public void actionPerformed(ActionEvent e) {
            Container parent = getParent();
            //  Determine next X position
            nextX = getLocation().x + (deltaX * directionX);
            if (nextX < 0) {
                nextX = 0;
                directionX *= -1;
            }
            if ( nextX + getSize().width > appWidth) {
                nextX = appWidth - getSize().width;
                directionX *= -1;
            }
            //  Determine next Y position
            nextY = getLocation().y + (deltaY * directionY);
            if (nextY < 0) {
                nextY = 0;
                directionY *= -1;
            }
            if ( nextY + getSize().height > appHeight) {
                nextY = appHeight - getSize().height;
                directionY *= -1;
            }
            setLocation(nextX, nextY);  //move the Point
        }
    }
}
 
Danke!
Habe den Code jetzt etwas abgeändert so wie ihn benötige.
Ansich funktioniert es so auch, aber es sieht irgendwie so aus als ob das mit dem Thread für die Verzögerung nicht richtig läuft, sondern immernoch über Timer. Hmm...



Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Poly extends JApplet implements ActionListener{
    private JPanel animation;
  
    private int appWidth;
    private int appHeight;
    private AnimatedPoints ani1, ani2, ani3, ani4, ani5;
    
    public void init() {
    	ani1 = new AnimatedPoints(10, 10, 2, 3, 1, 1, 5);
        ani2 = new AnimatedPoints(300, 100, 3, 2, -1, 1, 10);
        ani3 = new AnimatedPoints(30, 100, 3, 2, -1, 1, 10);
        ani4 = new AnimatedPoints(300, 10, 2, 2, 1, 1, 15);
        ani5 = new AnimatedPoints(500, 300, 4, 3, 1, 1, 7);
        animation = new JPanel(){
            /**
			 * 
			 */
			private static final long serialVersionUID = -7218061871397162294L;
			
			public void paint (Graphics g){
               
          
                
                int[] x = new int[]{ani1.getx(), ani2.getx(),ani3.getx(),ani4.getx(),ani5.getx()};
                int[] y = new int[]{ani1.gety(), ani2.gety(),ani3.gety(),ani4.gety(),ani5.gety()};
                g.drawPolygon(x,y,5);
            }
        };
        getContentPane().add(animation);
        appWidth = 200;
        appHeight = 200;
        Thread rt = new AppletRepainter(this, 50); 
        rt.start();	

    }
    public void actionPerformed(ActionEvent e) {animation.repaint();}
    class AnimatedPoints extends JLabel implements ActionListener{
        /**
		 * 
		 */
		private static final long serialVersionUID = -8410527416198789843L;
		private int deltaX = 2;
        private int deltaY = 3;
        private int directionX = 1;
        private int directionY = 1;
        private int nextX;
        private int nextY;
        public AnimatedPoints( int startX, int startY, int deltaX, int deltaY,
                int directionX, int directionY, int delay){
            this.deltaX = deltaX;
            this.deltaY = deltaY;
            this.directionX = directionX;
            this.directionY = directionY;
            setLocation(startX, startY);
            new Timer(delay, this).start();
        }
        public int getx(){return nextX;}
        public int gety(){return nextY;}
        public void actionPerformed(ActionEvent e) {
        
    
            nextX = getLocation().x + (deltaX * directionX);
            if (nextX < 0) {
                nextX = 0;
                directionX *= -1;
            }
            if ( nextX + getSize().width > appWidth) {
                nextX = appWidth - getSize().width;
                directionX *= -1;
            }
          
            nextY = getLocation().y + (deltaY * directionY);
            if (nextY < 0) {
                nextY = 0;
                directionY *= -1;
            }
            if ( nextY + getSize().height > appHeight) {
                nextY = appHeight - getSize().height;
                directionY *= -1;
            }
            setLocation(nextX, nextY);
        }
    }
  public class AppletRepainter extends Thread {
    	
	JApplet a;
 	int msecs;
    	
  	public AppletRepainter(JApplet a, int fps) { 
  		this.a = a;
  		msecs = 1000 / fps;
  	}
    	
  	public void run(){
  		while (true){
  			a.repaint();
  			try{
  				sleep(msecs); 
  			}
  			catch (InterruptedException e){
 				System.out.println("InterruptedException");
	
  			}}	
    	
    }
  

  }}
[/b]
 
Sanix hat gesagt.:
Notfalls könntest du auch das Applet dekompilieren.

Klappt aber auch nicht immer. Es scheint compiler zu geben, welche die Variablennamen und Methodennamen verstümmeln, so dass das Verstehen des Quelltextes erschwert wird. Dann hast du nach dem Dekompilieren nur noch ein Programm in der Hand, welches so vielsagende Variablen/Klassen/Methodennamen hat, wie A, B, ..., Z, AA, AB, ..., AZ

Das macht dann auch keinen Spaß mehr 😉
 
Sanix hat gesagt.:
Notfalls könntest du auch das Applet dekompilieren.
Hier ist es:
Code:
//package applet;
/*
 * Anipolygon.java
 */
// Decompiled by DJ v3.8.8.85 Copyright 2005 Atanas Neshkov  Date: 30/06/2006 09:04:31
// Home Page : [url]http://members.fortunecity.com/neshkov/dj.html[/url]  - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name:   Anipolygon.java

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Anipolygon extends Applet
        implements MouseListener, MouseMotionListener, Runnable {
    
    public Anipolygon() {
    }
    
    public void init() {
        setBackground(Color.black);
        addMouseListener(this);
        addMouseMotionListener(this);
        setSize(300, 300);
        for(currentpoint = 0; currentpoint < 50; currentpoint++) {
            x[currentpoint] = 0;
            y[currentpoint] = 0;
        }
        
        currentpoint = 0;
        numpoints = 0;
        done = false;
        animating = false;
        rmod = (int)(Math.random() * 10D) - 5;
        grmod = (int)(Math.random() * 10D) - 5;
        bmod = (int)(Math.random() * 10D) - 5;
        r = (int)(Math.random() * 256D);
        gr = (int)(Math.random() * 256D);
        b = (int)(Math.random() * 256D);
    }
    
    public void paint(Graphics g) {
        if(done) {
            g.setColor(new Color(r, gr, b));
            g.drawPolygon(x, y, numpoints);
            r += rmod;
            if(r >= 255 || r <= 0) {
                r -= rmod;
                rmod *= -1;
            }
            gr += grmod;
            if(gr >= 255 || gr <= 0) {
                gr -= grmod;
                grmod *= -1;
            }
            b += bmod;
            if(b >= 255 || b <= 0) {
                b -= bmod;
                bmod *= -1;
            }
        } else {
            for(currentpoint = 1; currentpoint <= numpoints; currentpoint++)
                g.drawLine(x[currentpoint], y[currentpoint], x[currentpoint - 1], y[currentpoint - 1]);
            
        }
    }
    
    private int findpt(int i, int j) {
        for(int k = 0; k < numpoints; k++)
            if(Math.abs(x[k] - i) <= 3 && Math.abs(y[k] - j) <= 3)
                return k;
        
        return -1;
    }
    
    public void mousePressed(MouseEvent mouseevent) {
        if(!done) {
            x[numpoints + 1] = mouseevent.getX();
            y[numpoints + 1] = mouseevent.getY();
            if(findpt(x[numpoints + 1], y[numpoints + 1]) == 0 && numpoints >= 3) {
                x[numpoints + 1] = x[0];
                y[numpoints + 1] = y[0];
                done = true;
                aniX = new int[numpoints];
                aniY = new int[numpoints];
                for(currentpoint = 0; currentpoint < numpoints; currentpoint++) {
                    aniX[currentpoint] = (int)(Math.random() * 5D) - 2;
                    aniY[currentpoint] = (int)(Math.random() * 5D) - 2;
                }
                
                numpoints--;
            }
            if(numpoints > 1)
                repaint();
            numpoints++;
        } else {
            currentpoint = findpt(mouseevent.getX(), mouseevent.getY());
            if(currentpoint >= 0 || !animating)
                start();
            else
                stop();
        }
    }
    
    public void mouseReleased(MouseEvent mouseevent) {
        if(done) {
            if(currentpoint >= 0) {
                x[currentpoint] = mouseevent.getX();
                y[currentpoint] = mouseevent.getY();
                repaint();
                stop();
            }
            currentpoint = -1;
        }
    }
    
    public void mouseClicked(MouseEvent mouseevent) {
    }
    
    public void mouseEntered(MouseEvent mouseevent) {
        setBackground(Color.white);
        if(done)
            stop();
    }
    
    public void mouseExited(MouseEvent mouseevent) {
        setBackground(Color.black);
        if(done)
            start();
    }
    
    public void mouseDragged(MouseEvent mouseevent) {
        if(done && currentpoint >= 0) {
            x[currentpoint] = mouseevent.getX();
            y[currentpoint] = mouseevent.getY();
            repaint();
        }
    }
    
    public void mouseMoved(MouseEvent mouseevent) {
        if(!done) {
            x[numpoints] = mouseevent.getX();
            y[numpoints] = mouseevent.getY();
            repaint();
        }
    }
    
    public void start() {
        animate = new Thread(this);
        animate.start();
    }
    
    public synchronized void stop() {
        animate = null;
        notify();
    }
    
    public synchronized void run() {
        while(Thread.currentThread() == animate) {
            repaint();
            for(int i = 0; i < numpoints; i++) {
                if(i != currentpoint) {
                    x[i] += aniX[i];
                    y[i] += aniY[i];
                }
                if(x[i] <= 0 || x[i] >= 300)
                    aniX[i] *= -1;
                if(y[i] <= 0 || y[i] >= 300)
                    aniY[i] *= -1;
            }
            
            try {
                wait(10L);
            } catch(InterruptedException interruptedexception) {
                return;
            }
        }
    }
    
    private static int x[] = new int[50];
    private static int y[] = new int[50];
    boolean done;
    boolean animating;
    private static int r;
    private static int gr;
    private static int b;
    private static int rmod;
    private static int grmod;
    private static int bmod;
    private static int numpoints;
    private static int currentpoint;
    int aniX[];
    int aniY[];
    Thread animate;
    
}
 
moormaster hat gesagt.:
Klappt aber auch nicht immer. Es scheint compiler zu geben, welche die Variablennamen und Methodennamen verstümmeln, so dass das Verstehen des Quelltextes erschwert wird.
Das sind dann keine Compiler, so ein Tool nennt sich Obfuscator.
 
L-ectron-X hat gesagt.:
moormaster hat gesagt.:
Klappt aber auch nicht immer. Es scheint compiler zu geben, welche die Variablennamen und Methodennamen verstümmeln, so dass das Verstehen des Quelltextes erschwert wird.
Das sind dann keine Compiler, so ein Tool nennt sich Obfuscator.

Gut zu wissen 😉 Es scheint seinen Zweck jedenfalls recht gut zu erfüllen, wenns darum geht, den Quelltext zu verbergen 😉 Da lohnt es sich nur noch bei aussergewöhnlichen Dingen, den Quelltext zu analysieren; ansonsten wird es in den meisten Fällen wo einfacher sein, sich selber darüber Gedanken zu machen, wie man sowas programmiert 😉
 
moormaster hat gesagt.:
L-ectron-X hat gesagt.:
moormaster hat gesagt.:
Klappt
aber auch nicht immer. Es scheint compiler zu geben, welche die Variablennamen und Methodennamen verstümmeln, so dass das Verstehen des Quelltextes erschwert wird.
Das sind dann keine Compiler, so ein Tool nennt sich Obfuscator.

Gut zu wissen 😉
...
In der alten Javainsel war da ein gutes Kapitel dazu drinn! Wurde jedoch in der neuen Aussgabe fast vollständig gekürtzt! 🙁
Der klägliche Überrest des so guten kapittels ist:
http://www.galileocomputing.de/openbook/avainsel5/javainsel26_006.htm#Xxx999379
 
michi2 hat gesagt.:
moormaster hat gesagt.:
L-ectron-X hat gesagt.:
moormaster hat gesagt.:
Klappt
aber auch nicht immer. Es scheint compiler zu geben, welche die Variablennamen und Methodennamen verstümmeln, so dass das Verstehen des Quelltextes erschwert wird.
Das sind dann keine Compiler, so ein Tool nennt sich Obfuscator.

Gut zu wissen 😉
...
In der alten Javainsel war da ein gutes Kapitel dazu drinn! Wurde jedoch in der neuen Aussgabe fast vollständig gekürtzt! 🙁
Der klägliche Überrest des so guten kapittels ist:
http://www.galileocomputing.de/openbook/avainsel5/javainsel26_006.htm#Xxx999379

Nanu? Die 4. Auflage wurde völlig ausm Netz entfernt... vor nicht allzulanger Zeit durfte man neben der 5. auch noch die 4. lesen 😉
 
Ja, hat mich auch gewundert, die Auflsge 3 war bis vor kurtzem auch noch auf dem Server! Müsste die Auflage 4 noch irgentwo auf dem rechner haben.
 
Also, ich würde das mit dem Timer gern komplett raus haben und nur über den Thread die Verzögerung haben wollen. Allerdings bewegt sich das Polygon nicht mehr, wenn ich Zeile 58 streiche.
Also scheint da mit dem Thread was falsch zu sein.
Dann hab ich noch so meine Probleme beim Verständnis der Klasse Animated Points. Wäre lieb, wenn mir da jemand sagt, was die einzelnen Zeilen der Klasse bedeuten
 
Also, ich würde das mit dem Timer gern komplett raus haben und nur über den Thread die Verzögerung haben wollen. Allerdings bewegt sich das Polygon nicht mehr, wenn ich Zeile 58 streiche.
Also scheint da mit dem Thread was falsch zu sein.
Dann hab ich noch so meine Probleme beim Verständnis der Klasse Animated Points. Wäre lieb, wenn mir da jemand sagt, was die einzelnen Zeilen der Klasse bedeuten
 
Missy hat gesagt.:
..Dann hab ich noch so meine Probleme beim Verständnis der Klasse Animated Points..
Die Klasse AnimatedPoints ruft actionPerformed im delay-Takt auf ("new Timer(delay, this).start();").
Dort werden die nächsten x und y Koordinaten vom Point ermittelt und der Point dorthin
bewegt ("setLocation(nextX, nextY);").
Der Point wird hier verkörpert durch die Location von einem fiktiven JLabel ("class AnimatedPoints extends JLabel ").
Ich weiss übrigens immer noch nicht warum du den Timer unbedingt durch einen Thread ersetzen willst,
der Timer ist ja ein Thread, von daher ist das für mich wie Speck und Schweinefleisch.
 
oki, dann hat sich das hier wohl erledigt, aber trotzdem danke soweit
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben