JavaGUI窗口显示垃圾
我编写了一个Java程序,根据用户执行的按钮点击,获取一个三角形并旋转、移位或旋转并移位 在此之前,我指示用户输入逻辑坐标范围,以确定像素坐标将如何映射到真实的x-y坐标系JavaGUI窗口显示垃圾,java,swing,paintcomponent,Java,Swing,Paintcomponent,我编写了一个Java程序,根据用户执行的按钮点击,获取一个三角形并旋转、移位或旋转并移位 在此之前,我指示用户输入逻辑坐标范围,以确定像素坐标将如何映射到真实的x-y坐标系 最初,在屏幕中间出现三角形,在单击按钮之后,在其上预先确定了某个操作(即旋转、移位等)后显示三角形。 但是,在操作完成并重新绘制三角形后,我看到在JPanel的左上角也绘制了一个输入框 我不确定这是怎么一直被吸引到那里的 代码: 导入java.awt.Color; 导入java.awt.Dimension; 导入java.a
最初,在屏幕中间出现三角形,在单击按钮之后,在其上预先确定了某个操作(即旋转、移位等)
后显示三角形。 但是,在操作完成并重新绘制三角形后,我看到在JPanel的左上角也绘制了一个输入框 我不确定这是怎么一直被吸引到那里的 代码:导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
公共类RotateandShift三角形扩展JFrame{
公共静态void main(字符串[]args){new rotateandshift triangles();}
旋转和移动三角形(){
超级(“绘制50个三角形”);
最终JPanel drawingPanel=新的DrawTriangles();
JPanel buttonPanel=新的JPanel();
JButton rotate=新JButton(“rotate”),
shift=新按钮(“shift”),
rotateShift=新按钮(“旋转和移动”),
重置=新的按钮(“重置”);
拉丝板.立根(颜色.白色);
按钮面板。添加(旋转);
按钮面板。添加(shift);
按钮面板添加(旋转移位);
按钮面板添加(重置);
addWindowListener(新的WindowAdapter(){
公共无效窗口关闭(WindowEvent e){
系统出口(0);
}
});
DrawTriangles.rWidth=Float.parseFloat(JOptionPane.showInputDialog(“Input rWidth”);
DrawTriangles.rHeight=Float.parseFloat(JOptionPane.showInputDialog(“Input rHeight”);
rotate.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
DrawTriangles.rotate=true;
drawingPanel.repaint();
}
});
shift.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
DrawTriangles.shift=true;
drawingPanel.repaint();
}
});
addActionListener(新ActionListener()){
已执行的公共无效操作(操作事件e){
DrawTriangles.rotate=true;
DrawTriangles.shift=true;
drawingPanel.repaint();
}
});
reset.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
DrawTriangles.reset=true;
drawingPanel.repaint();
}
});
设置大小(600400);
添加(“南”,按钮面板);
添加(“中心”,绘图面板);
setVisible(真);
}
}
类DrawTriangles扩展了JPanel{
静态浮点rWidth、rHeight、pixelSize;
静态整数maxX、maxY、minMaxXY、centerX、centerY;
静态布尔旋转=假,移位=假,重置=假;
浮动角度=0;
void initialize(){
维度d=getSize();
maxX=d.宽度-1;maxY=d.高度-1;
pixelSize=Math.max(rWidth/maxX,rHeight/maxY);
minMaxXY=Math.min(maxX,maxY);
centerX=maxX/2;centerY=maxY/2;
}
公共intix2(float x){return Math.round(x);}
public int iY2(float y){return maxY-Math.round(y);}
公共静态int iX(float x){return Math.round(centerX+x/pixelSize);}
公共静态int-iY(float y){返回Math.round(centerY-y/pixelSize);}
公共静态浮点fx(int x){return(x-centerX)*pixelSize;}
公共静态浮点fy(int y){return(centerY-y)*pixelSize;}
公共空间涂料(图g){
超级组件(g);
初始化();
int left=iX(-rWidth/2),right=iX(rWidth/2);
int-top=iY(rHeight/2),bot=iY(-rHeight/2);
g、 抽绳(“X:+-rWidth/2+”Y:+rHeight/2,左侧,顶部+10);
g、 抽绳(“X:+rWidth/2+”Y:+rHeight/2,右-55,顶部+10);
g、 抽绳(“X:+-rWidth/2+”Y:+-rHeight/2,左,bot);
g、 抽绳(“X:+rWidth/2+”Y:+-rHeight/2,右-55,bot);
g、 setColor(Color.BLUE);
g、 drawRect(左、上、右-左、机器人-上);
浮动侧=0.95f*最小最大XY,侧半=0.5F*侧,
h=侧半*(浮动)数学sqrt(3),
xA,yA,xB,yB,xC,yC,
xA1,yA1,xB1,yB1,xC1,yC1,p,q;
q=0.05F;
p=1-q;
xA=中心X-侧半部;
yA=中心-0.5F*h;
xB=中心X+侧半部;
yB=yA;
xC=中心x;
yC=centerY+0.5F*h;
如果(!重置){
如果(旋转){
角度+=Float.parseFloat(JOptionPane.showInputDialog(“输入旋转角度(度)”);
float xR=fx(Integer.parseInt(JOptionPane.showInputDialog(“输入旋转的X坐标”)),
yR=fx(Integer.parseInt(JOptionPane.showInputDialog(“输入旋转的Y坐标”));
xA=旋转度(xA,yA,xR,yR,角);
yA=旋转(xA,yA,xR,yR,角);
xB=旋转度(xB,yB,xR,yR,角);
yB=旋转(xB,yB,xR,yR,角度);
xC=rotateX(xC,yC,xR,yR,角);
yC=旋转(xC,yC,xR,yR,角度);
旋转=假;
}
如果(班次){
float xShift=-fx(Integer.parseInt(JOptionPane.showInputDialog)(“为Sh输入X坐标
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class RotateAndShiftTriangles extends JFrame {
public static void main(String[] args) { new RotateAndShiftTriangles(); }
RotateAndShiftTriangles() {
super("Drawing 50 Triangles");
final JPanel drawingPanel = new DrawTriangles();
JPanel buttonPanel = new JPanel();
JButton rotate = new JButton("Rotate"),
shift = new JButton("Shift"),
rotateShift = new JButton("Rotate and Shift"),
reset = new JButton ("Reset");
drawingPanel.setBackground(Color.WHITE);
buttonPanel.add(rotate);
buttonPanel.add(shift);
buttonPanel.add(rotateShift);
buttonPanel.add(reset);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
DrawTriangles.rWidth = Float.parseFloat(JOptionPane.showInputDialog("Input rWidth"));
DrawTriangles.rHeight = Float.parseFloat(JOptionPane.showInputDialog("Input rHeight"));
rotate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DrawTriangles.rotate = true;
drawingPanel.repaint();
}
});
shift.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DrawTriangles.shift = true;
drawingPanel.repaint();
}
});
rotateShift.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DrawTriangles.rotate = true;
DrawTriangles.shift = true;
drawingPanel.repaint();
}
});
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrawTriangles.reset = true;
drawingPanel.repaint();
}
});
setSize(600, 400);
add("South", buttonPanel);
add("Center", drawingPanel);
setVisible(true);
}
}
class DrawTriangles extends JPanel {
static float rWidth, rHeight, pixelSize;
static int maxX, maxY, minMaxXY, centerX, centerY;
static boolean rotate = false, shift = false, reset = false;
float angle = 0;
void initialize() {
Dimension d = getSize();
maxX = d.width - 1; maxY = d.height - 1;
pixelSize = Math.max(rWidth / maxX, rHeight / maxY);
minMaxXY = Math.min(maxX, maxY);
centerX = maxX/2; centerY = maxY/2;
}
public int iX2(float x) { return Math.round(x); }
public int iY2(float y) { return maxY - Math.round(y); }
public static int iX(float x) { return Math.round(centerX + x / pixelSize); }
public static int iY(float y) { return Math.round(centerY - y / pixelSize); }
public static float fx(int x) { return (x - centerX) * pixelSize; }
public static float fy(int y) { return (centerY - y) * pixelSize; }
public void paint(Graphics g) {
super.paintComponent(g);
initialize();
int left = iX(-rWidth/2), right = iX(rWidth/2);
int top = iY(rHeight/2), bot = iY(-rHeight/2);
g.drawString("X: " + -rWidth/2 + " Y: " + rHeight/2, left, top + 10);
g.drawString("X: " + rWidth/2 + " Y: " + rHeight/2, right - 55, top + 10);
g.drawString("X: " + -rWidth/2 + " Y: " + -rHeight/2, left, bot);
g.drawString("X: " + rWidth/2 + " Y: " + -rHeight/2, right - 55, bot);
g.setColor(Color.BLUE);
g.drawRect(left, top, right - left, bot - top);
float side = 0.95f * minMaxXY, sideHalf = 0.5F * side,
h = sideHalf * (float)Math.sqrt(3),
xA, yA, xB, yB, xC, yC,
xA1, yA1, xB1, yB1, xC1, yC1, p, q;
q = 0.05F;
p = 1 - q;
xA = centerX - sideHalf;
yA = centerY - 0.5F * h;
xB = centerX + sideHalf;
yB = yA;
xC = centerX;
yC = centerY + 0.5F * h;
if(!reset) {
if(rotate) {
angle += Float.parseFloat(JOptionPane.showInputDialog("Input Angle of Rotation (in degrees)"));
float xR = fx(Integer.parseInt(JOptionPane.showInputDialog("Input X Coordinate for Rotation"))),
yR = fx(Integer.parseInt(JOptionPane.showInputDialog("Input Y Coordinate for Rotation")));
xA = rotateX(xA, yA, xR, yR, angle);
yA = rotateY(xA, yA, xR, yR, angle);
xB = rotateX(xB, yB, xR, yR, angle);
yB = rotateY(xB, yB, xR, yR, angle);
xC = rotateX(xC, yC, xR, yR, angle);
yC = rotateY(xC, yC, xR, yR, angle);
rotate = false;
}
if(shift) {
float xShift = -fx(Integer.parseInt(JOptionPane.showInputDialog("Input X Coordinate for Shift"))),
yShift = -fx(Integer.parseInt(JOptionPane.showInputDialog("Input Y Coordinate for Shift")));
xA += xShift;
yA += yShift;
xB += xShift;
yB += yShift;
xC += xShift;
yC += yShift;
shift = false;
}
}
g.setColor(Color.RED);
for (int i = 0; i < 50; i++) {
g.drawLine(iX2(xA), iY2(yA), iX2(xB), iY2(yB));
g.drawLine(iX2(xB), iY2(yB), iX2(xC), iY2(yC));
g.drawLine(iX2(xC), iY2(yC), iX2(xA), iY2(yA));
if(i == 0) {
g.setColor(Color.BLACK);
g.drawString("A: X- " + xA + " Y- " + yA, 0, 50);
g.drawString("B: X- " + xB + " Y- " + yB, 0, 60);
g.drawString("C: X- " + xC + " Y- " + yC, 0, 70);
g.setColor(Color.RED);
}
xA1 = p * xA + q * xB;
yA1 = p * yA + q * yB;
xB1 = p * xB + q * xC;
yB1 = p * yB + q * yC;
xC1 = p * xC + q * xA;
yC1 = p * yC + q * yA;
xA = xA1; xB = xB1; xC = xC1;
yA = yA1; yB = yB1; yC = yC1;
}
if(reset)
angle = 0;
reset = false;
}
public float rotateX(float x, float y, float xR, float yR, float angle) {
angle *= (Math.PI / 180.0);
float c = (float)Math.cos(angle), s = (float)Math.sin(angle),
xF = x - xR, yF = y - yR,
rx = xF * c - yF * s;
return rx + xR;
}
public float rotateY(float x, float y, float xR, float yR, float angle) {
angle *= (Math.PI / 180.0);
float c = (float)Math.cos(angle), s = (float)Math.sin(angle),
xF = x - xR, yF = y - yR,
ry = xF * s + yF * c;
return ry + yR;
}
}
public void paint(Graphics g) {
super.paintComponent(g);
public void paintComponent(Graphics g) {
super.paintComponent(g);
add("South", buttonPanel);
add("Center", drawingPanel);
add(buttonPanel, BordeLayout.PAGE_END);
add("Center", BorderLayout.CENTER);
public void setRotate(Boolean rotate)
{
this.rotate = rotate
repaint();
}
//DrawTriangles.rotate = true; // wrong
drawingPanel.setRotate(true);
if(!reset) {
if(rotate) {
angle += Float.parseFloat("15");
float xR = fx(3),
yR = fx(3);
// other stuff...
}
shift.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
float xShift = -DrawTriangles.fx(Integer.parseInt(JOptionPane.showInputDialog("Input X Coordinate for Shift"))),
yShift = -DrawTriangles.fx(Integer.parseInt(JOptionPane.showInputDialog("Input Y Coordinate for Shift")));
drawingPanel.xShift = xShift;
drawingPanel.yShift = yShift;
DrawTriangles.shift = true;
drawingPanel.repaint();
}
});
public void paint(Graphics gg) {
BufferedImage bf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = bf.createGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
...
gg.drawImage(bf, 0, 0, null);
if(reset)
angle = 0;
reset = false;
}
angle += Float.parseFloat(JOptionPane.showInputDialog("Input Angle of Rotation (in degrees)"));
float xR = fx(Integer.parseInt(JOptionPane.showInputDialog("Input X Coordinate for Rotation"))),
yR = fx(Integer.parseInt(JOptionPane.showInputDialog("Input Y Coordinate for Rotation")));
float xShift = -fx(Integer.parseInt(JOptionPane.showInputDialog("Input X Coordinate for Shift"))),
yShift = -fx(Integer.parseInt(JOptionPane.showInputDialog("Input Y Coordinate for Shift")));