gpt4 book ai didi

java - JScrollPane+ Canvas : rendering issue

转载 作者:太空宇宙 更新时间:2023-11-04 08:19:06 25 4
gpt4 key购买 nike

我正在开发 MS Paint 的 Java 版本。您可以看到到目前为止的样子 here 。 (图像太高且太多,无法嵌入问题中)

它使用 JScrollPane 来移动 Canvas 的子类。如果您不调整窗口大小,它就可以正常运行。如果你制作窗口smaller ,乍一看似乎工作原理是一样的。

但是,如果您scroll around ,很明显应用程序正在渲染相同的“视口(viewport)”,只是移动了。如果你keep scrolling ,它与其他所有内容重叠变得更加明显。

所以基本上,它渲染了错误的视口(viewport)。调整窗口大小会将视口(viewport)更新为正确的。如果您尝试在灰色区域上绘制,它会绘制得很好,只是您看不到它。

每当滚动条移动时,我都尝试在 Canvas 上执行repaint()。它没有改变任何东西。

我应该怎么做才能解决这个问题?

这是框架的代码:(参数 img 是它将绘制的图像。您也必须自己执行 setVisible(true) )

import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;


public class JPaint extends JFrame {
JPaintPanel panel;
public JPaint(BufferedImage img) {
super("Edit your image");
panel = new JPaintPanel(img);
setContentPane(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Trial and error got me these numbers.
//I have no idea how find the proper size...
setSize(img.getWidth()+20-1, img.getHeight()+50+50+2);
}
//go to the panel you hold, ask it to retrieve it from the canvas
public BufferedImage grabImage() {
return panel.grabImage();
}
}


class JPaintPanel extends JPanel {
JTools toolbar;
JCanvas canvas;
JScrollPane scrollPane;
public JPaintPanel(BufferedImage img) {
super(new BorderLayout());
toolbar = new JTools();
canvas = new JCanvas(img);
JScrollPane scrollPane = new JScrollPane(canvas);
scrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
canvas.repaint();
}

});
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
canvas.repaint();
}

});
setPreferredSize(new Dimension(img.getWidth(),img.getHeight()+50));
add(toolbar, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
}
public BufferedImage grabImage() {
return canvas.getImage();
}
}
class JTools extends JPanel {
JSlider scale;

public JTools() {
scale= new JSlider(JSlider.HORIZONTAL,
0, 400, 100);
scale.setMajorTickSpacing(100);
scale.setPaintTicks(true);
scale.setPaintLabels(true);
scale.setPreferredSize(new Dimension(300,50));
add(scale);
}

}
class JCanvas extends Canvas {

BufferedImage im;
Graphics2D g2d;
Point old = new Point();
Point now = new Point();

public JCanvas(BufferedImage imIn) {
im = imIn;
g2d = im.createGraphics();


setPreferredSize(new Dimension(im.getWidth(), im.getHeight()));


setColor(Color.WHITE);
setWidth(4);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//g2d.drawRect(5, 5, 40, 20);

addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
//System.out.println(e.getPoint());
//g2d.fillRect(e.getX()-5, e.getY()-5, 10, 10);
old=e.getPoint();
now=e.getPoint();
g2d.drawLine(e.getX(), e.getY(), e.getX(), e.getY());
repaint();
}
});
addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
//System.out.println(e.getPoint());
old=e.getPoint();
now=e.getPoint();
g2d.drawLine(e.getX(), e.getY(), e.getX(), e.getY());
repaint();
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
//System.out.println(e.getPoint());
//g2d.fillRect(e.getX()-5, e.getY()-5, 10, 10);
old=now;
now=e.getPoint();
g2d.drawLine((int)old.getX(), (int)old.getY(), (int)now.getX(), (int)now.getY());
repaint();
}
});
}
public void paint(Graphics g) {
//super.paint(g);
update(g);
}
public void update(Graphics g) {
//super.update(g);
g.drawImage(im, 0, 0, null);
getToolkit().sync();
}
public void setWidth(float w) {
g2d.setStroke(new BasicStroke(w, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
}
public void setColor(Color c) {
g2d.setColor(c);
}
public BufferedImage getImage() {
return im;
}
}

最佳答案

class JCanvas extends Canvas { 

没有正当理由,请勿将 Swing 与 AWT 组件混合使用。改为扩展 JComponent

关于java - JScrollPane+ Canvas : rendering issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9899502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com