Pastebin
81777 geplaatste codes! 

Deprecated: Assigning the return value of new by reference is deprecated in /home/joel/domains/plaatscode.be/public_html/toon.php on line 13

Plaats #33094 (Java)

Gepost op 19-10-2008 15:48 door keffie91
Downloaden als .txt-bestand - Bewerk en post opnieuw
  1. import java.awt.*;
  2. import javax.swing.*;
  3. public class Main extends JFrame{
  4.     //Main Program that represents the GUI
  5.     JMenuBar mBar = new JMenuBar();
  6.     JToolBar tBar = new JToolBar(JToolBar.HORIZONTAL);
  7.    
  8.     JMenu Filemenu = new JMenu("File");
  9.      public JRadioButton line = new JRadioButton("Line");
  10.     JRadioButton rectangle = new JRadioButton("Rectangle");
  11.     JRadioButton eclipse = new JRadioButton("Eclipse");
  12.     JRadioButton arc = new JRadioButton("Arc");
  13.     public Main(){
  14.         setTitle("DK PaintProgram");
  15.         setJMenuBar(mBar);
  16.         getContentPane().add(tBar, BorderLayout.NORTH);
  17.         getContentPane().add(new Painter(),BorderLayout.CENTER);
  18.        
  19.         mBar.add(Filemenu);
  20.        
  21.         tBar.add(line);tBar.addSeparator();tBar.add(rectangle);
  22.         tBar.addSeparator();tBar.add(eclipse);tBar.addSeparator();tBar.add(arc);
  23.         tBar.setFloatable(false);
  24.        
  25.         ButtonGroup g = new ButtonGroup();
  26.         g.add(line);g.add(rectangle);g.add(eclipse);g.add(arc);
  27.        
  28.         setSize(900,900);
  29.         setVisible(true);
  30.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  31.     }
  32.     public static void main(String[] args) {
  33.         new Main();
  34.     }
  35.  
  36. }
  37.  
  38. import javax.swing.*;
  39. import java.awt.*;
  40. import java.awt.event.*;
  41. import java.util.*;
  42. public class Painter extends JPanel {
  43.         private Vector Lines = new Vector(); /* Vector to keep track of  all
  44.         the lines that are drawn */
  45.         private Vector Rectangles = new Vector();/* Vector to keep track of
  46.          all the rectangles that are drawn*/
  47.         private Line newL; // Instantiate a new Line see Line.java
  48.         private Rectangle newR;// Instantiate a new Rectangle see Rectangle.java
  49.        
  50.         public Painter(){
  51.                 setBackground(Color.WHITE);
  52.                 addMouseListener(l1);
  53.         }
  54.  
  55.         public void paintComponent(Graphics g){
  56.             /* draw all the lines in the Vector */
  57.                 super.paintComponent(g);
  58.                 for(ListIterator i=Lines.listIterator();i.hasNext();){
  59.                         ((Line) i.next()).draw(g);
  60.                        
  61.                 }
  62.         }
  63.         MouseListener l1 = new MouseAdapter(){
  64.                         public void mousePressed(MouseEvent e){
  65.                                 newL = new Line(e.getPoint(),e.getPoint());/*
  66.                                  start a new Line and get the point where you
  67.                                  pressed the mouseButton*/
  68.                         }
  69.                         public void mouseReleased(MouseEvent e){
  70.                                 newL.p2 = e.getPoint();/* get the point where
  71.                                  you released the mouse*/
  72.                                 Lines.add(newL);/*add the new line to the
  73.                                  vector so that it can keep track of it*/
  74.                                 repaint();/*repaint, draw all the lines again */
  75.                         }
  76.         };
  77. }
  78.  
  79. import javax.swing.*;
  80. import java.awt.*;
  81. import java.awt.event.*;
  82. import java.util.*;
  83. public class RectanglePainter extends JPanel {
  84.         private Vector Rectangles = new Vector();/* Vector to keep track of
  85.          all the rectangles that are drawn*/
  86.         private Rectangle newR;// Instantiate a new Rectangle see Rectangle.java
  87.        
  88.         public RectanglePainter(){
  89.                 setBackground(Color.WHITE);
  90.                 addMouseListener(l1);
  91.         }
  92.         public void paintComponent(Graphics g){
  93.             /* draw all the rectangles on the screen */
  94.                 super.paintComponent(g);
  95.                 for(ListIterator i=Rectangles.listIterator();i.hasNext();){
  96.                         ((Rectangle) i.next()).draw(g);
  97.                        
  98.                 }
  99.         }
  100.         MouseListener l1 = new MouseAdapter(){
  101.                 public void mousePressed(MouseEvent e){
  102.                         newR = new Rectangle(e.getPoint(),e.getPoint());/*
  103.                                  start a new Rectangle and get the point where you
  104.                                  pressed the mouseButton*/
  105.                 }
  106.                 public void mouseReleased(MouseEvent e){
  107.                         newR.p2 = e.getPoint();/* get the point where
  108.                                  you released the mouse*/
  109.                         Rectangles.add(newR);/*add the new Rectangle to the
  110.                                  vector so that it can keep track of it*/
  111.                         repaint();/*repaint, draw all the lines again */
  112.                 }
  113.         };
  114.        
  115. }
  116.  
  117. import java.awt.*;
  118. import java.io.*;
  119. public class Line implements Serializable {
  120.         public Point p1, p2;/* new Points, the points where you pressed and
  121.          released the mouse*/
  122.        
  123.         public Line(){
  124.                 p1 = new Point();
  125.                 p2 = new Point();
  126.         }
  127.         public Line(Point a, Point b){
  128.                 p1 = new Point(a);
  129.                 p2 = new Point(b);
  130.         }
  131.         public void draw(Graphics g){
  132.             //draw the line
  133.             g.drawLine(p1.x, p1.y, p2.x, p2.y);
  134.         }
  135. }
  136.  
  137. import java.awt.*;
  138. import java.io.*;
  139. public class Rectangle implements Serializable {
  140.         public Point p1, p2;/* new Points, the points where you pressed and
  141.          released the mouse*/
  142.        
  143.         public Rectangle(){
  144.                 p1 = new Point();
  145.                 p2 = new Point();
  146.         }
  147.         public Rectangle(Point a, Point b){
  148.                 p1 = new Point(a);
  149.                 p2 = new Point(b);
  150.         }
  151.         public void draw(Graphics g){
  152.             //draw the rectangle
  153.             g.drawRect(p1.x,p1.y,p2.x-p1.x,p2.y-p1.y);
  154.         }
  155. }
  156.  
© 2014 Sitemasters.be - Gehost door Vircon