Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature may not be available in some browsers.
class Foo extends JPanel
{
Kreis kreis = new Kreis(100,100,50);
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (kreis != null) kreis.maleAuf(g);
}
public void dontDrawItAnymore()
{
kreis = null;
repaint();
}
}
class Foo extends JPanel
{
List<Kreis> kreise = new ArrayList<Kreis>();
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(Kreis kreis:kreise) {
kreis.maleAuf(g);
}
}
public void addKreis(Kreis kreis)
{
kreise.add(kreis);
repaint();
}
public void removeKreis(Kreis kreis)
{
kreise.remove(kreis);
repaint();
}
}
André Uhres hat gesagt.:Erst sagst du, bevor du einen neuen malst muss der alte weg,
und jetzt willst du mehrere malen?
Beschreibe doch einfach mal, was es werden soll,
ohne darauf einzugehen, wie man es verwirklichen könnte.
g.drawOval(x, y, d, d);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(x, y, d, d);
}
public void startAnimation() {
javax.swing.Timer timer = new javax.swing.Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (d == 0 || d > 200) {
x = 150;
y = 150;
d = 10;
} else {
d += 2;
x -= 1;
y -= 1;
}
repaint();
}
});
timer.start();
}
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
class Foo extends JPanel {
public int d=1;
public int x,y;
public void startAnimation() {
javax.swing.Timer timer = new javax.swing.Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ( d == 0 || d > 200) {
x = 150;
y = 150;
d = 10;
} else {
d += 2;
x -= 1;
y -= 1;
}
repaint();
}
});
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(x, y,d,d);
}
}
import javax.swing.*;
public class FooTest {
public static void main(final String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setSize(400, 300);
Foo panel = new Foo();
f.add(panel);
panel.startAnimation();
f.setVisible(true);
}
}
Marco13 hat gesagt.:Wenn du selbstständig d=1 und das delay auf 500 setzt (OHNE SELBSTSTÄNDIG NACHZUDENKEN WAS DAS ZUR FOLGE HAT!!!) darfst du nicht so ungeduldig sein....