使用以下代码,我可以画一条虚线:
public void foo(Graphics2D g2d, Shape shape)
{
Stroke stroke = BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10, new float[]{10}, 0);
g2d.setStroke(stroke);
g2d.draw(shape);
}
创建一个形状后,我希望能够缩放该形状(最多20 000次)。我遇到的问题是,当我在形状上放大太多时,应用程序开始滞后,如果我继续缩放,最终会崩溃。
简单来说,我没有问题。
因此,我的问题如下:有没有办法用虚线绘制非常大的形状(例如:一个200 000像素乘300 000像素的矩形)?
谢谢。
编辑:
这是一个简短的例子,我可以重现我的问题:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Scale
{
private static int _scale = 1;
public static int getScale()
{
return _scale;
}
public static void setScale(int scale)
{
_scale = scale;
}
}
class Surface extends JPanel implements ActionListener
{
private static Surface _surface;
boolean isBlue = false;
private Surface()
{
}
public static Surface getInstance()
{
if (_surface == null)
{
_surface = new Surface();
}
return _surface;
}
private void doDrawing(Graphics g)
{
Shape rectangle = new Rectangle(0, 0, 600 * Scale.getScale(), 400 * Scale.getScale());
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.blue);
Stroke stroke = new BasicStroke(10, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[]{10.0f}, 0);
g2d.setStroke(stroke);
// Adding a clip don't seem to do the trick :(
g2d.clip(new Rectangle(0, 0, 100, 100));
long startTime = System.nanoTime();
g2d.draw(rectangle);
long elapseTime = System.nanoTime() - startTime;
// Printing the time it took each time I render my shape. As the size increase, the time increase. If the shape decrease, the time decrease as well.
System.out.println(elapseTime);
g2d.dispose();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
doDrawing(g);
}
@Override
public void actionPerformed(ActionEvent e)
{
repaint();
}
}
public class MainFrame extends JFrame implements KeyListener
{
public MainFrame()
{
initUI();
setFocusable(true);
addKeyListener(this);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
MainFrame ex = new MainFrame();
ex.setVisible(true);
}
});
}
private void initUI()
{
final Surface surface = Surface.getInstance();
add(surface);
setTitle("My boggus apps");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void keyTyped(final KeyEvent e)
{
}
@Override
public void keyPressed(final KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP)
{
Scale.setScale(Scale.getScale() * 2);
if (Scale.getScale() > 200000)
{
Scale.setScale(200000);
}
Surface.getInstance().repaint();
}
else if (key == KeyEvent.VK_DOWN)
{
Scale.setScale(Scale.getScale() / 2);
if (Scale.getScale() < 1)
{
Scale.setScale(1);
}
Surface.getInstance().repaint();
}
else
{
System.out.println(key);
}
}
@Override
public void keyReleased(final KeyEvent e)
{
}
}
答案 0 :(得分:2)
对于带有虚线笔划的长行,渲染性能不佳是AWT Graphics2D和JavaFX 2中一个已知且尚未解决的问题:
Java 8u152和9.0.1中仍未解决。但是,9.0.1中的性能略好一些(在我的一个测试9.0.1和8u152中,大约1.5s与4.0s)。
背景: 长虚线分解为它们包含的每个短划线,即使这些短划线都没有触及剪切区域。
已知但主要是不满意的解决方法:
答案 1 :(得分:0)
以下是根据路径的性质使用区域相交或线/矩形相交的解决方案。这不是理想的解决方案,因为它无法正确地将虚线放置在比剪切区域大得多的路径上。但是,它将这些情况降到最低。
private void drawLimited(Shape primitive, Graphics2D canvas) {
if (!(canvas.getStroke() instanceof BasicStroke) || ((BasicStroke) canvas.getStroke()).getDashArray() == null) {
// draw normally
canvas.draw(primitive);
return;
}
Rectangle r = canvas.getClipBounds();
// use a large padding to exclude all but the worst performance cases
int pad = Ints.max(canvas.getStroke() instanceof BasicStroke ? (int) Math.ceil(((BasicStroke) canvas.getStroke()).getLineWidth()) : 5,
r.width * 50, r.height * 50);
Rectangle paddedClip = new Rectangle(r.x - pad, r.y - pad,
r.width + 2 * pad, r.height + 2 * pad);
Shape toDraw = intersectPath(paddedClip, primitive);
if (toDraw != null) {
canvas.draw(toDraw);
}
}
private static @Nullable Shape intersectPath(Rectangle2D rectangle, Shape path) {
Rectangle2D r2 = path.getBounds2D();
if (r2.getWidth() == 0 && r2.getHeight() == 0) {
return null;
} else if (rectangle.contains(r2)) {
return path;
}
if (r2.getWidth() == 0 || r2.getHeight() == 0) {
// we have a flat shape, so area intersection doesn't work -- this is not precisely correct for multi-part paths, but close enough?
path = new Line2D.Double(r2.getMinX(), r2.getMinY(), r2.getMaxX(), r2.getMaxY());
}
if (path instanceof Line2D.Double) {
Line2D line = (Line2D) path;
return line.intersects(rectangle) ? intersect(line, rectangle) : null;
} else {
Area a = new Area(rectangle);
a.intersect(new Area(path));
return a;
}
}
private static @Nullable Line2D.Double intersect(Line2D.Double l, Rectangle2D r) {
if (r.contains(l.getP1()) && r.contains(l.getP2())) {
return l;
}
// parameterize line as x=x1+t*(x2-x1), y=y1+t*(y2-y1), so line is between 0 and 1
// then compute t values for lines bounding rectangles, and intersect the three intervals
// [0,1], [tx1,tx2], and [ty1,ty2]
double tx1 = l.x1 == l.x2 ? (between(l.x1, r.getMinX(), r.getMaxX()) ? 0 : -1) : (r.getMinX() - l.x1) / (l.x2 - l.x1);
double tx2 = l.x1 == l.x2 ? (between(l.x1, r.getMinX(), r.getMaxX()) ? 1 : -1) : (r.getMaxX() - l.x1) / (l.x2 - l.x1);
double ty1 = l.y1 == l.y2 ? (between(l.x1, r.getMinY(), r.getMaxY()) ? 0 : -1) : (r.getMinY() - l.y1) / (l.y2 - l.y1);
double ty2 = l.y1 == l.y2 ? (between(l.x1, r.getMinY(), r.getMaxY()) ? 1 : -1) : (r.getMaxY() - l.y1) / (l.y2 - l.y1);
double t0 = max(0, min(tx1, tx2), min(ty1, ty2));
double t1 = min(1, max(tx1, tx2), max(ty1, ty2));
return t0 > t1 ? null : new Line2D.Double(l.x1 + t0 * (l.x2 - l.x1), l.y1 + t0 * (l.y2 - l.y1),
l.x1 + t1 * (l.x2 - l.x1), l.y1 + t1 * (l.y2 - l.y1));
}
private static boolean between(double x, double t0, double t1) {
return x >= t0 ? x <= t1 : x >= t1;
}