Hallo, vor Urzeiten hab ich mir mal einen Horrorcode geschrieben, der mir im Nachhinein sogar vernünftig vorkommt.
trotzdem irgendwie abschreckend. Wenn ihr einen beseren weg wisst, bitte sagsts mir
Ich wollte mir eine Basisklasse Painter schreiben, bei der ich einfach nur n paar set-Methoden aufrufen muss und dann schon alles über paintComponent läuft...
[highlight="Java"]
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.ImageObserver;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Painter extends JComponent {
public final static int DRAW_IMAGE = 0;
public final static int DRAW_LINE = 99;
public final static int DRAW_ARC = 1, FILL_ARC = -1,
DRAW_OVAL = 2, FILL_OVAL = -2,
DRAW_RECT = 3, FILL_RECT = -3,
DRAW_POLYGON = 4, FILL_POLYGON = -4,
DRAW_ROUND_RECT = 5, FILL_ROUND_RECT = -5;
public static final int DRAW_POLYLINE = -100;
public static final int CLEAR_RECT = 123;
public static final int COPY_AREA = -123;
private int x;
private int y;
private int width;
private int height;
private int dx;
private int dy;
private int startAngle;
private int arcAngle;
private int dx2;
private int dy2;
private int sx1;
private int sy1;
private int sx2;
private int sy2;
private int x2;
private int y2;
private int nPoints;
private int arcWidth;
private int arcHeight;
private int[] xPoints;
private int[] yPoints;
private Image img;
private ImageObserver observer;
private Polygon p;
private Font font;
private Color color = Color.BLACK;
private int modus;
public Painter(int modus){
this.modus = modus;
}
public void setFont(Font font)
{
this.font = font;
}
public Font getFont()
{
return this.font;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(font);
g.setColor(color);
switch ( modus )
{
case CLEAR_RECT:{
g.clearRect(x, y, width, height);
break;
}
case COPY_AREA:{
g.copyArea(x, y, width, height, dx, dy);
break;
}
case DRAW_ARC:{
g.drawArc(x, y, width, height, startAngle, arcAngle);
break;
}
case DRAW_IMAGE:{
g.drawImage(img, dx, dy, dx2, dy2, sx1, sy1, sx2, sy2, observer);
break;
}
case DRAW_LINE:{
g.drawLine(x, y, x2, y2);
break;
}
case DRAW_OVAL:{
g.drawOval(dx, y, width, height);
break;
}
case DRAW_POLYGON:{
g.drawPolygon(p);
break;
}
case DRAW_POLYLINE:{
g.drawPolyline(xPoints, yPoints, nPoints);
break;
}
case DRAW_RECT:{
g.drawRect(x, y, width, height);
break;
}
case DRAW_ROUND_RECT:{
g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
break;
}
case FILL_ARC:{
g.fillArc(x, y, width, height, startAngle, arcAngle);
break;
}
case FILL_OVAL:{
g.fillOval(x, y, width, height);
break;
}
case FILL_POLYGON:{
g.fillPolygon(p);
break;
}
case FILL_RECT:{
g.fillRect(x, y, width, height);
break;
}
case FILL_ROUND_RECT:{
g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
break;
}
}
}
//*****************************************************
public static void main(String[]args)
{
final Painter p = new Painter(Painter.DRAW_LINE);
JFrame f= new JFrame();
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
p.setColor(Color.GREEN);
p.setX(50);
p.setY(50);
p.setX2(200);
p.setY2(200);
p.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
p.repaint();
}
});
}
//*****************************************************
public void setY(int y) {
this.y = y;
}
public int getY(){
return this.y;
}
public void setColor(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setModus(int modus) {
this.modus = modus;
}
public int getModus() {
return modus;
}
public void setWidth(int width) {
this.width = width;
}
public int getWidth() {
return width;
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return this.height;
}
public void setDx(int dx) {
this.dx = dx;
}
public int getDx() {
return this.dx;
}
public void setDy(int dy) {
this.dy = dy;
}
public int getDy() {
return this.dy;
}
public void setStartAngle(int startAngle) {
this.startAngle = startAngle;
}
public int getStartAngle() {
return this.startAngle;
}
public void setArcAngle(int arcAngle) {
this.arcAngle = arcAngle;
}
public int getArcAngle() {
return this.arcAngle;
}
public void setDx2(int dx2) {
this.dx2 = dx2;
}
public int getDx2() {
return this.dx2;
}
public void setDy2(int dy2) {
this.dy2 = dy2;
}
public int getDy2() {
return this.dy2;
}
public void setSx1(int sx1) {
this.sx1 = sx1;
}
public int getSx1() {
return this.sx1;
}
public void setSy1(int sy1) {
this.sy1 = sy1;
}
public int getSy1() {
return this.sy1;
}
public void setSx2(int sx2) {
this.sx2 = sx2;
}
public int getSx2() {
return this.sx2;
}
public void setSy2(int sy2) {
this.sy2 = sy2;
}
public int getSy2() {
return sy2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getX2() {
return this.x2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public int getY2() {
return this.y2;
}
}
[/highlight]
Oder liegt es am layout-manager/Wurde das Panel nicht richtig geaddet?
Danke im Voraus
trotzdem irgendwie abschreckend. Wenn ihr einen beseren weg wisst, bitte sagsts mir
Ich wollte mir eine Basisklasse Painter schreiben, bei der ich einfach nur n paar set-Methoden aufrufen muss und dann schon alles über paintComponent läuft...
[highlight="Java"]
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.ImageObserver;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Painter extends JComponent {
public final static int DRAW_IMAGE = 0;
public final static int DRAW_LINE = 99;
public final static int DRAW_ARC = 1, FILL_ARC = -1,
DRAW_OVAL = 2, FILL_OVAL = -2,
DRAW_RECT = 3, FILL_RECT = -3,
DRAW_POLYGON = 4, FILL_POLYGON = -4,
DRAW_ROUND_RECT = 5, FILL_ROUND_RECT = -5;
public static final int DRAW_POLYLINE = -100;
public static final int CLEAR_RECT = 123;
public static final int COPY_AREA = -123;
private int x;
private int y;
private int width;
private int height;
private int dx;
private int dy;
private int startAngle;
private int arcAngle;
private int dx2;
private int dy2;
private int sx1;
private int sy1;
private int sx2;
private int sy2;
private int x2;
private int y2;
private int nPoints;
private int arcWidth;
private int arcHeight;
private int[] xPoints;
private int[] yPoints;
private Image img;
private ImageObserver observer;
private Polygon p;
private Font font;
private Color color = Color.BLACK;
private int modus;
public Painter(int modus){
this.modus = modus;
}
public void setFont(Font font)
{
this.font = font;
}
public Font getFont()
{
return this.font;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(font);
g.setColor(color);
switch ( modus )
{
case CLEAR_RECT:{
g.clearRect(x, y, width, height);
break;
}
case COPY_AREA:{
g.copyArea(x, y, width, height, dx, dy);
break;
}
case DRAW_ARC:{
g.drawArc(x, y, width, height, startAngle, arcAngle);
break;
}
case DRAW_IMAGE:{
g.drawImage(img, dx, dy, dx2, dy2, sx1, sy1, sx2, sy2, observer);
break;
}
case DRAW_LINE:{
g.drawLine(x, y, x2, y2);
break;
}
case DRAW_OVAL:{
g.drawOval(dx, y, width, height);
break;
}
case DRAW_POLYGON:{
g.drawPolygon(p);
break;
}
case DRAW_POLYLINE:{
g.drawPolyline(xPoints, yPoints, nPoints);
break;
}
case DRAW_RECT:{
g.drawRect(x, y, width, height);
break;
}
case DRAW_ROUND_RECT:{
g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
break;
}
case FILL_ARC:{
g.fillArc(x, y, width, height, startAngle, arcAngle);
break;
}
case FILL_OVAL:{
g.fillOval(x, y, width, height);
break;
}
case FILL_POLYGON:{
g.fillPolygon(p);
break;
}
case FILL_RECT:{
g.fillRect(x, y, width, height);
break;
}
case FILL_ROUND_RECT:{
g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
break;
}
}
}
//*****************************************************
public static void main(String[]args)
{
final Painter p = new Painter(Painter.DRAW_LINE);
JFrame f= new JFrame();
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
p.setColor(Color.GREEN);
p.setX(50);
p.setY(50);
p.setX2(200);
p.setY2(200);
p.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
p.repaint();
}
});
}
//*****************************************************
public void setY(int y) {
this.y = y;
}
public int getY(){
return this.y;
}
public void setColor(Color color) {
this.color = color;
}
public Color getColor() {
return color;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setModus(int modus) {
this.modus = modus;
}
public int getModus() {
return modus;
}
public void setWidth(int width) {
this.width = width;
}
public int getWidth() {
return width;
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return this.height;
}
public void setDx(int dx) {
this.dx = dx;
}
public int getDx() {
return this.dx;
}
public void setDy(int dy) {
this.dy = dy;
}
public int getDy() {
return this.dy;
}
public void setStartAngle(int startAngle) {
this.startAngle = startAngle;
}
public int getStartAngle() {
return this.startAngle;
}
public void setArcAngle(int arcAngle) {
this.arcAngle = arcAngle;
}
public int getArcAngle() {
return this.arcAngle;
}
public void setDx2(int dx2) {
this.dx2 = dx2;
}
public int getDx2() {
return this.dx2;
}
public void setDy2(int dy2) {
this.dy2 = dy2;
}
public int getDy2() {
return this.dy2;
}
public void setSx1(int sx1) {
this.sx1 = sx1;
}
public int getSx1() {
return this.sx1;
}
public void setSy1(int sy1) {
this.sy1 = sy1;
}
public int getSy1() {
return this.sy1;
}
public void setSx2(int sx2) {
this.sx2 = sx2;
}
public int getSx2() {
return this.sx2;
}
public void setSy2(int sy2) {
this.sy2 = sy2;
}
public int getSy2() {
return sy2;
}
public void setX2(int x2) {
this.x2 = x2;
}
public int getX2() {
return this.x2;
}
public void setY2(int y2) {
this.y2 = y2;
}
public int getY2() {
return this.y2;
}
}
[/highlight]
Oder liegt es am layout-manager/Wurde das Panel nicht richtig geaddet?
Danke im Voraus