I have tried drawing diamond grapich2D, how to make SceleUp when I click the button How to draw a diamond shape in java? the sheigth variable was successfully changed, but there was no change in size on the diamond?
the question is how to change the size of diamond when clicked on the button? is there something wrong with the code below.
public class JavaApplication251 {
public static void main(String[] args) {
new JavaApplication251();
}
public JavaApplication251() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Diamond diamond;
public TestPane() {
Button scaleUP = new Button("Scale Up");
scaleUP.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("ButonClick");
System.out.println(diamond.getSheight());
diamond.setSheight(diamond.getSheight() +30);
diamond.SceleUpdate();
}
});
add(scaleUP);
diamond = new Diamond(100, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - diamond.getBounds().width) / 2;
int y = (getHeight()- diamond.getBounds().height) / 2;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
Shape shape = at.createTransformedShape(diamond);
g2d.setColor(Color.YELLOW);
g2d.fill(shape);
g2d.setColor(Color.RED);
g2d.draw(shape);
g2d.dispose();
}
}
public class Diamond extends Path2D.Double {
private double sWidth;
private double sheight;
public Diamond(double width, double height) {
sWidth = width;
sheight = height;
SceleUpdate();
}
void SceleUpdate(){
moveTo(0, sheight / 2);
lineTo(sWidth / 2, 0);
lineTo(sWidth, sheight / 2);
lineTo(sWidth / 2, sheight);
closePath();
}
public double getsWidth() {
return sWidth;
}
public void setsWidth(double sWidth) {
this.sWidth = sWidth;
}
public double getSheight() {
return sheight;
}
public void setSheight(double sheight) {
this.sheight = sheight;
}
}
}