Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i've tried updating my paint function but it doesn't seem to work

Java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.ImageObserver;
public class NewJApplet extends javax.swing.JApplet {
    int xW=30;
    int yW=340;
    int yW1=420;
    int xR=xW+70;
    int yR=yW;
    int yR1=yW1;
    int[] xwz = {xW ,xW , xW+70};
    int [] ywz={yW, yW+70, yW+70};
    int []ywz1={yW1,yW1+70,yW1+70};
    int []xrz = {xR, xR, xR - 70};
    int[] yrz = {yR+70, yR, yR};
    int []yrz1= {yR1+70,yR1,yR1};
    public Image offscreen;
    public Graphics h;
  private Rectangle2D.Float myRect = new Rectangle2D.Float(30, 20, 70, 70);
  private Rectangle2D.Float myRect1 = new Rectangle2D.Float(30, 100, 70, 70);
  private Rectangle2D.Float myRect2 = new Rectangle2D.Float(30, 180, 70, 70);
  private Rectangle2D.Float myRect3 = new Rectangle2D.Float(30, 260, 70, 70);
  private Polygon PW= new Polygon(xwz, ywz, 3);
  private Polygon PR= new Polygon(xrz,yrz,3);
  private Polygon PW1=new Polygon(xwz,ywz1,3);
    private Polygon PR1=new Polygon(xrz,yrz1,3);
  MovingAdapter ma = new MovingAdapter();
    public NewJApplet() {
        addMouseMotionListener(ma);
        addMouseListener(ma);
        addMouseWheelListener(new ScaleHandler());
  }
  class MovingAdapter extends MouseAdapter {
    private int x;
    private int y;
    public void mousePressed(MouseEvent e) {
      x = e.getX();
      y = e.getY();
    }
    public void mouseDragged(MouseEvent e) {
      int dx = e.getX() - x;
      int dy = e.getY() - y;
      repaint();
      if (myRect.getBounds2D().contains(x, y) ) {
            myRect.x += dx;
            myRect.y += dy;
            repaint();
      }
                if(myRect1.getBounds2D().contains(x, y))
                {
                    myRect1.x+=dx;
                    myRect1.y+=dy;
                    repaint();
                }
                if(myRect2.getBounds2D().contains(x, y))
                {
                    myRect2.x+=dx;
                    myRect2.y+=dy;
                    repaint();
                }
                if(myRect3.getBounds2D().contains(x, y))
                {
                    myRect3.x+=dx;
                    myRect3.y+=dy;
                    repaint();
                }
                if(PW.getBounds2D().contains(x, y) || PR.getBounds2D().contains(x, y) )
                {
                    for(int i = 0; i < PW.xpoints.length; i++)
                        PW.xpoints[i] +=dx;
                    for(int i = 0; i < PW.ypoints.length; i++)
                        PW.ypoints[i] += dy;

                    for(int i = 0; i < PR.xpoints.length; i++)
                        PR.xpoints[i] += dx;
                    for(int i = 0; i < PR.ypoints.length; i++)
                        PR.ypoints[i] += dy;
                    repaint();
                }
                if(PW1.getBounds2D().contains(x, y) || PR1.getBounds2D().contains(x, y) )
                {
                    for(int i = 0; i < PW1.xpoints.length; i++)
                        PW1.xpoints[i] +=dx;
                    for(int i = 0; i < PW1.ypoints.length; i++)
                        PW1.ypoints[i] += dy;

                    for(int i = 0; i < PR1.xpoints.length; i++)
                        PR1.xpoints[i] += dx;
                    for(int i = 0; i < PR1.ypoints.length; i++)
                        PR1.ypoints[i] += dy;
                    repaint();  
                }
                      x += dx;
                      y += dy;
  }
      }
  class ScaleHandler implements MouseWheelListener {
    public void mouseWheelMoved(MouseWheelEvent e) {
          int  x=e.getX();
          int y=e.getY();
      if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
        if (myRect.getBounds2D().contains(x, y)) {
          float amount = e.getWheelRotation() * 5f;
          myRect.width += amount;
          myRect.height += amount;
          repaint();
        }
      }
    }
     public void mouseWheelMoved1(MouseWheelEvent e)
     {
          int x=e.getX();
          int y=e.getY();
         if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
      {
                 if(myRect1.getBounds2D().contains(x, y))
        {
            float amount=e.getWheelRotation()*5f;
            myRect1.width+=amount;
            myRect1.height+=amount;
            repaint();
        } 
      }
     }
  }
    @Override
    public void init() {
       setSize(854,550);
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
   public void update(Graphics g)
   {
       if(offscreen==null)
       {
           offscreen=createImage(this.getSize().width,this.getSize().height);
           h=offscreen.getGraphics();
       }
       h.setColor(getBackground());
       h.fillRect(0, 0, this.getSize().width, this.getSize().height);
       g.drawImage(offscreen, 0, 0, this);
       this.repaint();
   }
   @Override
  public void paint(Graphics g) {
      super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    Rectangle originalClipBounds = g2d.getClipBounds();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
       RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
     g2d.setColor(Color.red);
    g2d.fill(myRect);
    g2d.fill(myRect1);
    g2d.setColor(Color.white);
    g2d.fill(myRect2);
    g2d.fill(myRect3);
    g2d.setColor(Color.red);
    g2d.fill(PR);
    g2d.fill(PR1);
    g2d.setColor(Color.white);
    g2d.fill(PW);
    g2d.fill(PW1); 
  }
}
Posted
Updated 15-Sep-14 9:20am
v3
Comments
mohammad_tahsiri 15-Sep-14 14:55pm    
also how can i make the two objects that are painted red and white, move correctly?

1 solution

Flickering is usually removed using the double buffering technique[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Sep-14 21:19pm    
True, a 5.
—SA
CPallini 16-Sep-14 3:14am    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900