tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
8
ZUGRIFFE
17277
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Thug-Angel Thug-Angel ist offline Mitglied
    Registriert seit
    Mar 2005
    Beiträge
    16
    Hi, hab ein Problem, kann mir bitte jemand helfen?
    Ich weiß nicht wie ich in einem JPanel zeichnen soll, hier mal ein Versuch, hab auch gegoogelt, aber kam irgendwie nicht klar damit...

    das erste Programm Teil ist von der Klasse MyDraw, der andere von MainForm, dort zeichen ich das ganze Gerüst etc.

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    import java.awt.Graphics;
    import java.awt.Panel;
    import javax.swing.JPanel;
    /*
     * MyDraw.java
     *
     * Created on 23. Mai 2006, 16:13
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Thug Angel
     */
    public class MyDraw extends JPanel{
        
        /** Creates a new instance of MyDraw */
        public MyDraw() {
        }
        
        public void paintComponent (Graphics g)
        {
            super.paintComponent(g);
            g.drawLine(0,0,20,20);
        }
        
    }

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
            
            jPanel1.setVisible(true);
            jPanel1.add(new MyDraw());
            jPanel1.addNotify();
            jPanel1.repaint();
            
            
        }

    Danke im Vorraus!

    mfg
    Thug-Angel
     

  2. #2
    flashray flashray ist offline Mitglied Rubin
    Registriert seit
    Sep 2005
    Ort
    Mannheim
    Beiträge
    1.325
     

  3. #3
    Thug-Angel Thug-Angel ist offline Mitglied
    Registriert seit
    Mar 2005
    Beiträge
    16
    sry. da werde ich leider auch nicht schlauer, weiß einfach nicht wie ich void paint(Graphics g) auf das panel übertragen muss, damit was im panel gezeichnet wird...

    schonmal danke im vorraus!

    mfg
     

  4. #4
    flashray flashray ist offline Mitglied Rubin
    Registriert seit
    Sep 2005
    Ort
    Mannheim
    Beiträge
    1.325
    Hallo,

    deine Klasse funktioniert doch!

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    import java.awt.Graphics;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Example extends JFrame {
     
        public Example() {
            super("Example");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setAlwaysOnTop(true);
            this.setLocationByPlatform(true);
            this.setSize(320, 480);
     
            this.add(new MyDraw());
     
            this.setVisible(true);
        }
     
        public static void main(String[] args) {
            new Example();
        }
     
        public class MyDraw extends JPanel {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawLine(0, 0, 200, 200);
            }
        }
    }


    Vg Erdal
     

  5. #5
    Thug-Angel Thug-Angel ist offline Mitglied
    Registriert seit
    Mar 2005
    Beiträge
    16
    ja, auf einen neuen frame, jedoch habe ich schon in einem frame mit radio buttons etc.

    hier ein bild.

    danke für die Mühe ^_^

    mfg
    Miniaturansicht angehängter Grafiken Miniaturansicht angehängter Grafiken In JPanel  zeichnen-24477attachment.gif  
     

  6. #6
    flashray flashray ist offline Mitglied Rubin
    Registriert seit
    Sep 2005
    Ort
    Mannheim
    Beiträge
    1.325
    Hallo,

    schau mal hier:

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    
    import java.awt.*;
     
    import javax.swing.*;
     
    public class Example extends JFrame {
     
        private JPanel checkPanel = new JPanel();
     
        public Example() {
            super("Example");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setAlwaysOnTop(true);
            this.setLocationByPlatform(true);
            this.setSize(320, 480);
     
            checkPanel.setLayout(new GridLayout(3, 3));
            for (int i = 0; i < 9; i++) {
                checkPanel.add(new JCheckBox("" + i));
            }
     
            this.add(new JButton("Button"), BorderLayout.NORTH);
            this.add(new JLabel("Label"), BorderLayout.WEST);
            this.add(new JButton("Button"), BorderLayout.EAST);
            this.add(checkPanel, BorderLayout.CENTER);
            this.add(new MyDraw(), BorderLayout.SOUTH);
     
            this.setVisible(true);
        }
     
        public static void main(String[] args) {
            new Example();
        }
     
        public class MyDraw extends JPanel {
            public MyDraw() {
                this.setPreferredSize(new Dimension(320, 100));
                this.setBorder(BorderFactory.createLoweredBevelBorder());
            }
     
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawLine(0, 0, 100, 100);
            }
        }
    }


    Vg Erdal
     

  7. #7
    ipspy4711 ipspy4711 ist offline Mitglied
    Registriert seit
    Jul 2011
    Beiträge
    17
    Der Post ist jetzt schon 5 Jahre alt, aber vielleicht liest trotzdem jemand:

    Wie muss ich es anstellen, dass in dem Bereich, in dem im obigen Beispiel nun die Linie ist, unendlich viele Komponenten geaddet werden können?

    Vielen Dank schon vorab!
     

  8. #8
    genodeftest genodeftest ist offline Mitglied Brillant
    Registriert seit
    Jun 2009
    Beiträge
    870
    was möchtest du tun? bitte erklär das noch mal genauer.

    btw: neuer Fall, neuer Thread. Es wäre schon angemessen gewesen, einen neuen Thread zu öffnen.
     
    Code bitte so einfügen: [java]System.out.println("Hallo");[/java] (Analog für andere Programmiersprachen)
    Code java:
    1
    
    System.out.println("Hallo");
    hilfreich zu Java: Really Big Index, Java ist auch eine Insel Band 1 und Band 2.
    ___________
    Ubuntu Bug #1: Microsoft has a majority market share
    Casecon: Projekt leiser Käse

  9. #9
    ipspy4711 ipspy4711 ist offline Mitglied
    Registriert seit
    Jul 2011
    Beiträge
    17
    Sorry, bin neu hier.

    Neuer Thread:
    http://www.tutorials.de/swing-java2d...-zeichnen.html
     

Ähnliche Themen

  1. Antworten: 8
    Letzter Beitrag: 07.12.10, 10:34
  2. In JPanel sofort zeichnen
    Von amiganer im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 1
    Letzter Beitrag: 24.12.08, 23:26
  3. Rechteck über JPanel zeichnen flackert
    Von Ryu20 im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 5
    Letzter Beitrag: 10.04.08, 17:31
  4. JPanel und JComponent - Zeichnen
    Von CerebrosuS im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 0
    Letzter Beitrag: 11.10.07, 17:43
  5. auf JPanel zeichnen
    Von Apon im Forum Java
    Antworten: 1
    Letzter Beitrag: 07.03.05, 12:37