Java for循环和图形问题
我的代码:Java for循环和图形问题,java,graphics,for-loop,polygon,graphics2d,Java,Graphics,For Loop,Polygon,Graphics2d,我的代码: import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Polygon; import java.awt.RenderingHints; import java.util.ArrayList; import javax.swing.ImageIcon; im
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
public class HexagonWDW extends JFrame {
public HexagonWDW() {
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("I am a duck!");
add(new Hexas());
setVisible(true);
}
public static void main(String[] args) {
new HexagonWDW();
}
}
class Hexas extends JPanel {
Image[] img;
int xmax;
int ymax;
int totaloff;
ArrayList<Integer> xs;
ArrayList<Integer> ys;
ArrayList<Integer> ims;
public Hexas() {
totaloff = 0;
img = new Image[3];
xs = new ArrayList();
ys = new ArrayList();
ims = new ArrayList();
xmax = 2;
ymax = 2;
img[0] = new ImageIcon("img.png").getImage();
img[1] = new ImageIcon("blue.png").getImage();
img[2] = new ImageIcon("green.png").getImage();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(rh);
init(g2);
ims.set(1, 1);
updateIcons(g2);
}
public void init(Graphics2D g2) {
int xr = 0;
int yr = 0;
setBackground(Color.white);
Dimension size = getSize();
double w = size.getWidth();
double h = size.getHeight();
int r = 20;
double pi = Math.PI;
int q = 1;
for (int j = 0; j < ymax; j++) {
System.out.println("Y: " + j);
q = q == 1 ? 0 : 1;
totaloff += q;
int aq = q == 1 ? 0 : 1;
int y = 50 + j * 20;
for (int a = 0; a < (q + xmax); a++) {
Polygon sprite = new Polygon();
int x = 50 + a * 65 + aq * 32;
System.out.println("Totaloff: " + totaloff + " and q: " + q);
System.out.println("Poly: " + (int) j * xmax + totaloff + a + q + "\n");
for (int i = 0; i < 6; i++) {
xr = (int) (x + r * Math.cos(i * 2 * pi / 6));
yr = (int) (y + r * Math.sin(i * 2 * pi / 6));
xs.add(j * xmax + totaloff + a + q, xr);
ys.add(j * xmax + totaloff + a + q, yr);
ims.add(j * xmax + totaloff + a + q, 0);
sprite.addPoint(xr, yr);
System.out.println("Point added at" + i + xr + yr);
}
g2.setColor(Color.black);
g2.fillPolygon(sprite);
g2.setColor(Color.blue);
g2.drawPolygon(sprite);
int cur = ims.get(j * ymax + totaloff + a + q);
g2.drawImage(img[cur], xr - 15, yr + 15, null);
}
}
}
public void updateIcons(Graphics g2) {
for (int i = 0; i < (xmax * ymax + totaloff); i++) {
g2.drawImage(img[ims.get(1)], xs.get(1) - 15, ys.get(1) + 15, null);
}
}
}
作为多边形输出的内容应该是每个多边形的索引,从0开始,每次递增1,但显然不是。
我能做的任何事情都可以使循环运行得更准确,我的数字达到它们应该达到的水平。我确信您不会编写左对齐的代码,所以不要期望我们阅读左对齐的代码。编辑您的问题并修复代码的格式。您不应该覆盖框架的paint()方法来进行自定义绘制。阅读上的Swing教程了解正确的方法。缓存生成
多边形的结果
,并在每次调用paintComponent
时对其进行简单的重画…1)在调试输出中添加空格,以便我们可以判断哪些数字属于哪个变量2)请注释/使用更详细的变量名,以便更容易地了解你想做什么。