In JPanel zeichnen

Thug-Angel

Grünschnabel
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.

Java:
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);
    }
    
}

Java:
 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
 
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
 
Hallo,

deine Klasse funktioniert doch!

Java:
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
 
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
 

Anhänge

  • 24477attachment.gif
    24477attachment.gif
    17,3 KB · Aufrufe: 1.849
Hallo,

schau mal hier:

Java:
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
 
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!
 
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.
 
Zurück