JPaint. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.vector;

JPaint import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Vector; import javax.swing.JFileChooser; import javax.swing...
Author: Dayna Jacobs
0 downloads 0 Views 24KB Size
JPaint

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Vector; import javax.swing.JFileChooser; import javax.swing.filechooser.*; import java.io.*; public class JPaint extends JFrame implements ActionListener, IGraphicAction { GrfPanel grafik; JList grfliste; JButton BnOpen; JButton BnSave; JToggleButton BnNone; // Wechselschalter JToggleButton BnLine; JToggleButton BnCircle; JToggleButton BnRect; JButton BnTest; // Popupmenues lk=lokal JMenuItem lkTop; JMenuItem lkDown; JMenuItem lkFlash; JMenuItem lkDelete; JMenuItem lkDeleteAll; Timer myTimer=null; int anzTimer=0;

String sFileName; public JPaint() { setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); setSize(800,500); setGUI(); sFileName=""; } private void setGUI() { JPanel bnPanel = new JPanel(); BnOpen = new JButton("Open");

BnSave = new JButton("Save"); BnOpen.setFont( new Font("Arial", Font.BOLD,18) ); BnSave.setFont( new Font("Arial", Font.BOLD,18) ); } // setGUI public int getButton(MouseEvent e) { int mode = e.getModifiers(); if ((mode & e.BUTTON1_MASK) > 0) { return 1; } if ((mode & e.BUTTON2_MASK) > 0) { return 2; } if ((mode & e.BUTTON3_MASK) > 0) { return 3; } return 0; } // getButton private void grfliste_Clicked(MouseEvent e) { } // grfliste_Clicked

// Popup-Menue fuer Kopieren Oeffnen und Loeschen public void popupEditMenu( MouseEvent me ) { }//popupMenu

@Override public void actionPerformed(ActionEvent e) { } // actionPerformed

private void openFile() { JFileChooser jFileChooser1 = new JFileChooser(); MyFileFilter grfFilter; grfFilter = new MyFileFilter("grf", "Grafik-Datei (*.grf)"); jFileChooser1.setMultiSelectionEnabled(false); jFileChooser1.setCurrentDirectory( new File( (String) System.getProperties().get("user.dir") ) ); jFileChooser1.addChoosableFileFilter( grfFilter ); if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) { // Rufen Sie openFile auf, um Text aus einer Datei in die TextArea zu laden sFileName = jFileChooser1.getSelectedFile().getPath(); }

} private void loadFile() { FileInputStream fis; ObjectInputStream ois = null; /* try { setTitle(sFileName); System.out.println(" Ende des Lesens " ); } catch (Exception e) { e.printStackTrace(); } finally { if (ois != null) { try { ois.close(); } catch (IOException ioe) {} } } */ } // loadFile private void saveFile() { JFileChooser jFileChooser1 = new JFileChooser(); MyFileFilter grfFilter; grfFilter = new MyFileFilter("grf", "Grafik-Datei (*.grf)"); jFileChooser1.setMultiSelectionEnabled(false); jFileChooser1.setCurrentDirectory( new File( (String) System.getProperties().get("user.dir") ) ); jFileChooser1.addChoosableFileFilter( grfFilter ); if (JFileChooser.APPROVE_OPTION == jFileChooser1.showSaveDialog(this)) { // Rufen Sie openFile auf, um Text aus einer Datei in die TextArea zu laden sFileName = jFileChooser1.getSelectedFile().getPath(); setTitle(sFileName); } } private void saveGraphics() { FileOutputStream fout; ObjectOutputStream oos = null; /* try { } catch (Exception e) { e.printStackTrace(); } finally {

if (oos != null) { try { oos.flush(); } catch (IOException ioe) {} try { oos.close(); } catch (IOException ioe) {} } } */ } // save public void insertGraphic(int x1, int y1, int x2, int y2) { } private void addGrafik(IGrafik grf) { } private void callTest() { } public static void main(String[] args) { JPaint f = new JPaint(); f.setVisible(true); } }

Zusatz: grfliste.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { grfliste_Clicked(e); } }); JSplitPane split = new JSplitPane(JSplitPane.???,false, Comp1, Comp2 ); this.getContentPane().add( split, BorderLayout.CENTER ); split.setDividerLocation(150);

GrfPanel import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Vector;

