![]() |
| | Themen-Optionen | Thema durchsuchen |
| | Nach oben #1 |
| Gast
Beiträge: n/a
|
Hallo! Ich möchte für eine kleine Simulation ein Programm schreiben, dass mir auf ein JPanel Kreise mit bestimmten Koordinaten und Radien zeichnet. Auf Wunsch sende ich das Programm per Mail. Das Programm besteht z.Z. aus vier Fenstern (mainWnd, canvasWnd, controllerWnd, addBallWnd). Nachdem der Benutzer die Eigenschaften des Balles im Fenster addBallWnd festgelegt und bestätigt hat, soll auf dem JPanel von canvasWnd der Kreis gezeichnet werden. Von besonderem Interesse sind dabei die Zeilen 104 - 120 bzw. 147 - 162. Code: import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class ball extends JPanel
{
private int x = 0, y = 0, r = 100, xMax = 500, yMax = 300;
private Color clr = new Color( 0, 0, 0 );
/*
* hier stehen die ueblichen setXXX und getXXX Funktionen
* sowie die deklaration der Funktion addComponent
*
*/
public ball()
{
/*
* first part of main windows declarations
*/
final JButton buttonAddBall = new JButton("(add ball)");
final JButton buttonController = new JButton("controller");
/*
* first part of canvas windows declarations
*/
final JFrame canvasWnd = new JFrame("canvas");
/*
* controller window declarations
*/
// controllerWnd
final JFrame controllerWnd = new JFrame("controller");
controllerWnd.setLocation(50, 120);
controllerWnd.setSize(150, 100);
// controllerWndCont
Container controllerWndCont = controllerWnd.getContentPane();
// controllerWndGBL
GridBagLayout controllerWndGBL = new GridBagLayout();
controllerWndCont.setLayout( controllerWndGBL );
// JLabels
JLabel labelControllerRadius = new JLabel("radius");
JLabel labelControllerX = new JLabel("x");
JLabel labelControllerY = new JLabel("y");
final JLabel labelControllerRadius1 = new JLabel( Integer.toString(getRadius()) );
final JLabel labelControllerX1 = new JLabel( Integer.toString(getX()) );
final JLabel labelControllerY1 = new JLabel( Integer.toString(getY()) );
// addComponents
// x, y, w, h, wx, wy
addComponent( controllerWndCont, controllerWndGBL, labelControllerRadius, 0, 0, 1, 1, 1, 1 );
addComponent( controllerWndCont, controllerWndGBL, labelControllerRadius1, 1, 0, 1, 1, 1, 1 );
addComponent( controllerWndCont, controllerWndGBL, labelControllerX, 0, 1, 1, 1, 1, 1 );
addComponent( controllerWndCont, controllerWndGBL, labelControllerX1, 1, 1, 1, 1, 1, 1 );
addComponent( controllerWndCont, controllerWndGBL, labelControllerY, 0, 2, 1, 1, 1, 1 );
addComponent( controllerWndCont, controllerWndGBL, labelControllerY1, 1, 2, 1, 1, 1, 1 );
/*
* add ball window declarations
*/
// addBallWnd
final JFrame addBallWnd = new JFrame("add ball");
addBallWnd.setLocation(50, 240);
addBallWnd.setSize(150, 170);
// addBallWndCont
Container addBallWndCont = addBallWnd.getContentPane();
// addBallWndGBL
GridBagLayout addBallWndGBL = new GridBagLayout();
addBallWndCont.setLayout( addBallWndGBL );
// JLabels
JLabel labelAddBallRadius = new JLabel("radius");
JLabel labelAddBallX = new JLabel("x");
JLabel labelAddBallY = new JLabel("y");
JLabel labelAddBallColorR = new JLabel("red");
JLabel labelAddBallColorG = new JLabel("green");
JLabel labelAddBallColorB = new JLabel("blue");
// JTextFields
final JTextField textFieldAddBallRadius = new JTextField("100");
final JTextField textFieldAddBallX = new JTextField("0");
final JTextField textFieldAddBallY = new JTextField("0");
final JTextField textFieldAddBallColorR = new JTextField("0");
final JTextField textFieldAddBallColorG = new JTextField("0");
final JTextField textFieldAddBallColorB = new JTextField("0");
// JButtons
JButton buttonAddBallOK = new JButton("OK");
buttonAddBallOK.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
setRadius( Integer.parseInt(textFieldAddBallRadius.getText().trim()) );
setX( Integer.parseInt(textFieldAddBallX.getText().trim()) );
setY( Integer.parseInt(textFieldAddBallY.getText().trim()) );
setColor
(
Integer.parseInt(textFieldAddBallColorR.getText().trim()),
Integer.parseInt(textFieldAddBallColorG.getText().trim()),
Integer.parseInt(textFieldAddBallColorB.getText().trim())
);
labelControllerRadius1.setText( Integer.toString(getRadius()) );
labelControllerX1.setText( Integer.toString(getX()) );
labelControllerY1.setText( Integer.toString(getY()) );
canvasWnd.repaint(); // seems like it does not work :(
}
});
// addComponents
// x, y, w, h, wx, wy
addComponent( addBallWndCont, addBallWndGBL, labelAddBallRadius, 0, 0, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, textFieldAddBallRadius, 1, 0, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, labelAddBallX, 0, 1, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, textFieldAddBallX, 1, 1, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, labelAddBallY, 0, 2, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, textFieldAddBallY, 1, 2, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, labelAddBallColorR, 0, 3, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, textFieldAddBallColorR, 1, 3, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, labelAddBallColorG, 0, 4, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, textFieldAddBallColorG, 1, 4, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, labelAddBallColorB, 0, 5, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, textFieldAddBallColorB, 1, 5, 1, 1, 1, 1 );
addComponent( addBallWndCont, addBallWndGBL, buttonAddBallOK, 1, 6, 2, 1, 1, 1 );
/*
* canvas window declarations
*/
// canvasWnd
JPanel canvasPanel = new JPanel()
{
public void paint( Graphics g )
{
super.paint( g );
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g2.setColor( getColor() );
g2.draw
(
// P(x,y:( obere, linke Ecke > nicht der Mittelpunkt!
// x, y, w, h, start, extend, type
new Arc2D.Double( getX(), getY(), 2*getRadius(), 2*getRadius(), 0, 360, Arc2D.OPEN )
);
}
};
canvasWnd.setLocation(220, 120);
canvasWnd.setSize(640, 480);
// canvasWndCont
Container canvasWndCont = canvasWnd.getContentPane();
// canvasWndGBL
GridBagLayout canvasWndGBL = new GridBagLayout();
canvasWndCont.setLayout( canvasWndGBL );
// JButtons
final JButton buttonShowCanvas = new JButton("show canvas");
buttonShowCanvas.setToolTipText("display canvas containing balls");
buttonShowCanvas.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
if ( buttonShowCanvas.getText().equals("close canvas") )
{
canvasWnd.setVisible( false );
addBallWnd.setVisible( false );
buttonShowCanvas.setText("open canvas");
buttonAddBall.setText("(add ball)");
}
else
{
canvasWnd.setVisible( true );
buttonShowCanvas.setText("close canvas");
buttonAddBall.setText("add ball");
}
}
});
// addComponents
addComponent( canvasWndCont, canvasWndGBL, canvasPanel, 0, 0, 1, 1, 1, 1 );
/*
* main window declarations
*/
// mainWnd
JFrame mainWnd = new JFrame("simBall");
mainWnd.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
mainWnd.setLocation(50, 30);
mainWnd.setSize( 300, 70 );
Container mainWndCont = mainWnd.getContentPane();
GridBagLayout mainWndGBL = new GridBagLayout();
mainWndCont.setLayout( mainWndGBL );
// JButtons
JButton buttonClose = new JButton("close");
buttonClose.setToolTipText("close me to free some memory");
buttonClose.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
System.exit(0);
}
});
buttonAddBall.setToolTipText("enter coordinates, radius and color for a new ball");
buttonAddBall.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
if ( buttonAddBall.getText().equals("add ball") )
{
buttonAddBall.setText("(add ball)");
addBallWnd.setVisible( true );
}
}
});
buttonController.setToolTipText("view coordinates, radius and color of last ball");
buttonController.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
if ( buttonController.getText().equals("controller") )
{
buttonController.setText("(controller)");
controllerWnd.setVisible( true );
}
}
});
// addComponents for main window
// x, y, w, h, wx, wy
addComponent( mainWndCont, mainWndGBL, buttonShowCanvas, 0, 0, 1, 1, 1, 1 );
addComponent( mainWndCont, mainWndGBL, buttonAddBall, 1, 0, 1, 1, 1, 1 );
addComponent( mainWndCont, mainWndGBL, buttonController, 0, 1, 1, 1, 1, 1 );
addComponent( mainWndCont, mainWndGBL, buttonClose, 1, 1, 1, 1, 1, 1 );
mainWnd.setVisible( true );
}
public static void main( String args[] )
{
new ball();
}
}
|
|
| | Nach oben #2 |
| Chefkoch-Mod Registriert seit: 30.05.2004
Beiträge: 432
|
Ich denke mal, am einfachsten ist es, wenn Du das Graphics-Objekt löscht und die aktuellen Dinge neu zeichnest. Mir ist jedenfalls keine bessere Möglichkeit bekannt.
__________________ Denk mal darüber nach... Lars ACHTUNG: wenn ich von Klassen spreche, könnte ich auch deren Instanzen meinen. www.linuxforen.de +++ www.macuser.de +++ www.mrunix.de +++ www.lmprojects.de |
| | |
| | Nach oben #3 |
| Gast
Beiträge: n/a
|
Hallo! Kannst du mir mal ein kleines Code Snippet aufschreiben? Im Moment weiß ich nicht, wie ich das Graphics Objekt löschen kann. Man muss ja dabei beachten, dass ich nur das JPanel oder nur das JFrame eines der vier Fenster (canvasWnd) neu zeichnen möchte ... Weil sich nur dort Dinge verändern. Nur dort werden Kreise gezeichnet .. |
|
![]() |
| Lesezeichen |
| Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1) | |
| Themen-Optionen | Thema durchsuchen |
| |
Ähnliche Themen | ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| [PHP] thumbnails erstellen - kleine Funktion | Jann Hendrik | Tutorials | 2 | 16.01.2008 10:38 |
| [PHP] Zeitformate, Datum und Co | Jann Hendrik | Tutorials | 0 | 05.12.2006 18:48 |
| [SUCHE] Funktion erstellen | Jan | Gesuche | 5 | 30.10.2006 10:09 |
| [PHP] FTP-Funktionen in PHP nutzen | MrNiceGuy | Tutorials | 0 | 24.05.2006 14:18 |
| funktion mit variablenname aufrufen? | Niedi | PHP-Programmierung | 4 | 20.09.2005 23:05 |