Problem mit dem graphics-Objekt

Status
Nicht offen für weitere Antworten.

Angel

Aktives Mitglied
da man hier leider keine Dateien anhängen kann, poste ich den Code erst mal hier rein.

Es gibt 2 Klassen:

BesIF
UBSp26052004

Hier erst mal der Inhalt der BesIF:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
import java.io.*;

public class BesIF
{
  GUI gui = new GUI();

  public static void main(String[] args)
  {
    BesIF besIF1 = new BesIF();
  }

  public BesIF()
  {
      gui.setVisible(true);
  }

  public class GUI extends JFrame
  {

    GuiCan can = new GuiCan();

    //Bildung der Menüleiste
    public JMenuBar mb;

    //Bildung der Menügruppen "File"
    public JMenu mFile;

    //Inhalt der Menügruppe "File"
    public JMenuItem miOpen;
    public JMenuItem miClose;
    public JMenuItem miExit;

    Vector vec = new Vector();

    public GUI()
    {
      this.setSize(500, 500);
      this.setTitle("Canvas Test");

      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      this.setLocation( (d.width - getSize().width) / 2,
                       (d.height - getSize().height) / 2);

      //Bildung des Kompletten Menüs inkl. der Bezeichungen
      this.mb = new JMenuBar();

      this.mFile = new JMenu("File");
      this.miOpen = new JMenuItem("Open ...");
      this.miClose = new JMenuItem("Save ...");
      this.miExit = new JMenuItem("Exit");

      //Der Menügruppe "File" werden die Einträge bzw. Menüitems zugewiesen
      this.mFile.add(miOpen);
      this.mFile.add(miClose);
      //Trennlinie zwischen den Menüitem "Close" und "Exit"
      this.mFile.addSeparator();
      this.mFile.add(miExit);

      //Menügruppe "File" wird der Menüleiste hinzugefügt
      this.mb.add(mFile);
      //setzt die menüleiste "mb" auch wirklich als menüleiste in das Frame --> ist notwendig, damit die Leiste überhaupt angezeigt wird
      this.setJMenuBar(mb);

      can.setBackground(Color.black);
      this.getContentPane().add(BorderLayout.CENTER, can);

      this.miOpen.addActionListener(new ActionListener() {

        public void actionPerformed(final ActionEvent e) {
          //Objekt von SaveDlg: Öffnen-Modus
          new SaveDlg(FileDialog.LOAD);
        }
      });

      //Listener für Close
      this.miClose.addActionListener(new ActionListener() {

        public void actionPerformed(final ActionEvent e) {
          //Objekt von SaveDlg: Speichern-Modus
          new SaveDlg(FileDialog.SAVE);
        }
      });

      //Listener für Exit
      this.miExit.addActionListener(new ActionListener() {

        public void actionPerformed(final ActionEvent e) {
          dispose();
          System.exit(0);
        }
      });

      this.addWindowListener(new WindowAdapter()
      {
        public void windowClosing(WindowEvent e)
        {
          dispose();
          System.exit(0);
        }
      });
    }

    public final class SaveDlg extends JFrame //begin SaveDlg Class
      {
        private String selectedFile = null;
        private String modusstring = null;

        public SaveDlg(final int modus) //modus = 0 (Open); modus = 1 (Save)
        {
          //Swing FileDialog
          final JFileChooser chooser = new JFileChooser();

          if (modus==0)
          {
            modusstring = "Bitte wählen Sie eine Datei aus:";
            chooser.setDialogTitle(modusstring);
            final int returnVal = chooser.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION)
            {
              selectedFile = "" + chooser.getSelectedFile();

              if (modus == 0)
                Filereader(selectedFile);

              if (modus == 1)
                Filewriter(selectedFile);
            }
          }
          else
          {
              modusstring = "Bitte geben Sie einen neuen Dateinamen ein:";
              chooser.setDialogTitle(modusstring);
              final int returnVal = chooser.showSaveDialog(null);
              if (returnVal == JFileChooser.APPROVE_OPTION)
              {
                selectedFile = "" + chooser.getSelectedFile();

                if (modus == 0)
                  Filereader(selectedFile);

                if (modus == 1)
                  Filewriter(selectedFile);
              }
          }
        }


        public void Filereader(final String datpfad)
        {
          final File fi = new File(datpfad);
          try
          {
            final ObjectInputStream in = new ObjectInputStream(new FileInputStream(fi));

            vec = (Vector)in.readObject();
            in.close();
            can.repaint();
          }
          catch (Exception ex)
          {
            System.out.println(ex);
          }
        }

