Ich hab mal eben als Beispiel den AntialiasingTest von oben entsprechend umgebaut: ```/* (@)AntiAliasingTest.java */
/* Copyright 2009 Sebastian Haufe
-
Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
-
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
/**
- TODO: Javadoc me!
-
-
@version $Revision$ as of $Date$
-
@author Sebastian Haufe
-
@since Playground-3.8
*/
public class AntiAliasingTest extends JPanel {
private final Arc2D.Double circle =
new Arc2D.Double(0, 0, 0, 0, 0, 360, Arc2D.OPEN);
private final Random rnd = new Random();
private final Color[] colors =
{ Color.WHITE, Color.LIGHT_GRAY, Color.GRAY, Color.DARK_GRAY,
Color.BLACK, Color.RED, Color.PINK, Color.ORANGE, Color.YELLOW,
Color.GREEN, Color.MAGENTA, Color.CYAN, Color.BLUE, };
private int stepSize = 20;
private int arcRadius = 50;
private BufferedImage offScreenImage = null;
private boolean offScreenImageValid = false;
// -------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------
/** Creates a new AntiAliasingTest
. */
public AntiAliasingTest() {
super(false);
final int size = 4000;
setPreferredSize(new Dimension(size, size));
}
// -------------------------------------------------------------------------
// Bean getters and setters
// -------------------------------------------------------------------------
/**
- Returns the stepSize.
-
-
@return the stepSize
*/
public int getStepSize() {
return stepSize;
}
/**
- Sets the stepSize.
-
-
@param stepSize the stepSize to set
*/
public void setStepSize(int stepSize) {
final int old = this.stepSize;
this.stepSize = stepSize;
firePropertyChange(“stepSize”, old, stepSize); //$NON-NLS-1$
}
/**
- Returns the arcRadius.
-
-
@return the arcRadius
*/
public int getArcRadius() {
return arcRadius;
}
/**
- Sets the arcRadius.
-
-
@param arcRadius the arcRadius to set
*/
public void setArcRadius(int arcRadius) {
final int old = this.arcRadius;
this.arcRadius = arcRadius;
firePropertyChange(“arcRadius”, old, arcRadius); //$NON-NLS-1$
}
// -------------------------------------------------------------------------
// Off screen image
// -------------------------------------------------------------------------
@Override
public void invalidate() {
offScreenImageValid = false;
super.invalidate();
}
@Override
public void validate() {
super.validate();
updateOffScreenImageSize();
}
private void updateOffScreenImageSize() {
final int w = getWidth();
final int h = getHeight();
BufferedImage img = offScreenImage;
if (img == null || img.getWidth() != w || img.getHeight() != h) {
offScreenImage =
img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
offScreenImageValid = false;
}
}
private void updateOffScreenImageAsNecessary() {
if (!offScreenImageValid) {
updateOffScreenImageSize();
paintComponent2D(offScreenImage.createGraphics(), true);
offScreenImageValid = true;
}
}
// -------------------------------------------------------------------------
// Painting
// -------------------------------------------------------------------------
@Override
protected void paintComponent(Graphics g) {
updateOffScreenImageAsNecessary();
g.drawImage(offScreenImage, 0, 0, null);
}
protected void paintComponent2D(Graphics2D g2d, boolean disposeGraphics) {
try {
super.paintComponent(g2d);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
paintCircles(g2d);
} finally {
if (disposeGraphics) {
g2d.dispose();
}
}
}
private void paintCircles(Graphics2D g2d) {
final int r = arcRadius;
final int dia = r * 2;
final int w = getWidth();
final int h = getHeight();
circle.width = dia;
circle.height = dia;
for (int x = r; x < w - dia - r; x += stepSize) {
for (int y = r; y < h - dia - r; y += stepSize) {
paintCircle(g2d, x, y);
}
}
}
private void paintCircle(Graphics2D g2d, double x, double y) {
g2d.setColor(colors[rnd.nextInt(colors.length)]);
circle.x = x;
circle.y = y;
g2d.draw(circle);
}
// -------------------------------------------------------------------------
// Program Entry Point
// -------------------------------------------------------------------------
/**
private static void createAndShowGui() {
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
final AntiAliasingTest view = new AntiAliasingTest();
view.setPreferredSize(new Dimension(400, 400));
final JScrollPane sp =
new JScrollPane(view, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
final JViewport vp = sp.getViewport();
vp.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
vp.setPreferredSize(new Dimension(400, 400));
contentPane.add(sp);
final JComboBox cb =
new JComboBox(new String[] { "Size: 400x400", "Size: 800x800",
"Size: 1600x1600", "Size: 3200x3200", "Size: 6400x6400" });
cb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (cb.getSelectedIndex()) {
case 0:
view.setPreferredSize(new Dimension(400, 400));
break;
case 1:
view.setPreferredSize(new Dimension(800, 800));
break;
case 2:
view.setPreferredSize(new Dimension(1600, 1600));
break;
case 3:
view.setPreferredSize(new Dimension(3200, 3200));
break;
case 4:
view.setPreferredSize(new Dimension(6400, 6400));
break;
}
view.revalidate();
view.repaint();
}
});
contentPane.add(cb, BorderLayout.NORTH);
final JFrame f = new JFrame("Test Frame: AntiAliasingTest");
f.setContentPane(contentPane);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}```
Ebenius