如何使用repaint()
和run()
保留以前的更改,或者是否有其他解决方案。
我的代码运行良好。此代码描绘了3个椭圆形。当我在文本框中输入x坐标的输入时,它会绘制/绘制新的椭圆。但是,每当我在文本框中输入和更改x坐标时,椭圆就会重新定位。我想要做的是保留我之前做出的改变。例如,当您点击see coordinates
按钮时,我会在开始时获得3个椭圆,然后在输入后获得4个等等,然后单击add coordinate
按钮。
这是我的代码:
/deleted/
非常感谢任何帮助。
[编辑]
我正在实施K-最近邻居。到目前为止,我所做的是:
1.created a gray panel of size 500x500 from (0,0)
2.gets file using browse button.
3.gets string and tokenize
4.token[0]= k-nearest, token[1]= total neighbors
5.token[2]= x-coordinate, token[3]= y-coordinate, token[4]= type of cluster and so on.
6.input x-coord and y-coord
7.compute distance between coordinates from txt file and input
8.draw shape according to minimum distance
这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import java.lang.Math;
public class Knn extends JFrame implements ActionListener{
JPanel grid,panel;
JLabel xlab,ylab;
JTextArea xtex,ytex,temp;
JButton cbut,fbut;
String text;
int k, ktotal, x, y,xcori,ycori;
String[] token;
int[] itoken;
boolean paint=false, paintclass=false;
double ans,xcord,ycord;
double[] dtoken,ansarray,xarray,yarray;
Map<Double, Double> anshash;
public Knn(){
super("K-Nearest Neighbor");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,500);
//this.pack();
this.setVisible(true);
this.setResizable(true);
this.setLayout(null);
grid= new JPanel();
grid.setBackground(Color.GRAY);
grid.setBounds(0,0,500,500);
panel= new JPanel();
//panel.setLayout(null);
panel.setBounds(500,0,100,100);
temp= new JTextArea();
xlab= new JLabel("X");
xtex= new JTextArea("",1,7);
ylab= new JLabel("Y");
ytex= new JTextArea("",1,7);
cbut= new JButton("classify");
fbut= new JButton("Browse");
panel.add(xlab);
panel.add(xtex);
panel.add(ylab);
panel.add(ytex);
panel.add(cbut);
panel.add(fbut);
this.add(grid);
this.add(panel);
fbut.addActionListener(this);
cbut.addActionListener(this);
}
public double formula(double x1, double y1, double x2, double y2){
double tmpans= Math.pow((x2-x1),2.0) + Math.pow((y2-y1),2.0);
ans= Math.sqrt(tmpans);
return ans;
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==fbut){ //choose file input coordinates
JFileChooser fileChooser= new JFileChooser();
int returnVal = fileChooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile();
try {
//output file content to text box, copy to a string
temp.read( new FileReader(file.getAbsolutePath()),null);
text= temp.getText();
}catch(IOException ex){
ex.printStackTrace();
}
}
token = text.split("\\W+");
itoken=new int[token.length];
int i=0;
for (String str : token){
itoken[i++] = Integer.parseInt(str); //convert from string to integer
}
for (i = 4; i < itoken.length; i += 3) { //x50
itoken[i-2]= itoken[i-2] * 50;
itoken[i-1]= itoken[i-1] * 50;
}
paint=true;
repaint();
}
if(e.getSource()==cbut){
solveClass();
}
}
public void paint(Graphics g){
super.paint(g);
if (paint) {
for (int i = 4; i < itoken.length; i += 3) {
x = itoken[i-2];
y = itoken[i-1];
if(itoken[i]==1)
g.fillRect(x - 5, y - 5, 10, 10);
if(itoken[i]==2)
g.fillOval(x - 5, y - 5, 10, 10);
if(itoken[i]==3)
g.drawOval(x - 5, y - 5, 10, 10);
}
}
if(paintclass){
if(anshash.get(ansarray[0])==1)
g.fillRect(xcori - 5, ycori - 5, 10, 10);
if(anshash.get(ansarray[0])==2)
g.fillOval(xcori - 5, ycori - 5, 10, 10);
if(anshash.get(ansarray[0])==3)
g.drawOval(xcori - 5, ycori - 5, 10, 10);
}
}
public void solveClass(){
String xcors= xtex.getText();
String ycors= ytex.getText();
dtoken=new double[itoken.length];
xarray=new double[itoken[1]];
yarray=new double[itoken[1]];
ansarray=new double[itoken[1]];
int j=0;
for (String str : token)
dtoken[j++] = Double.parseDouble(str); //convert from string to double
//convert to int
xcori= Integer.parseInt(xcors);
ycori= Integer.parseInt(ycors);
//convert to double
xcord= Double.parseDouble(xcors);
ycord= Double.parseDouble(ycors);
int q=0;
for (int i = 3; i < itoken.length; i += 3) {
xarray[q]= dtoken[i-1];
yarray[q]= dtoken[i];
q++;
}
for (int i = 0; i < xarray.length; i ++)
ansarray[i]= formula(xcord,ycord,xarray[i]*50,yarray[i]*50); //x50
anshash= new HashMap<Double,Double>();
int r=0;
for(int i = 4; i < itoken.length; i += 3){
anshash.put(ansarray[r], dtoken[i]);
r++;
}
Arrays.sort(ansarray); //sort
paintclass=true;
repaint();
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Knn knn= new Knn();
}
});
}
}
这是文本文件:
3 5
1 2 1
4 3 2
7 5 2
6 9 3
5 5 1
答案 0 :(得分:1)
“但是,每当我在文本框中输入和更改x坐标时,椭圆就会重新定位。我想要做的是保留我之前做过的更改”
使用List
球对象。这是Ball
类
public class Ball {
int x;
int y;
public Ball(int x, int y) {
this.x = x;
this.y = y;
}
public void drawball(Graphics g) {
g.fillOval(x, y, 20, 20);
}
}
这是JPanel
类
public class BallPanel extends JPanel {
private List<Ball> balls;
@Override
protected void paintComponent(Graphics g) {
super.paintCompoent(g);
for (Ball ball: balls) {
ball.drawBall(g);
}
}
}
基本点是保持球位置的数据结构。您可能还希望为x和y设置getter和setter,以便操作它们的位置。
要添加更多球,您需要做的就是添加到List
和new Ball(...)
并致电repaint();
这是一个正在运行的例子。只需输入x
和y
小于500,然后点击按钮即可。你会看到更多的球被添加。您可以在actionPerformed
中看到我添加new Ball
的位置以及paintCompoent
我在那里循环播放List
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class TestBall extends Frame implements ActionListener {
private JTextField enterX;
private JTextField enterY;
private JLabel labelX;
private JLabel labelY;
private JButton addBall;
private List<Ball> balls;
private BallPanel ballPanel;
public TestBall() {
balls = new ArrayList<>();
ballPanel = new BallPanel();
JPanel p1 = new JPanel();
labelX = new JLabel("Enter X");
enterX = new JTextField(6);
labelY = new JLabel("Enter Y");
enterY = new JTextField(6);
addBall = new JButton("Add Ball");
addBall.addActionListener(TestBall.this);
p1.add(labelX);
p1.add(enterX);
p1.add(labelY);
p1.add(enterY);
p1.add(addBall);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(ballPanel, BorderLayout.CENTER);
frame.add(p1, BorderLayout.PAGE_START);
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
int x = Integer.parseInt(enterX.getText());
int y = Integer.parseInt(enterY.getText());
balls.add(new Ball(x, y));
ballPanel.repaint();
}
public class BallPanel extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Ball ball : balls) {
ball.drawBall(g);
}
}
}
public class Ball {
int x;
int y;
public Ball(int x, int y) {
this.x = x;
this.y = y;
}
public void drawBall(Graphics g) {
g.fillOval(x, y, 20, 20);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestBall();
}
});
}
}