Frame1:
package client;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
/**
* @author user
*/
public class Frame1 extends javax.swing.JFrame {
private Grafica1 jPanel1 = new Grafica1();
/** Creates new form Frame1 */
public Frame1() {
initComponents();
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void initComponents() {//GEN-BEGIN:initComponents
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton5.setText("up");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
});
jButton6.setText("down");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
});
jButton7.setText("right");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
});
jButton8.setText("left");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(157, 157, 157)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton6)
.addComponent(jButton5))
.addContainerGap(183, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(61, 61, 61)
.addComponent(jButton8)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton7)
.addGap(53, 53, 53))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addComponent(jButton5)
.addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton8))
.addGap(12, 12, 12)
.addComponent(jButton6)
.addContainerGap(173, Short.MAX_VALUE))
);
pack();
}//GEN-END:initComponents
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_jButton8ActionPerformed
jPanel1.s=jPanel1.s-10;
jPanel1.repaint();// TODO add your handling code here:
}//GEN-LAST:event_jButton8ActionPerformed
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_jButton5ActionPerformed
jPanel1.b=jPanel1.b-10;
if (jPanel1.b >200){
double n, m=7;
int c=5;
c = (int) (c + m);
n=10/(Math.tan(c));
n=jPanel1.a-n;
jPanel1.i = (int) (jPanel1.i - m);
jPanel1.repaint();}
else
jPanel1.b=jPanel1.b+10;
// TODO add your handling code here:
}//GEN-LAST:event_jButton5ActionPerformed
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_jButton7ActionPerformed
jPanel1.s=jPanel1.s+10;
jPanel1.repaint();// TODO add your handling code here:
}//GEN-LAST:event_jButton7ActionPerformed
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-
FIRST:event_jButton6ActionPerformed
jPanel1.b=jPanel1.b+10;
if (jPanel1.b <410){
double n, m=7;
int c=5;
c = (int) (c + m);
n=10/(Math.tan(c));
n=jPanel1.a-n;
jPanel1.i = (int) (jPanel1.i + m);
jPanel1.repaint();}
else
jPanel1.b=jPanel1.b-10;
// TODO add your handling code here:
}//GEN-LAST:event_jButton6ActionPerformed
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration//GEN-END:variables
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing
.UIManager
.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing
.UIManager
.setLookAndFeel(info.getClassName());
break;
} catch (ClassNotFoundException ex) {
java.util
.logging
.Logger
.getLogger(Frame1.class.getName())
.log(java.util
.logging
.Level
.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util
.logging
.Logger
.getLogger(Frame1.class.getName())
.log(java.util
.logging
.Level
.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util
.logging
.Logger
.getLogger(Frame1.class.getName())
.log(java.util
.logging
.Level
.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util
.logging
.Logger
.getLogger(Frame1.class.getName())
.log(java.util
.logging
.Level
.SEVERE, null, ex);
//</editor-fold>
/* Create and display the form */
java.awt
.EventQueue
.invokeLater(new Runnable() {
public void run() {
new Frame1().setVisible(true);
});
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
// End of variables declaration//GEN-END:variables
private void jbInit() throws Exception {
this.getContentPane().setLayout( null );
this.setSize(new Dimension(650, 650));
this.setTitle( "Exercitii grafica 2D" );
jPanel1.setBounds(new Rectangle(0, 0, 550, 550));
jPanel1.setLayout(null);
this.getContentPane().add(jPanel1, null);
}}
Grafica1 :
package client;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
/**
* @author user
*/
public class Grafica1 extends javax.swing.JPanel {
public int a=190, b=300,i=0,s=0,j=0;
/** Creates new form Grafica1 */
public Grafica1() {
initComponents();
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
private void initComponents() {//GEN-BEGIN:initComponents
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
public void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.black);
double lungime = 10;
double unghiX = 30;//grade
double unghiY = 60; // grade
double angle;
double rotireXX = lungime * Math.cos(Math.toRadians(unghiX));
double rotireYX = lungime * Math.sin(Math.toRadians(unghiX));
double rotireXY = lungime * Math.cos(Math.toRadians(unghiY));
double rotireYY = lungime * Math.sin(Math.toRadians(unghiY));
g2.setStroke(new BasicStroke(1));
g2.drawLine(20, 400, 500, 400);
g2.drawLine(20, 20, 20, 400);
g2.drawLine(500, 400, 500 - (int) rotireXX, 400 + (int) rotireYX);
g2.drawLine(500, 400, 500 - (int) rotireXX, 400 - (int) rotireYX);
g2.drawLine(20, 20, 20 - (int) rotireXY, 20 + (int) rotireYY);
g2.drawLine(20, 20, 20 + (int) rotireXY, 20 + (int) rotireYY);
g2.setStroke(new BasicStroke(3));
g2.drawRect(80-15-i+j+s, 400-15, 30, 30);
g2.drawRect(300-15+i-j+s, 400-15, 30, 30);
g2.drawString("Robot Plan-Paralel", 10,450);
g2.drawString("E(Xe, Ye)", a+s,b);
g2.drawLine(80-i+j+s,400, a+s, b);
g2.drawLine(300+i-j+s, 400, a+s, b);
g2.setStroke(new BasicStroke(1));
g2.drawOval(a-4+s, b-4, 8, 8);
g2.drawLine(20,205,500,205);
/*Graphics2D g2 = (Graphics2D)g;
double leftX = 50;
double topY = 50;
double width = 200;
double height = 150;
//trasare dreptunghi
Rectangle2D dreptunghi = new Rectangle2D.Double(leftX, topY, width, height);
g2.draw(dreptunghi);
//schimbare culoare
//trasare elipsa
g2.setPaint(Color.RED);
Ellipse2D elipsa = new Ellipse2D.Double();
elipsa.setFrame(dreptunghi);
//umplere elipsa
g2.fill(elipsa);
g2.draw(elipsa);
//desenare cerc
double centruX = dreptunghi.getCenterX();
double centruY = dreptunghi.getCenterY();
double raza = 120;
Ellipse2D cerc = new Ellipse2D.Double();
cerc.setFrameFromCenter(centruX, centruY, centruX+raza, centruY+raza);
g2.draw(cerc);
//desenare linie
g2.draw(new Line2D.Double(leftX, topY, leftX+width,topY+height));
g2.setPaint(Color.BLUE);
//desenare text
g2.drawString("Salutare! ", (int)leftX, (int)topY-2);
masina(g2);*/
/* void masina(Graphics2D g2 ) {
Rectangle2D r;
g2.draw(r = new Rectangle2D.Double(100,170,200,30));
g2.fill(r);
g2.draw(r = new Rectangle2D.Double(150,100,150,100));
g2.fill(r);
g2.setPaint(Color.ORANGE);
g2.draw(r = new Rectangle2D.Double(170,110,40,55));
g2.fill(r);
g2.setPaint(Color.BLACK);
Ellipse2D c;
g2.draw(c = new Ellipse2D.Double(160,180,40,40));
g2.fill(c);
g2.draw(c = new Ellipse2D.Double(240,170,50,50));
g2.fill(c);
g2.setPaint(Color.WHITE);
g2.draw(c = new Ellipse2D.Double(165,185,30,30));
g2.fill(c);
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables