import javax.swing.*;
public class Arbeitsblatt extends JComponent
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Schrubber´s TestTool");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(new Form(10,10,50,50));//Strich
//frame.add(new Form(110,110,90,90,1));//Rechteck
frame.add(new Form(50,50,40));//Kreis
}
}
import java.awt.Graphics;
public class Form extends Arbeitsblatt
{
Daten d = new Daten();
public Form(int x1, int y1, int x2, int y2)//Linie
{
d.setX1(x1);
d.setY1(y1);
d.setX2(x2);
d.setY2(y2);
d.setModus(1);
}
public Form(int x1, int y1, int x2, int y2, int modus)//Rechteck
{
d.setX1(x1);
d.setY1(y1);
d.setX2(x2);
d.setY2(y2);
d.setModus(2);
}
public Form(int x1, int y1, int radius)//Kreis
{
d.setX1(x1);
d.setY1(y1);
d.setHeight(radius*2);
d.setWidth(radius*2);
d.setModus(3);
}
public void paint(Graphics g)
{
super.paint(g);
if(d.getModus()==1)
{
g.drawLine(d.getX1(),d.getY1(),d.getX2(),d.getY2());
}
else
{
if(d.getModus()==2)
{
g.drawRect(d.getX1(),d.getY1(),d.getX2(),d.getY2());
}
else
{
g.drawOval(d.getX1(), d.getY1(), d.getWidth(), d.getHeight());
}
}
}
}
public class Daten
{
private int x1;
private int y1;
private int x2;
private int y2;
private int height;
private int width;
private int modus;
public void setX1(int x1)
{
this.x1 = x1;
}
public int getX1()
{
return x1;
}
public void setY1(int y1)
{
this.y1 = y1;
}
public int getY1()
{
return y1;
}
public void setX2(int x2)
{
this.x2 = x2;
}
public int getX2()
{
return x2;
}
public void setY2(int y2)
{
this.y2 = y2;
}
public int getY2()
{
return y2;
}
public void setHeight(int height)
{
this.height = height;
}
public int getHeight()
{
return height;
}
public void setWidth(int width)
{
this.width = width;
}
public int getWidth()
{
return width;
}
public void setModus(int modus)
{
this.modus = modus;
}
public int getModus()
{
return modus;
}
}
[..]
Allerdings überschreiben eigene Unterklassen von Swing-Komponenten im Regelfall nicht paint(), sondern paintComponent(). Das liegt daran, dass Swing in paint() zum Beispiel noch Rahmen zeichnet und sich um eine Pufferung des Bildschirminhalts zur Optimierung kümmert. So ruft paint() die drei Methoden paintComponent(), paintBorder() und paintChildren() auf, und [..]
zusätzlich
import javax.swing.BorderFactory;
public class Test
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Schrubber´s TestTool");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(new Form(10, 10, 50, 50));// Strich
frame.add(new Form(10, 10, 50, 50));// Strich
frame.add(new Form(10, 10, 50, 50));// Strich
frame.add(new Form(110, 110, 90, 90, 1));// Rechteck
frame.add(new Form(50, 50, 40));// Kreis
frame.setVisible(true);
}
}
class Arbeitsblatt
extends JComponent
{
}
class Form
extends Arbeitsblatt
{
Daten d = new Daten();
public Form(int x1, int y1, int x2, int y2)// Linie
{
setPreferredSize(new Dimension(100, 100));
setBorder(BorderFactory.createLineBorder(Color.BLUE));
d.setX1(x1);
d.setY1(y1);
d.setX2(x2);
d.setY2(y2);
d.setModus(1);
}
public Form(int x1, int y1, int x2, int y2, int modus)// Rechteck
{
setPreferredSize(new Dimension(100, 100));
setBorder(BorderFactory.createLineBorder(Color.BLUE));
d.setX1(x1);
d.setY1(y1);
d.setX2(x2);
d.setY2(y2);
d.setModus(2);
}
public Form(int x1, int y1, int radius)// Kreis
{
setPreferredSize(new Dimension(100, 100));
setBorder(BorderFactory.createLineBorder(Color.BLUE));
d.setX1(x1);
d.setY1(y1);
d.setHeight(radius * 2);
d.setWidth(radius * 2);
d.setModus(3);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (d.getModus() == 1)
{
g.drawLine(d.getX1(), d.getY1(), d.getX2(), d.getY2());
}
else
{
if (d.getModus() == 2)
{
g.drawRect(d.getX1(), d.getY1(), d.getX2(), d.getY2());
}
else
{
g.drawOval(d.getX1(), d.getY1(), d.getWidth(), d.getHeight());
}
}
}
}
JFrame nutzt standardmäßig das BorderLayout. Ein einfachesJava:frame.add(new Form(10,10,50,50));//Strich //frame.add(new Form(110,110,90,90,1));//Rechteck frame.add(new Form(50,50,40));//Kreis
add(Component)
import java.awt.*;
import javax.swing.*;
public class Arbeitsblatt extends JComponent
{
public static void main(String[] args)
{
/*
JFrame frame = new JFrame("Schrubber´s TestTool");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new LayeredPane();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.setSize(400,400);
newContentPane.setSize(400,400);
frame.add(new Form(0,0,200,200));
frame.add(new Form(0,0,200,200,1));
frame.add(new Form(0,0,50));
frame.pack();
frame.setVisible(true);
*/
erstelleGUI();
}
public static void erstelleGUI()
{
JFrame frame = new JFrame("Schrubber´s TestTool");
JComponent newContentPane = new LayeredPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(newContentPane);
frame.setSize(400,400);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
newContentPane.setOpaque(true);
newContentPane.setSize(400,400);
JPanel draw1 = new JPanel();
draw1.setLayout(new GridLayout());
// draw1.setLayout(new BoxLayout(draw1, BoxLayout.LINE_AXIS));
draw1.add(new Form(50,50,100,100));
JPanel draw2 = new JPanel();
draw2.setLayout(new GridLayout());
// draw2.setLayout(new BoxLayout(draw1, BoxLayout.LINE_AXIS));
draw2.add(new Form(50,50,100,100));
newContentPane.add(draw1);
newContentPane.add(draw2);
frame.pack();
frame.setVisible(true);
}
}
import java.awt.Color;
public class Daten
{
private int x1;
private int y1;
private int x2;
private int y2;
private int height;
private int width;
private int modus;
private Color color;
private boolean befehl = false;
public void setX1(int x1)
{
this.x1 = x1;
}
public int getX1()
{
return x1;
}
public void setY1(int y1)
{
this.y1 = y1;
}
public int getY1()
{
return y1;
}
public void setX2(int x2)
{
this.x2 = x2;
}
public int getX2()
{
return x2;
}
public void setY2(int y2)
{
this.y2 = y2;
}
public int getY2()
{
return y2;
}
public void setHeight(int height)
{
this.height = height;
}
public int getHeight()
{
return height;
}
public void setWidth(int width)
{
this.width = width;
}
public int getWidth()
{
return width;
}
public void setModus(int modus)
{
this.modus = modus;
}
public int getModus()
{
return modus;
}
public boolean isBefehl()
{
return befehl;
}
public void setBefehl(boolean befehl)
{
this.befehl = befehl;
}
}
import java.awt.Color;
import java.awt.Graphics;
public class Form extends Arbeitsblatt
{
Daten d = new Daten();
Protokoll p = new Protokoll();
public Form(int x1, int y1, int x2, int y2)//Linie
{
d.setX1(x1);
d.setY1(y1);
d.setX2(x2);
d.setY2(y2);
d.setModus(1);
}
public Form(int x1, int y1, int x2, int y2, int modus)//Rechteck
{
d.setX1(x1);
d.setY1(y1);
d.setX2(x2);
d.setY2(y2);
d.setModus(2);
}
public Form(int x1, int y1, int radius)//Kreis
{
d.setX1(x1);
d.setY1(y1);
d.setHeight(radius*2);
d.setWidth(radius*2);
d.setModus(3);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(d.getModus()==1)
{
if(d.isBefehl())
g.setColor(null);
g.drawLine(d.getX1(),d.getY1(),d.getX2(),d.getY2());
}
else
{
if(d.getModus()==2)
{
if(d.isBefehl())
g.setColor(null);
g.drawRect(d.getX1(),d.getY1(),d.getX2(),d.getY2());
}
else
{
if(d.isBefehl())
g.setColor(null);
g.drawOval(d.getX1(), d.getY1(), d.getWidth(), d.getHeight());
}
}
}
}
import java.awt.*;
import javax.swing.*;
public class LayeredPane extends JPanel
{
public LayeredPane()
{
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
}
}