import javax.swing.*; public class EmptyFrame { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("An Empty Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
import javax.swing.*; import java.awt.BorderLayout; public class FramePanel { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel p = new JPanel(new BorderLayout()); JButton b = new JButton("Bonjour"); p.add(b,BorderLayout.CENTER); JLabel l = new JLabel("South"); p.add(l,BorderLayout.SOUTH); frame.add(p); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Frame avec Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
import java.awt.geom.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.util.Random; public class Olympics extends JComponent { Random r; Olympics () { r = new Random(); } public void paintComponent(Graphics g) { int dia=50; int stX=r.nextInt(180); int stY=r.nextInt(280); Graphics2D g2 = (Graphics2D)g; g2.setStroke(new BasicStroke(4)); g2.setColor(Color.CYAN); g2.drawOval(stX,stY,dia,dia); g2.setColor(Color.BLACK); g2.drawOval((int)(stX+dia*1.1),stY,dia,dia); g2.setColor(Color.RED); g2.drawOval((int)(stX+dia*1.1*2),stY,dia,dia); g2.setColor(Color.YELLOW); g2.drawOval((int)(stX+dia*1.1*.5),stY+dia/2,dia,dia); g2.setColor(Color.GREEN); g2.drawOval((int)(stX+dia*1.1*1.5),stY+dia/2,dia,dia); } public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(new BorderLayout()); Olympics oly = new Olympics(); panel.add(oly); frame.add(panel); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Frame avec Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
import java.awt.geom.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.util.Random; public class OlyLogo extends JComponent { Random r; Image img; double heightWidthRatio; OlyLogo (String imgName) { r = new Random(); img = getToolkit().getImage(imgName); try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(img, 0); tracker.waitForID(0); } catch (Exception e) {} double width = img.getWidth(this); double height = img.getHeight(this); heightWidthRatio=height/width; } public void paintComponent(Graphics g) { int dia=50; int stX=r.nextInt(180); int stY=r.nextInt(280); Graphics2D g2 = (Graphics2D)g; g2.drawImage(img,stX,stY,100,(int)(100*heightWidthRatio),this); } public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(new BorderLayout()); panel.setBackground(Color.WHITE); OlyLogo oly = new OlyLogo("oly.gif"); panel.add(oly); frame.add(panel); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Frame avec Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
import java.awt.geom.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.util.Random; public class OlyAnimated extends JComponent implements Runnable{ Random r; Image img; double heightWidthRatio; Thread animator; OlyAnimated (String imgName) { r = new Random(); img = getToolkit().getImage(imgName); try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(img, 0); tracker.waitForID(0); } catch (Exception e) {} double width = img.getWidth(this); double height = img.getHeight(this); heightWidthRatio=height/width; animator = new Thread(this); animator.start(); } public void run() { while (1==1) { try { Thread.sleep(100); repaint(); } catch (InterruptedException e) { System.out.println(e); } } } public void paintComponent(Graphics g) { int dia=50; int stX=r.nextInt(180); int stY=r.nextInt(280); Graphics2D g2 = (Graphics2D)g; g2.drawImage(img,stX,stY,100,(int)(100*heightWidthRatio),this); } public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(new BorderLayout()); panel.setBackground(Color.WHITE); OlyAnimated oly = new OlyAnimated("oly.gif"); panel.add(oly); frame.add(panel); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Frame avec Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
import java.awt.geom.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.util.Random; public class OlyAnimated2 extends JComponent implements Runnable{ Random r; Image img; double heightWidthRatio; Thread animator; int stX; int stY; double dir=0; int stepSize=4; int rate=20; OlyAnimated2 (String imgName) { r = new Random(); img = getToolkit().getImage(imgName); try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(img, 0); tracker.waitForID(0); } catch (Exception e) {} double width = img.getWidth(this); double height = img.getHeight(this); stX=r.nextInt(180); stY=r.nextInt(280); dir=r.nextDouble()*Math.PI*2; heightWidthRatio=height/width; animator = new Thread(this); animator.start(); } public void run() { while (1==1) { try { Thread.sleep(rate); repaint(); } catch (InterruptedException e) { System.out.println(e); } } } public void paintComponent(Graphics g) { int speed=8; //dir = dir+ ((r.nextDouble()-.5)*.1); stX=(int)(stX+Math.sin(dir)*stepSize); stY=(int)(stY+Math.cos(dir)*stepSize); if (stX<0 || stY <0 || stX>180 || stY>280) { dir=dir+Math.PI + Math.PI*(r.nextDouble()-.5); stX=(int)(stX+Math.sin(dir)*stepSize); stY=(int)(stY+Math.cos(dir)*stepSize); } Graphics2D g2 = (Graphics2D)g; g2.drawImage(img,stX,stY,100,(int)(100*heightWidthRatio),this); } public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(new BorderLayout()); panel.setBackground(Color.WHITE); OlyAnimated2 oly = new OlyAnimated2("oly.gif"); panel.add(oly); frame.add(panel); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Frame avec Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
import java.awt.geom.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import java.util.Random; public class Pong extends JPanel implements KeyTracker { Random r; Image img; double heightWidthRatio; Thread animator; int stX; int stY; double dir=0; int stepSize=4; int rate=20; Pong (String imgName) { r = new Random(); img = getToolkit().getImage(imgName); try { MediaTracker tracker = new MediaTracker(this); tracker.addImage(img, 0); tracker.waitForID(0); } catch (Exception e) {} double width = img.getWidth(this); double height = img.getHeight(this); stX=r.nextInt(180); stY=r.nextInt(280); dir=r.nextDouble()*Math.PI*2; heightWidthRatio=height/width; animator = new Thread(this); animator.start(); } public void run() { while (1==1) { try { Thread.sleep(rate); repaint(); } catch (InterruptedException e) { System.out.println(e); } } } public void paintComponent(Graphics g) { int speed=8; //dir = dir+ ((r.nextDouble()-.5)*.1); stX=(int)(stX+Math.sin(dir)*stepSize); stY=(int)(stY+Math.cos(dir)*stepSize); if (stX<0 || stY <0 || stX>180 || stY>280) { dir=dir+Math.PI + Math.PI*(r.nextDouble()-.5); stX=(int)(stX+Math.sin(dir)*stepSize); stY=(int)(stY+Math.cos(dir)*stepSize); } Graphics2D g2 = (Graphics2D)g; g2.drawImage(img,stX,stY,100,(int)(100*heightWidthRatio),this); } public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(new BorderLayout()); panel.setBackground(Color.WHITE); Pong oly = new Pong("oly.gif"); panel.add(oly); frame.add(panel); final int FRAME_WIDTH = 300; final int FRAME_HEIGHT = 400; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Frame avec Panel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }