Auf Thema antworten

Ich habe den Code überflogen und den des Layouts mal so angepasst, wie du ihn wahrscheinlich haben wolltest. Die Änderungen habe ich kommentiert, so dass du erkennst, was ich gemacht habe.

Es kann durchaus sein, dass du einige Zeilen des GridBag-Gitters anpassen musst.

Und es sind noch einige unschöne Dinge in deinem Code, die du irgendwann mal verbessern solltest. Zum Beispiel solltest du nun statt im Canvas im von mir angelegten JPanel zeichnen. Dazu ist die paintComponent()-Methode zu überschreiben.

[code=Java]package myGui;


import java.util.Vector;

 

//import javax.swing.*;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JSlider;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JTextPane;

import javax.swing.BorderFactory;

 

//import javax.swing.event.*;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

 

//import java.awt.*;

//import java.awt.Canvas;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.Dimension;

 

//import java.awt.event.*;

import java.awt.event.ActionEvent;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.event.ActionListener;

 

import java.awt.image.BufferStrategy;

 

public class CopyOfEditor extends JFrame implements ActionListener, ItemListener, ChangeListener, Runnable, MouseListener, MouseMotionListener{

   

    /*

     * Area of GUI-Elements

     */

   

    private static Thread fThread;

   

    //private Canvas drawArea;

    private JPanel drawArea;

    //private BufferStrategy bufferStrategy;

   

    private JMenuBar mTopMenu;

        private JMenu mFile;

            private JMenuItem mSave;

            private JMenuItem mOpen;

            private JMenuItem mDiscard;

        private JMenu mNew;

            private JMenuItem mRect;

            private JMenuItem mBox;

            private JMenuItem mBarrel;

            private JMenuItem mPoly;

        private JMenu mAction;

            private JMenuItem mMove;

            private JMenuItem mScale;

            private JMenuItem mTurn;

            private JMenuItem mDelete;

           

    private JTextField fileName;

    private JLabel ctrlActionTxt;

   

    private JButton bMoveable;

    private JSlider sDensity;

    private JCheckBox sGrid;

   

    private JTextField tPosX;

    private JTextField tPosY;

    private JTextField tDimX;

    private JTextField tDimY;

   

    private JTextPane chgLog;

    private JScrollPane chgLogScroll;

   

    /*

     *

     */

   

    /*

     * Area of Control Elements

     */

   

    public boolean mouseDown;

    public int mouseX, mouseY, mouseXSpeed, mouseYSpeed;

   

    private enum CtrlAction{

        MOVE,

        SCALE,

        TURN,

        DELETE

    }

    CtrlAction ctrlAction;

   

    int scrollX, scrollY;  

    /*

     *

     */

   

    /*

     * Area of Utility Elements

     */

   

    private long ftime=System.currentTimeMillis();

    private float ftimer=0;   

    /*

     *

     */

   

    /*

     * Area of Initialisation

     */

   

    public static void main(String[] args){

        CopyOfEditor frame = new CopyOfEditor();

        fThread = new Thread(frame);

        fThread.start();

    }

   

    public CopyOfEditor(){

        super("Editor");

        //setSize(800, 600);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       

        //setVisible(true);

       

        initGUI();

        setGUI();

        pack(); //statt setSize()

        setVisible(true);

    }

   

    private void initGUI(){

        //drawArea=new Canvas(); Swing und AWT nicht mischen!

    drawArea = new JPanel();

    drawArea.setPreferredSize(new Dimension(500, 500)); //gewünschte Größe festlegen

        /*drawArea.setSize(500,500);*/ drawArea.setBackground(new Color(255,255,255));

        drawArea.addMouseListener(this); drawArea.addMouseMotionListener(this);

       

        mTopMenu=new JMenuBar();

        mFile=new JMenu("File"); mTopMenu.add(mFile);

            mSave=new JMenuItem("Save"); mFile.add(mSave); mSave.addActionListener(this);

            mOpen=new JMenuItem("Open"); mFile.add(mOpen); mOpen.addActionListener(this);

            mDiscard=new JMenuItem("Discard"); mFile.add(mDiscard); mDiscard.addActionListener(this);

        mNew=new JMenu("New Obj"); mTopMenu.add(mNew);

            mRect=new JMenuItem("Rect"); mNew.add(mRect); mRect.addActionListener(this);

            mBox=new JMenuItem("Box"); mNew.add(mBox); mBox.addActionListener(this);

            mBarrel=new JMenuItem("Barrel"); mNew.add(mBarrel); mBarrel.addActionListener(this);

            mPoly=new JMenuItem("Polygon"); mNew.add(mPoly); mPoly.addActionListener(this);

        mAction=new JMenu("Action"); mTopMenu.add(mAction);

            mMove=new JMenuItem("Move"); mAction.add(mMove); mMove.addActionListener(this);

            mScale=new JMenuItem("Scale"); mAction.add(mScale); mScale.addActionListener(this);

            mTurn=new JMenuItem("Turn"); mAction.add(mTurn); mTurn.addActionListener(this);

            mDelete=new JMenuItem("Delete"); mAction.add(mDelete); mDelete.addActionListener(this);


        setJMenuBar(mTopMenu); //Zeile 200-202 auskommentiert!

           

        fileName=new JTextField();

        ctrlActionTxt=new JLabel("   [ Move ]"); ctrlAction=CtrlAction.MOVE;

       

        bMoveable=new JButton("..."); bMoveable.addActionListener(this);

        sDensity=new JSlider(0,100); sDensity.addChangeListener(this);

            sDensity.setPaintTicks(true);

            sDensity.setMajorTickSpacing(25);

            sDensity.setMinorTickSpacing(5);

        sGrid=new JCheckBox("Draw Grid"); sGrid.addActionListener(this);

       

        tPosX=new JTextField(16); tPosX.addActionListener(this);

        tPosY=new JTextField(16); tPosY.addActionListener(this);

        tDimX=new JTextField(16); tDimX.addActionListener(this);

        tDimY=new JTextField(16); tDimY.addActionListener(this);

       

        chgLog=new JTextPane();

        chgLog.setContentType("text/html");

        chgLog.setEditable(false);

        chgLogScroll=new JScrollPane(chgLog);

        chgLogScroll.setPreferredSize(new Dimension(200, 200)); //gewünschte Größe des Log

        chgLogScroll.setMinimumSize(new Dimension(200, 200)); //Mindestgröße des Log

        chgLog.setText(chgLog.getText().substring(0,findSubstring(chgLog.getText(),"</p>"))+"New Session Started<br><br><br><br><br><br><br><br>"+chgLog.getText().substring(findSubstring(chgLog.getText(),"</p>"),chgLog.getText().length()));

    }

   

    private void setGUI(){

        GridBagConstraints c;

       

        JPanel mainPanel=new JPanel(new GridBagLayout());

        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); //einen leeren 10-Pixel-Rahmen

        this.add(mainPanel);

       

        /*c=new GridBagConstraints();

        c.gridx=0; c.gridy=0; c.gridwidth=2;

        mainPanel.add(mTopMenu, c);*/ //dafür wird setJMenuBar() benutzt!

       

        c=new GridBagConstraints();

        c.gridx=2; c.gridy=0; c.fill=GridBagConstraints.BOTH;

        mainPanel.add(ctrlActionTxt,c);

       

        c=new GridBagConstraints();

        c.gridx=0; c.gridy=1; c.gridwidth=3; c.gridheight=10; c.fill=GridBagConstraints.BOTH;

    c.weightx = 1.0; c.weighty = 1.0; //Gewichtung

        mainPanel.add(drawArea,c);

        //drawArea.createBufferStrategy(2); //ein JPanel ist bereits gepuffert, Canvas ersetzt

        //bufferStrategy=drawArea.getBufferStrategy();

       

        c=new GridBagConstraints();

        c.gridx=0; c.gridy=11;

        c.insets=new Insets(10,0,0,0);

        mainPanel.add(new JLabel("File Name: "),c);

       

        c=new GridBagConstraints();

        c.gridx=1; c.gridy=11; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;

        c.insets=new Insets(10,0,0,0);

        mainPanel.add(fileName,c);

       

        /*

         *

         */

           

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=1; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;

        c.insets=new Insets(0,10,0,0);

        mainPanel.add(bMoveable,c);

       

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=2; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;

        c.insets=new Insets(30,10,0,0);

        mainPanel.add(new JLabel("Density:"),c);

       

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=3; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;

        c.insets=new Insets(10,10,10,10);

        mainPanel.add(sDensity,c);

       

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=4; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;

        c.insets=new Insets(20,10,10,0);

        mainPanel.add(new JLabel("Position:"),c);

       

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=5;

        c.insets=new Insets(0,10,0,10);

        mainPanel.add(new JLabel("X:"),c);

       

        c=new GridBagConstraints();

        c.gridx=4; c.gridy=5; c.fill=GridBagConstraints.HORIZONTAL;

        mainPanel.add(tPosX,c);

       

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=6;

        c.insets=new Insets(0,10,0,10);

        mainPanel.add(new JLabel("Y:"),c);

       

        c=new GridBagConstraints();

        c.gridx=4; c.gridy=6; c.fill=GridBagConstraints.HORIZONTAL;

        mainPanel.add(tPosY,c);

       

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=7; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;

        c.insets=new Insets(10,10,10,0);

        mainPanel.add(new JLabel("Dimension:"),c);

       

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=8;

        c.insets=new Insets(0,10,0,10);

        mainPanel.add(new JLabel("X:"),c);

       

        c=new GridBagConstraints();

        c.gridx=4; c.gridy=8; c.fill=GridBagConstraints.HORIZONTAL;

        mainPanel.add(tDimX,c);

       

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=9;

        c.insets=new Insets(0,10,0,10);

        mainPanel.add(new JLabel("Y:"),c);

       

        c=new GridBagConstraints();

        c.gridx=4; c.gridy=9; c.fill=GridBagConstraints.HORIZONTAL;

        mainPanel.add(tDimY,c);

       

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=10; c.gridwidth=2; c.fill=GridBagConstraints.BOTH;

        c.insets=new Insets(30,10,0,0);

        mainPanel.add(chgLogScroll,c);

       

        c=new GridBagConstraints();

        c.gridx=3; c.gridy=11; c.gridwidth=2; c.fill=GridBagConstraints.HORIZONTAL;

        c.insets=new Insets(8,10,0,0);

        mainPanel.add(sGrid,c);

    }

   

    /*

     *

     */

   

    /*

     * Area of Control

     */

   

    public void mousePressed(MouseEvent e){

        mouseDown=true;

       

        setVisible(true);

    }

    public void mouseReleased(MouseEvent e){

        mouseDown=false;

       

        setVisible(true);

    }

 

    public void mouseMoved(MouseEvent e){

        mouseXSpeed=e.getX()-mouseX; mouseYSpeed=e.getY()-mouseY;

        mouseYSpeed=0;

        mouseX=e.getX(); mouseY=e.getY();

       

        setVisible(true);

    }

    public void mouseDragged(MouseEvent e){

        mouseXSpeed=e.getX()-mouseX; mouseYSpeed=e.getY()-mouseY;

        mouseX=e.getX(); mouseY=e.getY();

       

        setVisible(true);

    }

   

    public void actionPerformed(ActionEvent e){    

        Object src=e.getSource();

       

        if(src==mRect){

            addToChgLog("Rect added");

        } else if(src==mPoly){

            addToChgLog("Polygon added");

        } else if(src==mBox){

            addToChgLog("Box added");

        } else if(src==mBarrel){

            addToChgLog("Barrel added");

           

        } else if(src==mMove){

            ctrlAction=CtrlAction.MOVE;

            ctrlActionTxt.setText("   [ Move ]");

            addToChgLog("Mode changed to [MOVE]");

        } else if(src==mScale){

            ctrlAction=CtrlAction.SCALE;

            ctrlActionTxt.setText("   [ Scale ]");

            addToChgLog("Mode changed to [SCALE]");

        } else if(src==mDelete){

            ctrlAction=CtrlAction.DELETE;

            ctrlActionTxt.setText("   [ Delete ]");

            addToChgLog("Mode changed to [DELETE]");

        } else if(src==mTurn){

            ctrlAction=CtrlAction.TURN;

            ctrlActionTxt.setText("   [ Turn ]");

            addToChgLog("Mode changed to [TURN]");

           

        } else if(src==bMoveable){

            addToChgLog("Moveability changed to [Unmoveable]");

 

        } else if(src==mSave){

            String path=fileName.getText();

            if(path.equals("")){

                path="Test.txt";

                fileName.setText(path);

            }

 

            addToChgLog("Saved to "+path);

        } else if(src==mOpen){

            setInactive();

           

            String path=fileName.getText();

            if(path.equals("")){

                path="Test.txt";

                fileName.setText(path);

            }

 

            addToChgLog("Loaded from "+path);

        } else if(src==mDiscard){

            setInactive();

            addToChgLog("Discarded file");

           

        } else if(src==tPosX){

            try{

                float oldX=0;

                float newX=Float.parseFloat(tPosX.getText());

 

                addToChgLog("Moved X from "+oldX+" to "+newX);

            }catch(NumberFormatException pxe){

                pxe.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!

            }

        } else if(src==tPosY){

            try{

                float oldY=0;

                float newY=Float.parseFloat(tPosY.getText());

           

                addToChgLog("Moved Y from "+oldY+" to "+newY);

            }catch(NumberFormatException pye){

                pye.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!

            }

        } else if(src==tDimX){

            try{

                float oldX=0;

                float newX=Float.parseFloat(tDimX.getText());

               

                addToChgLog("Scaled X from "+oldX+" to "+newX);

            }catch(NumberFormatException dxe){

                dxe.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!

            }

        } else if(src==tDimY){

            try{

                float oldY=0;

                float newY=Float.parseFloat(tDimY.getText());

               

                addToChgLog("Scaled Y from "+oldY+" to "+newY);

            }catch(NumberFormatException dye){

                dye.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!

            }

        }

    }

   

    public void itemStateChanged(ItemEvent e){

       

    }

   

    public void stateChanged(ChangeEvent e){

        Object src=e.getSource();

    }

   

    /*

     *

     */

   

    /*

     * Area of Work Stuff

     */

   

    public void run(){

        //Graphics g; //wird nicht benutzt und auch nicht benötigt

        ftimer=1000f/60f;

       

        int dragX=0, dragY=0;

       

        scrollX=250;

        scrollY=250;

       

        setVisible(true);

       

        while(true){

            /*if(this.ftimer>0){

                while(System.currentTimeMillis()-ftime<ftimer){ }

                ftime=System.currentTimeMillis();

            }*/        

            try{

                int suspTime=(int)(ftimer-(System.currentTimeMillis()-ftime));

                if (suspTime>0)

                    fThread.sleep(suspTime);

                    ftime=System.currentTimeMillis();

            }catch(InterruptedException e){

                e.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!

            }

           

            /*try{

                if(!bufferStrategy.contentsLost())

                    bufferStrategy.show();

               

                g=bufferStrategy.getDrawGraphics();

                draw((Graphics2D) g);

                g.dispose();

            }catch(NullPointerException e){

            e.printStackTrace(); //Exceptions niemals unbehandelt lassen! Wenigstens den StackTrace ausgeben!

            }*/

           

            if(inRect(mouseX,mouseY,475,210,25,80))

                scrollX--;

            if(inRect(mouseX,mouseY,0,210,25,80))

                scrollX++;

            if(inRect(mouseX,mouseY,210,0,80,25))

                scrollY++;

            if(inRect(mouseX,mouseY,210,475,80,25))

                scrollY--;

           

            mouseXSpeed=0;

            mouseYSpeed=0;

        }

    }

   

    private void setActive(){      

        tPosX.setText("0");

        tPosY.setText("0");

       

        tDimX.setText("0");

        tDimY.setText("0");

       

        sDensity.setValue(50);

    }

   

    public void setInactive(){

        bMoveable.setText("...");

       

        sDensity.setValue(50);

       

        tPosX.setText("");

        tPosY.setText("");

        tDimX.setText("");

        tDimY.setText("");

    }

   

    public void draw(Graphics2D g){

        g.setColor(g.getBackground());

        g.fillRect(0,0,500,500);

       

        g.setColor(new Color(175,175,175));

        g.drawLine(scrollX-5,scrollY-5,scrollX+5,scrollY+5);

        g.drawLine(scrollX+5,scrollY-5,scrollX-5,scrollY+5);

       

        int gridSize=25;

        if(sGrid.isSelected())

            for(int i=0; i<500+gridSize; i+=gridSize){

                g.drawLine(i+(scrollX % gridSize), 0, i+(scrollX % gridSize), 500);

                g.drawLine(0, i+(scrollY % gridSize), 500, i+(scrollY % gridSize));

            }

    }

   

    private void addToChgLog(String newText){

        String oldText=chgLog.getText();

       

        String part1, part2;

        int pos;

       

        pos=findSubstring(oldText,"</p>");

       

        part1=oldText.substring(0,pos);

        part2=oldText.substring(pos,oldText.length());

       

        chgLog.setText(part1+"<br>"+newText+part2);

    }

   

    public int findSubstring(String src, String part){

        boolean found;

       

        for(int i=0; i<src.length()-part.length(); i++){

            found=true;

            for(int j=0; j<part.length(); j++)

                if(src.charAt(i+j)!=part.charAt(j))

                    found=false;

           

            if(found)

                return i;

        }

       

        return -1;

    }

   

    /*

     *

     */

   

    /*

     * Area of Creation Stuff

     */

   

    public boolean inRect(int x, int y, int x1, int y1, int b, int h){

        /*if(x>x1 && x<x1+b && y>y1 && y<y1+h)

             true;

        return false;*/

   

        return x>x1 && x<x1+b && y>y1 && y<y1+h; //so gehts einfacher

    }

   

    /*

     *

     */

   

    /*

     * Area of Useless Stuff

     */

   

    public void mouseClicked(MouseEvent e){}

    public void mouseEntered(MouseEvent e){}

    public void mouseExited(MouseEvent e){}

   

    /*

     *

     */

}[/code]



Oben