public class GrfPanel extends JPanel{ public GrfPanel() { this.setBackground(Color.red); setMouse(); } /* private void setMouse() { this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { this_mouseDragged(e); } public void mouseMoved(MouseEvent e) { this_mouseMoved(e); } }); this.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { this_mouseClicked(e); } public void mousePressed(MouseEvent e) { this_mousePressed(e); } public void mouseReleased(MouseEvent e) { this_mouseReleased(e); } }); } // setMouse */ public void paint (Graphics g) { int w = getWidth(); int h = getHeight(); // Loeschen des Bildschirms g.setColor(Color.WHITE); g.fillRect(0,0, getWidth(), getHeight() ); System.out.println("onpaint"); } }

void this_mouseClicked(MouseEvent e) { //System.out.println("mouseClicked"); }

void this_mouseMoved(MouseEvent e) { //System.out.println("mouseMoved"); // nur Aktion, wenn mousetaste oben ist } void this_mouseDragged(MouseEvent e) { //System.out.println("mouseDragged"); } void this_mousePressed(MouseEvent e) { System.out.println("mousePressed"); } void this_mouseReleased(MouseEvent e) { // System.out.println("mouseReleased"); } }

Klasse GObject

import java.awt.*; import java.io.Serializable; public class GObject { protected void setLineWidth(Graphics g) { if (linewidth>1) { Graphics2D g2 = (Graphics2D) g; g2.setStroke( new BasicStroke(linewidth) ); } else { Graphics2D g2 = (Graphics2D) g; g2.setStroke( new BasicStroke(1.0f) ); } } }

Circle

import javax.swing.*; import java.awt.event.*; import java.awt.*;

public class MyDialogCircle extends JFrame { private JLabel labelX= new JLabel("X"); private JLabel labelY= new JLabel("Y"); private JLabel labelR= new JLabel("Radius");

private JLabel labelLineWidth= new JLabel("Strichstärke"); private JLabel labelColor= new JLabel("Farbe"); private JTextField x= new JTextField(); private JTextField y= new JTextField(); private JTextField radius= new JTextField(); private JTextField linewidth= new JTextField(); private JPanel pColor= new JPanel(); Circle circle; JButton BnOk; JButton BnESC;

public MyDialogCircle() { setTitle("Bearbeiten eines Kreises"); setSize(600,300); result=false; setGUI(); setVisible(true); } private void setGUI() { this.getContentPane().setLayout( new GridBagLayout() ); x.setText( Integer.toString(circle.x) ); y.setText( Integer.toString(circle.y) ); radius.setText( Integer.toString(circle.r) ); linewidth.setText( Double.toString(circle.linewidth) ); pColor.setBackground(circle.color); labelX.setFont( new Font("Arial", Font.BOLD,18) ); labelY.setFont( new Font("Arial", Font.BOLD,18) ); labelR.setFont( new Font("Arial", Font.BOLD,18) ); labelLineWidth.setFont( new Font("Arial", Font.BOLD,18) ); labelColor.setFont( new Font("Arial", Font.BOLD,18) ); x.setFont( new Font("Arial", Font.BOLD,18) ); y.setFont( new Font("Arial", Font.BOLD,18) ); radius.setFont( new Font("Arial", Font.BOLD,18) );

linewidth.setFont( new Font("Arial", Font.BOLD,18) ); pColor.setPreferredSize( new Dimension(10,30));

this.getContentPane().add(labelX, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30,20,0,0), 0, 0)); this.getContentPane().add(x, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(30,40,0,20), 00, 0)); // ------------------------------this.getContentPane().add(labelY, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30,20,0,0), 0, 0)); this.getContentPane().add(y, new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(30,40,0,20), 00, 0)); // -------------------------------

this.getContentPane().add(labelR, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30,20,0,0), 0, 0)); this.getContentPane().add(radius, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(30,40,0,20), 00, 0)); // -------------------------------

this.getContentPane().add(labelLineWidth, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30,20,0,0), 0, 0)); this.getContentPane().add(linewidth, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(30,40,0,20), 00, 0)); // ------------------------------this.getContentPane().add(labelColor,

new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(30,20,0,0), 0, 0)); this.getContentPane().add(pColor, new GridBagConstraints(3, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(30,40,0,20), 00, 0)); // ------------------------------JPanel pn = new JPanel(); pn.setLayout( new FlowLayout() );

BnOk = new JButton("Ok"); BnOk.setFont( new Font("Arial", Font.BOLD,18) ); pn.add( BnOk); BnESC = new JButton("Abbruch"); BnESC.setFont( new Font("Arial", Font.BOLD,18) ); pn.add( BnESC); // gleiche Groesse BnOk.setPreferredSize( BnESC.getPreferredSize() ); this.getContentPane().add(pn, new GridBagConstraints(0, 3, 4, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(25,0,0,0), 0, 0)); BnOk.addActionListener(this); BnESC.addActionListener(this); pColor.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { chooseColor(); } }); } // setGUI @Override public void actionPerformed(ActionEvent e) { } private void BnESC_click() { setVisible(false); dispose(); }

private void BnOk_click() { setVisible(false); dispose(); }

private void chooseColor() { } }

MyFileFilter import java.io.File; import javax.swing.filechooser.FileFilter; public class MyFileFilter extends FileFilter { String extension; String description; /*

public MyFileFilter() { this((String) null, (String) null); }

*/ public MyFileFilter(String extension, String description) { this.extension = extension; this.description = description; }

@Override public String getDescription() { return description; } @Override public boolean accept(File f) { boolean bOkay=false; if ( f.isDirectory() ) return true; else { if ( f.getName().toLowerCase().endsWith( extension ) ) return true; } return false; } } // MyFileFilter

Suggest Documents