你好我一直试图用鼠标绘制几何形状,通过选择一个按钮来绘制什么并让它们留在Panel上。但到目前为止只剩下其中一个停留,当我试图绘制另一个时,最后一个消失了。我怎么能让他们留下来?
这是我的代码:
我在哪里创建一个框架
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Drawer extends JFrame implements ActionListener
{
platno p;
private final JButton About ;
private final JButton Save ;
private final JButton Load ;
private final JButton Rect;
private final JButton Oval;
private final JPanel panel;
private final JPanel panel2;
private final JPanel platno;
public Drawer()
{
super("Drawer");
p = new platno();
p.nacitaj("geomtvar.txt");
p.parser();
setLayout(new BorderLayout());
platno = p;
getContentPane().add(platno,BorderLayout.CENTER);
panel = new JPanel();
panel2 = new JPanel();
About = new JButton("About");
Save = new JButton("Save");
Load = new JButton("Load");
Rect = new JButton("Rectangle");
Oval = new JButton("Oval");
Dimension d = new Dimension(800,600);
panel.setLayout(new FlowLayout()); panel2.setLayout(new FlowLayout());
About.addActionListener(this); Save.addActionListener(this); Load.addActionListener(this); Rect.addActionListener(this); Oval.addActionListener(this);
panel.add(About); panel.add(Save); panel.add(Load);
panel2.add(Rect); panel2.add(Oval);
add(panel,BorderLayout.SOUTH);
add(panel2,BorderLayout.EAST);
pack();
setSize(d);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
Drawer z = new Drawer();
}
@Override
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == Load)
{
p.check = true;
p.repaint();
}
else if(ae.getSource() == About)
{
JFrame f = new JFrame("About");
JLabel l = new JLabel("2016");
f.add(l);
f.setSize(200, 200);
f.setVisible(true);
f.setLocationRelativeTo(null);
}
else if (ae.getSource() == Rect)
{
p.setType(1);
p.repaint();
}
else if (ae.getSource() == Oval)
{
p.setType(0);
}
}
}
在这里,我从文件中加载了形状,然后我希望它们用鼠标绘制。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.LinkedList;
import javax.swing.*;
public abstract class GeomShape extends JPanel
{
protected int width; protected int xpos;
protected int height; protected int ypos;
protected int radius;
protected int R,G,B;
protected int shape;
public double get_obvod(){return 1;};
public double get_obsah(){return 1;};
int x1, x2, y1, y2;
}
class rectangle extends GeomShape
{
protected boolean isNewRect = true;
@Override
public double get_obvod()
{
return (2*this.width)+(2*this.height);
}
@Override
public double get_obsah()
{
return this.width*this.height;
}
protected rectangle getRectangleFromPoints(rectangle tmp)
{
int width = this.x1 - this.x2;
int height = this.y1 - this.y2;
tmp.width = Math.abs(width); tmp.height = Math.abs(height); tmp.xpos = this.x1 ; this.ypos = this.y1;
return tmp;
}
}
class oval extends GeomShape
{
protected boolean isNewOval = true;
@Override
public double get_obvod()
{
return 2*Math.PI*this.radius;
}
@Override
public double get_obsah()
{
return Math.PI*Math.pow(this.radius,2);
}
protected oval getCircleFromPoints(oval tmp)
{
int width = this.x1 - this.x2;
int height = this.y1 - this.y2;
tmp.width = Math.abs(width); tmp.height = Math.abs(height); tmp.xpos = this.x1 ; this.ypos = this.y1;
return tmp;
}
}
class platno extends JPanel implements MouseListener,MouseMotionListener
{
platno ()
{
addMouseListener( this );
addMouseMotionListener( this );
}
public int filesize;
protected String[] data;
protected LinkedList <GeomShape> objekty = new LinkedList<GeomShape>();
protected int shape = Integer.MAX_VALUE;
public boolean check;
public boolean started = false;
protected rectangle rect = new rectangle();
protected oval circle = new oval();
protected GeomShape tmp;
protected int i = filesize;
public void nacitaj(String filename)
{
String text="", tmp=""; int i=0;
try
{
FileInputStream subor = new FileInputStream(filename);
InputStreamReader in = new InputStreamReader(subor);
BufferedReader zoSuboru = new BufferedReader(in);
LineNumberReader lnr = new LineNumberReader(new FileReader(new File(filename)));
lnr.skip(Long.MAX_VALUE);
this.filesize = lnr.getLineNumber() + 1 ;
lnr.close();
this.data = new String[this.filesize];
while((tmp=zoSuboru.readLine())!=null)
{
text=tmp+"\n";
this.data[i] = text;
System.out.println(this.data[i]);
i++;
}
zoSuboru.close();
}
catch(Exception e)
{
System.out.println("Error");
}
}
public void parser()
{
String[] tmp; int i = 0;
while(i<this.filesize)
{
tmp = this.data[i].split(";");
if(tmp[0].startsWith("0"))
{
objekty.add(new oval());
objekty.get(i).shape = Integer.parseInt(tmp[0]);
objekty.get(i).xpos = Integer.parseInt(tmp[1]);
objekty.get(i).ypos = Integer.parseInt(tmp[2]);
objekty.get(i).R = Integer.parseInt(tmp[3]);
objekty.get(i).G = Integer.parseInt(tmp[4]);
objekty.get(i).B = Integer.parseInt(tmp[5]);
objekty.get(i).radius = Integer.parseInt(tmp[6]);
}
else if (tmp[0].startsWith("1"))
{
objekty.add(new rectangle());
objekty.get(i).shape = Integer.parseInt(tmp[0]);
objekty.get(i).xpos = Integer.parseInt(tmp[1]);
objekty.get(i).ypos = Integer.parseInt(tmp[2]);
objekty.get(i).R = Integer.parseInt(tmp[3]);
objekty.get(i).G = Integer.parseInt(tmp[4]);
objekty.get(i).B = Integer.parseInt(tmp[5]);
objekty.get(i).width = Integer.parseInt(tmp[6]);
objekty.get(i).height = Integer.parseInt(tmp[7]);
}
i++;
}
}
public void setType(int arg)
{
if(arg == 0)
{
this.shape = 0;
}
else if(arg == 1)
{
this.shape = 1;
}
}
public Graphics load(Graphics g)
{
for(int i = 0 ; i < this.filesize ; i++)
{
if(objekty.get(i).shape == 0)
{
Color c = new Color(objekty.get(i).R,objekty.get(i).G,objekty.get(i).B);
g.setColor(c); //nastavenie farby z atributov
g.fillOval(objekty.get(i).xpos , objekty.get(i).ypos , (objekty.get(i).radius/2), (objekty.get(i).radius/2)); // vykreslenie
}
else if (objekty.get(i).shape == 1)
{
Color c = new Color(objekty.get(i).R,objekty.get(i).G,objekty.get(i).B);
g.setColor(c);
g.fillRect(objekty.get(i).xpos,objekty.get(i).ypos,objekty.get(i).width, objekty.get(i).height); // vykreslenie
}
}
return g;
}
/*
public GeomShape[] bigger(Geomtvar old_array[])
{
GeomShape new_array[] = new Geomtvar[old_array.length+1];
for(int i = 0 ; i<old_array.length;i++)
{
new_array[i]=old_array[i];
}
return new_array;
}
*/
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(check==true)
{
g = load(g);
}
if(this.shape == 1 && started == true)
{
if ( !rect.isNewRect )
{
//g.fillRect( (int) rect.xpos,(int) rect.ypos, (int)rect.sirka, (int) rect.dlzka );
g.fillRect( objekty.getLast().xpos,objekty.getLast().ypos, objekty.getLast().width, objekty.getLast().height );
}
for(int k = 0;k<objekty.size();k++)
{
g.fillRect( objekty.get(k).xpos,objekty.get(k).ypos, objekty.get(k).width, objekty.get(k).height );
}
}
if(this.shape == 0 && started == true)
{
if ( !circle.isNewOval )
{
g.fillOval(objekty.getLast().xpos,objekty.getLast().ypos, objekty.getLast().width, objekty.getLast().height );
}
for(int k = 0;k<objekty.size();k++)
{
g.fillOval (objekty.get(k).xpos,objekty.get(k).ypos, objekty.get(k).width, objekty.get(k).height );
}
}
}
public void mouseClicked( final MouseEvent event )
{
}
public void mousePressed( final MouseEvent event )
{
started = true;
if(this.shape == 1)
{
rect.x1 = event.getX(); rect.y1 = event.getY();
objekty.add(rect);
objekty.set(objekty.indexOf(objekty.getLast()), rect);
}
if(this.shape == 0)
{
circle.x1 = event.getX(); circle.y1 = event.getY();
objekty.add(circle);
objekty.set(objekty.indexOf(objekty.getLast()), circle);
}
repaint();
}
public void mouseReleased( final MouseEvent event )
{
if(this.shape == 1)
{
rect.x2 = event.getX(); rect.y2 = event.getY();
rect = rect.getRectangleFromPoints(rect);
objekty.set(objekty.indexOf(objekty.getLast()), rect);
repaint();
//rect.sirka = rect.dlzka = rect.x1 = rect.y1 = rect.x2 = rect.y2 = 0;
rect.isNewRect = false;
}
else if (this.shape == 0)
{
circle.x2 = event.getX(); circle.y2 = event.getY();
circle = circle.getCircleFromPoints(circle);
objekty.set(objekty.indexOf(objekty.getLast()), circle);
repaint();
//circle.sirka = circle.dlzka = circle.x1 = circle.x2 = circle.y1 = circle.y2 =0 ;
circle.isNewOval = false;
}
System.out.println("You're done.");
}
public void mouseEntered( final MouseEvent event ) {}
public void mouseExited( final MouseEvent event ) {}
public void mouseDragged( final MouseEvent event )
{
System.out.println("Drawing.");
if(this.shape == 1)
{
rect.x2 = event.getX(); rect.y2 = event.getY();
rect = rect.getRectangleFromPoints(rect);
objekty.set(objekty.indexOf(objekty.getLast()), rect);
}
if(this.shape == 0)
{
circle.x2 = event.getX(); circle.y2 = event.getY();
circle = circle.getCircleFromPoints(circle);
objekty.set(objekty.indexOf(objekty.getLast()), circle);
}
repaint();
}
public void mouseMoved( final MouseEvent event ) { System.out.println("You are moving!");}
}
然后当我完成绘画时,我希望他们都留在画面上。我怎么做 ?
我知道我在某个地方遇到了一些问题,但是我无法找到...帮助真的会被贬低。