Witam mam mały problem z programem tworzę okno w którym pojawiają się 2 figury geometryczne: prostokąt i kwadrat.
W momencie kiedy tworzę etykiety i pola tekstowe które by pokazywały aktualne położenie figur znikają figury.
//import sun.java2d.Surface;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Figures extends JFrame implements ActionListener {
private JMenuBar menuBar;
private JMenu mMenu;
private JMenuItem mDrawing,mEdit,mInfo,mExit;
private JLabel jX,jY,jHeight,jWidth;
private JTextField tX,tY,tHeight,tWidth;
public Figures() {
initUI();
}
private void initUI() {
setTitle("Figures");
setSize(800, 600);
setResizable(false);
add(new Surface());
setLayout(null);
jX = new JLabel("X:"); /// Kiedy to zakomentuje
jY = new JLabel("Y:");
jHeight = new JLabel("Height:");
jWidth = new JLabel("Width:");
jX.setBounds(200,500,40,30);
jY.setBounds(280,500,40,30);
jWidth.setBounds(350,500,80,30);
jHeight.setBounds(450,500,80,30);
add(jX);add(jY);add(jWidth);add(jHeight);
tX = new JTextField("888");
tY = new JTextField("888");
tHeight = new JTextField("888");
tWidth = new JTextField("888");
tX.setBounds(220,500,40,30);
tY.setBounds(300,500,40,30);
tWidth.setBounds(400,500,40,30);
tHeight.setBounds(510,500,40,30);
add(tX);add(tY);add(tWidth);add(tHeight); // aż dotąd to wszystko działa (w znaczeniu pojawiania się figur)
menuBar = new JMenuBar();
setJMenuBar(menuBar);
mMenu = new JMenu("Menu");
menuBar.add(mMenu);
mDrawing = new JMenuItem("Drawing");
mMenu.add(mDrawing);
mDrawing.addActionListener(this);
mMenu.addSeparator();
mEdit = new JMenuItem("Edit");
mMenu.add(mEdit);
mEdit.addActionListener(this);
mMenu.addSeparator();
mInfo = new JMenuItem("Information");
mMenu.add(mInfo);
mInfo.addActionListener(this);
mMenu.addSeparator();
mExit = new JMenuItem("Exit");
mMenu.add(mExit);
mExit.addActionListener(this);
//add(new Surface());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
Figures ex = new Figures();
ex.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ev) {
Object souorce = ev.getSource();
if(souorce == mEdit){
System.out.println("Edit");
}
else if(souorce == mDrawing){
System.out.println("Drawing");
}
else if(souorce == mInfo){
JOptionPane.showMessageDialog(null, " Figures 2018.1.0 \n bulit on April \n Designed to create and edit figures \n © Albert Piekielny ","Information",JOptionPane.PLAIN_MESSAGE);
}
else if(souorce == mExit){
dispose();
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
public class Surface extends JPanel{
private ZRectangle zrect;
private ZEllipse zell;
public Surface() {
initUI();
}
private void initUI() {
MovingAdapter ma = new MovingAdapter();
addMouseMotionListener(ma);
addMouseListener(ma);
addMouseWheelListener(new ScaleHandler());
zrect = new ZRectangle(50, 50, 50, 50);
zell = new ZEllipse(150, 70, 80, 80);
}
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Font font = new Font("Serif", Font.BOLD, 40);
g2d.setFont(font);
g2d.setPaint(new Color(141, 40, 200));
g2d.fill(zrect);
g2d.setPaint(new Color(5, 98, 100));
g2d.fill(zell);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
class ZEllipse extends Ellipse2D.Float {
public ZEllipse(float x, float y, float width, float height) {
setFrame(x, y, width, height);
}
public boolean isHit(float x, float y) {
return getBounds2D().contains(x, y);
}
public void addX(float x) {
this.x += x;
}
public void addY(float y) {
this.y += y;
}
public void addWidth(float w) {
this.width += w;
}
public void addHeight(float h) {
this.height += h;
}
}
class ZRectangle extends Rectangle2D.Float {
public ZRectangle(float x, float y, float width, float height) {
setRect(x, y, width, height);
}
public boolean isHit(float x, float y) {
return getBounds2D().contains(x, y);
}
public void addX(float x) {
this.x += x;
}
public void addY(float y) {
this.y += y;
}
public void addWidth(float w) {
this.width += w;
}
public void addHeight(float h) {
this.height += h;
}
}
class MovingAdapter extends MouseAdapter {
private int x;
private int y;
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
@Override
public void mouseDragged(MouseEvent e) {
doMove(e);
}
private void doMove(MouseEvent e) {
int dx = e.getX() - x;
int dy = e.getY() - y;
System.out.println(e.getX());
if (zrect.isHit(x, y)) {
zrect.addX(dx);
zrect.addY(dy);
repaint();
}
if (zell.isHit(x, y)) {
zell.addX(dx);
zell.addY(dy);
repaint();
}
x += dx;
y += dy;
}
}
class ScaleHandler implements MouseWheelListener {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
doScale(e);
}
private void doScale(MouseWheelEvent e) {
int x = e.getX(); /// to się zda ! ;)
int y = e.getY(); /// to też !
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
if (zrect.isHit(x, y)) {
float amount = e.getWheelRotation() * 5f;
zrect.addWidth(amount);
zrect.addHeight(amount);
repaint();
}
if (zell.isHit(x, y)) {
float amount = e.getWheelRotation() * 5f;
zell.addWidth(amount);
zell.addHeight(amount);
repaint();
}
}
}
}
}