Hallo zusammen,
ich versuche ein JPanel zu rotieren. Leider wird nur ein Teil des JPanels angezeigt, der Rest wird einfach abgeschnitten und ich vermute ein Problem mit dem Clipping.Mit
Im folgenden Code erledigt "paintComponent()" das Rotieren, mit "Rectangle r = g2.getClipBounds();" versuche ich ein größeres Clipping-Rechteck zu erzeugen, welches das rotierte JPanel enthält.
Hat jemand eine Idee?
ich versuche ein JPanel zu rotieren. Leider wird nur ein Teil des JPanels angezeigt, der Rest wird einfach abgeschnitten und ich vermute ein Problem mit dem Clipping.Mit
Im folgenden Code erledigt "paintComponent()" das Rotieren, mit "Rectangle r = g2.getClipBounds();" versuche ich ein größeres Clipping-Rechteck zu erzeugen, welches das rotierte JPanel enthält.
Java:
package tutorialswingmouse4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
class MyShape extends JPanel {
private final JPanel m_owner;
private double d = 4.5;
public MyShape( JPanel owner ) {
super();
m_owner = owner;
setLayout( null );
Timer s = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doTheUpdate();
}
});
s.start();
}
public void doTheUpdate() {
updateUI();
}
@Override
protected void paintComponent( Graphics g ) {
super.paintComponent( g );
int w = getWidth();
int h = getHeight();
//g.clearRect( 0, 0, getWidth(), getHeight() );
Graphics2D g2 = ( Graphics2D ) g;
Rectangle r = g2.getClipBounds();
r.x = r.x - 10;
r.y = r.y - 10;
r.width = r.width + 20;
r.height = r.height + 20;
g2.rotate(Math.toRadians( d * 10.0 ), w/2, h/2 );
System.out.println( r );
g.setColor( Color.green );
g.drawRect( 0, 0, w-1, h-1 );
g2.setClip( r );
d = d + 0.5;
}
}
public class TutorialSwingMouse4 extends JFrame {
private final JPanel panel;
private final MyShape shape;
public TutorialSwingMouse4() {
setDefaultCloseOperation( EXIT_ON_CLOSE );
setSize( 640, 480 );
panel = new JPanel();
panel.setLayout( null );
add( panel );
shape = new MyShape( panel );
shape.setSize( 150, 150 );
shape.setLocation( 100, 100 );
panel.add( shape );
setVisible( true );
}
public static void main(String[] args) {
new TutorialSwingMouse4();
}
}
Hat jemand eine Idee?