        public void Filewriter(final String datpfad){
          final File fi = new File(datpfad);
          try
          {
            final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fi));

            out.writeObject(vec);
            out.close();
            can.repaint();
          }
          catch (Exception ex)
          {
            System.out.println(ex);
          }
        }

      }//End of SaveDlg Class

    public class GuiCan extends JPanel
    {

      int mausy, mausx, h, b, hold, bold;

      int framey, framex;

      public GuiCan()
      {
        this.addMouseListener(new MouseAdapter()
        {
          public void mousePressed(MouseEvent e)
          {
            //Graphics g = can.getGraphics();
            mausx = e.getX();
            mausy = e.getY();
            createNewObject();
          }
        });

        hold=0;bold=0;
      }

      public void paint(Graphics graphics)
      {
        Graphics2D g = (Graphics2D) graphics;

        framey=gui.getHeight();
        framex=gui.getWidth();
        b=10*framex/100;
        h=10*framey/100;


        //pruefungstest.UBSp26052004 usb = new pruefungstest.UBSp26052004(Color.green,Color.blue);
        for (int i=0;i<vec.size();i++)
        {
          //(((UBSp26052004)vec.get(i))).drawFigure(g,mausx,mausy,h,b);
          (((UBSp26052004)vec.get(i))).drawFigure(g,h,b);
        }
      }

      public void createNewObject()
      {

        //vec.add(new UBSp26052004(Color.green,Color.blue));
        vec.add(new UBSp26052004(Color.green,Color.blue, mausx, mausy, h, b));
        repaint();
      }

    }
  }
}
 
und jetzt der Inhalt der UBSp26052004:

Code:
import java.awt.*;
import java.io.*;

public class UBSp26052004 implements Serializable
{
private int x,y,l, hold, bold;
private Color clDach, clBalken;

  public UBSp26052004(Color clD, Color clB)
  {
    clDach = clD;
    clBalken = clB;
  }

    public UBSp26052004(Color clD, Color clB, int x, int y, int hold, int bold)
  {
    this.x=x;this.y=y;
    this.hold=hold;this.bold=bold;
    clDach = clD;
    clBalken = clB;
  }

  public void drawFigure(Graphics graphics, int h, int b)
  {
    Graphics2D g = (Graphics2D) graphics;
    drawFigure(g,x,y,h,b);
  }

  public void drawFigure(Graphics2D g, int x, int y, int h, int b)
  {
    int xa=x*b/bold,ya=y*h/hold;//skalierung des Mittelpunktes

    g.setColor(clBalken);
    g.drawRect(xa-b*2/10,ya,b*4/10,h*6/10);

    int polx[] = new int[3];
    int poly[] = new int[3];

    polx[0] = xa;
    polx[1] = xa+b*5/10;
    polx[2] = xa-b*5/10;

    poly[0] = ya-h*4/10;
    poly[1] = ya;
    poly[2] = ya;

    g.setColor(Color.red);
    g.fillOval(xa,ya,1,1);

    g.setColor(Color.black);
    g.drawPolygon(polx,poly,3);

    g.drawOval(xa-b*2/10,ya-h*2/10,b*4/10,h*4/10);
  }
}
 
und jetzt das Problem dazu:

ich habe da in diesem Fenster komische Effekte. Wenn ich das Menü aufklappe und wieder zumache, erscheint es doppelt oder die Menüitems, die Grafik davon, bleiben auf dem Canvas wie ein Foto zurück :bahnhof:

ich bereite mich grad auf meine Java-Prüfung für morgen vor und hoffe, das mir jemand Tips geben kann, was da nicht stimmt, ich weiß jetzt momentan echt nicht weiter.

danke im vorraus....
 
Würde mal sagen, du musst noch den Hintergrund löschen:

Code:
      public void paint(Graphics graphics){
        Graphics2D g = (Graphics2D) graphics; 
        g.setColor( getBackground() );
        g.fillRect( 0, 0, getWidth(), getHeight() );
 
Beni hat gesagt.:
Würde mal sagen, du musst noch den Hintergrund löschen:

Code:
      public void paint(Graphics graphics){
        Graphics2D g = (Graphics2D) graphics; 
        g.setColor( getBackground() );
        g.fillRect( 0, 0, getWidth(), getHeight() );

*Kopf an die Wand haue*

danke dir, daran hab ich ne gedacht 🙂

@P3AC3MAK3R:

ok :?

wollts halt übersichtlich machen, bin nicht Postingzahlgeil.....
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben