我正在使用java Paint Component和一个空玻璃Image,如下所示。我的想法是根据用户给出的百分比用油漆颜色绘制这个图像。例如,如果用户将50%的输入放入我的jframe程序,它将输出下面的玻璃图像,半满的颜色。我是图形java的新手,我们怎么能这样做。
我正在考虑使用fillRect(x,y,w,h)作为绘图图形,但是我如何进行用户给出的计算,在paint fillRect()上使用它并相应地显示玻璃填充图形?
我可以对fillRect()进行什么计算?
转向此
答案 0 :(得分:2)
x绘制的矩形的宽度将是常量。这些常数也是如此。 (我只是试验和错误' ed这些值
private static final int GLASS_WIDTH = 75;
private static final int GLASS_X = 75;
所以你只需要计算矩形的y和height值。为此,我使用了一些辅助常量
private static final int HORIZON = 210;
private static final int GLASS_Y_TOP = 10;
private static final int GLASS_HEIGHT = HORIZON - GLASS_Y_TOP;
HORIZON
是矩形的最底部,GLASS_Y_TOP
是最高点处矩形的顶点,GLASS_HEIGHT
是矩形的100%。
了解这些事情,我能够计算出100%的矩形
private double glassY = GLASS_Y_TOP;
private double rectHeight = GLASS_HEIGHT;
...
rect = new Rectangle2D.Double(GLASS_X, glassY, GLASS_WIDTH, rectHeight);
两个新变量glassY
和rectHeight
是您在用户输入值更改时需要更改的值。您可以在方法调用中看到一个实现,以根据JSpinner
private void calculateGlassValues(int value) {
rectHeight = GLASS_HEIGHT * (value / 100.0);
glassY = HORIZON - rectHeight;
rect.setRect(GLASS_X, glassY, GLASS_WIDTH, rectHeight);
System.out.print(rect.getBounds());
}
最终产品
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class PaintPercentImage extends JPanel {
private static final int GLASS_WIDTH = 75;
private static final int GLASS_X = 75;
private static final int HORIZON = 210;
private static final int GLASS_Y_TOP = 10;
private static final int GLASS_HEIGHT = HORIZON - GLASS_Y_TOP;
private double glassY = GLASS_Y_TOP;
private double rectHeight = GLASS_HEIGHT;
private BufferedImage image;
private Rectangle2D rect;
private int imageWidth;
private int imageHeight;
private static JSpinner spinner;
public PaintPercentImage() {
try {
image = ImageIO.read(new URL("http://i.stack.imgur.com/G4IEu.jpg"));
imageWidth = image.getWidth();
imageHeight = image.getHeight();
System.out.println(imageHeight);
} catch (IOException e) {
e.printStackTrace();
}
rect = new Rectangle2D.Double(GLASS_X, glassY, GLASS_WIDTH, rectHeight);
spinner = createSpinner();
}
private JSpinner createSpinner() {
final JSpinner spinner = new JSpinner();
SpinnerNumberModel model = new SpinnerNumberModel(100, 0, 100, 1);
spinner.setModel(model);
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int value = (Integer) spinner.getModel().getValue();
calculateGlassValues(value);
System.out.println(value);
repaint();
}
});
return spinner;
}
private void calculateGlassValues(int value) {
rectHeight = GLASS_HEIGHT * (value / 100.0);
glassY = HORIZON - rectHeight;
rect.setRect(GLASS_X, glassY, GLASS_WIDTH, rectHeight);
System.out.print(rect.getBounds());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(image, 0, 0, imageWidth, imageHeight, this);
g2.setPaint(Color.YELLOW);
g2.fill(rect);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(imageWidth, imageHeight);
}
public static void showGui() {
JFrame frame = new JFrame();
frame.add(new PaintPercentImage());
frame.add(spinner, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showGui();
}
});
}
}