moi, osaan piirtää applettiin jne
mutta mites sitten kun haluaa tehdä saman
ei applettiin (stand-alone). en saa mitään
näkymään swing/awt paneliin...tyhjä valkoinen
ruutu edessä. miten tehdään sama swingillä?
hyvää miten piiretään swingiin? voiko nii tehdä?
applet koodi
------
// My first animation applet
// The moving Ball!
import java.awt.*;
import java.applet.*;
public class BallApplet extends Applet implements Runnable
{
int x_pos = 10;
int y_pos = 100;
int radius = 20;
public void init()
{
// sets applets background color to blue
setBackground(Color.blue);
}
public void start()
{
// define a new Thread
Thread th = new Thread(this);
// start the Thread
th.start();
}
public void stop()
{
}
public void destroy()
{
}
public void run()
{
// lower Threadpriority
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// run a long while true, this means to in our case "allways"
while(true)
{
// changing balls position by increasing x number
// ball moves towards right edge of applet
x_pos ;
//repaint the applet
repaint();
try
{
//stop the Thread for 20ms
Thread.sleep(20);
}
catch(InterruptedException ex)
{
// do nothing
}
// setThread to Maximum priority
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint(Graphics g)
{
// sets ball color red
g.setColor(Color.red);
// paint a filled colored circle
g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
}
mutta mites sitten kun haluaa tehdä saman
ei applettiin (stand-alone). en saa mitään
näkymään swing/awt paneliin...tyhjä valkoinen
ruutu edessä. miten tehdään sama swingillä?
hyvää miten piiretään swingiin? voiko nii tehdä?
applet koodi
------
// My first animation applet
// The moving Ball!
import java.awt.*;
import java.applet.*;
public class BallApplet extends Applet implements Runnable
{
int x_pos = 10;
int y_pos = 100;
int radius = 20;
public void init()
{
// sets applets background color to blue
setBackground(Color.blue);
}
public void start()
{
// define a new Thread
Thread th = new Thread(this);
// start the Thread
th.start();
}
public void stop()
{
}
public void destroy()
{
}
public void run()
{
// lower Threadpriority
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// run a long while true, this means to in our case "allways"
while(true)
{
// changing balls position by increasing x number
// ball moves towards right edge of applet
x_pos ;
//repaint the applet
repaint();
try
{
//stop the Thread for 20ms
Thread.sleep(20);
}
catch(InterruptedException ex)
{
// do nothing
}
// setThread to Maximum priority
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint(Graphics g)
{
// sets ball color red
g.setColor(Color.red);
// paint a filled colored circle
g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
}
Vastaukset 1
Vanhimmat
alla vastaus koodin pätkä:
--
import java.awt.*;
import java.awt.event.*;
public class Example01 extends Frame
{
public Example01()
{
super("2D Example 01");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
}
);
setSize(400, 300);
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawRect(50, 50, 200, 200);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.blue);
g2d.drawRect(75, 75, 300, 200);
}
public static void main(String args[])
{
new Example01();
}
}