1 Replies - 660 Views - Last Post: 24 April 2010 - 09:31 AM Rate Topic: -----

#1 ballerplaya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 05-April 10

Rectangle Drawing Flicker

Posted 24 April 2010 - 06:32 AM

I am working on a custom menu system for an applet based game.

Here is my full code:

Client.java:


package david;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class Client extends Applet implements MouseMotionListener,
		MouseListener {

	public final int AppWidth = 640;
	public final int AppHeight = 480;

	public static int mouseX;
	public static int mouseY;

	public Robot robot;

	public Color menuBack = new Color(0.0f, 0.0f, 1.0f, 0.25f); // R,G,B,Alpha
	public Color highlighter = new Color(0.0f, 0.0f, 1.0f, 0.38f); // R,G,B,Alpha

	public Rectangle MouseLoc;
	public Rectangle MenuItem1Rect;
	public Rectangle MenuItem2Rect;
	public Rectangle MenuItem3Rect;

	public Image Back;

	public String message = "Message";

	public void init() {
		// Init variables
		this.setSize(AppWidth, AppHeight);
		this.setBackground(Color.black);
		this.setMaximumSize(new Dimension(AppWidth, AppHeight));
		this.requestFocus();
		this.addMouseMotionListener(this);
		this.addMouseListener(this);

		MenuItem1Rect = new Rectangle(220, 130, 200, 30);
		MenuItem2Rect = new Rectangle(220, 161, 200, 30);
		MenuItem3Rect = new Rectangle(220, 192, 200, 30);

		// URL BackUrl =
		// this.getClass().getResource("/david/resources/back.png");
		Back = getImage(getDocumentBase(), "david/resources/back.png");
		// Back = (new ImageIcon(BackUrl)).getImage();

	}

	public void paint(Graphics g) {

		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		g2.drawImage(Back, 0, 0, null);

		g2.setColor(Color.blue);
		g2.drawRect(220, 80, 200, 260);

		g2.setColor(this.menuBack);
		g2.fillRect(221, 81, 199, 259);

		g2.setColor(Color.white);
		g2.setFont(new Font("Times New Roman", Font.BOLD, 26));
		g2.drawString("Main Menu", 255, 110);

		g2.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		g2.drawString("Login", 305, 150);
		g2.drawString("Exit", 305, 180);
		g2.drawString("About", 305, 210);

		g2.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		g2.setColor(Color.green);
		g2.drawString("Mouse Loc: " + mouseX + "," + mouseY, 5, 15);
		g2.drawString(message, 5, 30);

	}

	public void mouseDragged(MouseEvent arg0) {

	}

	public void mouseMoved(MouseEvent e) {
		mouseX = e.getX();
		mouseY = e.getY();
		MouseLoc = new Rectangle(mouseX, mouseY, 2, 2);

		Graphics gg = this.getGraphics();
		Collision(MouseLoc, MenuItem1Rect, gg);
		Collision(MouseLoc, MenuItem2Rect, gg);
		Collision(MouseLoc, MenuItem3Rect, gg);

	}

	public void Collision(Rectangle mouseRect, Rectangle collider, Graphics g) {
		g.setColor(highlighter);
		if (mouseRect.intersects(collider)) {

			g.fillRect((int) collider.getX(), (int) collider.getY(),
					(int) collider.getWidth(), (int) collider.getHeight());

			repaint();

		}

	}

	public boolean IsCollision(Rectangle mouseRect, Rectangle collider) {

		if (mouseRect.intersects(collider)) {
			return true;

		} else {
			return false;
		}
	}

	public void update(Graphics g) {
		Graphics gc;
		Image offscreen = null;
		
		offscreen = createImage(AppWidth, AppHeight);
		gc = offscreen.getGraphics();
		gc.setColor(getBackground());
		gc.fillRect(0, 0, AppWidth, AppHeight);
		gc.setColor(getForeground());

		paint(gc);
		
		g.drawImage(offscreen, 0, 0, this);

	}

	@Override
	public void mouseClicked(MouseEvent e) {

		mouseX = e.getX();
		mouseY = e.getY();
		MouseLoc = new Rectangle(mouseX, mouseY, 5, 10);

		if (IsCollision(MouseLoc, MenuItem1Rect) == true) {
			message = "You Clicked on the first menu Item!";
		} else

		if (IsCollision(MouseLoc, MenuItem2Rect) == true) {
			message = "You Clicked on the second menu Item!";

		} else

		if (IsCollision(MouseLoc, MenuItem3Rect) == true) {
			message = "You Clicked on the third menu Item!";

		}
		repaint();
	}

	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}
}





Here is the image file I use:(rename back.png)
http://www.ballerpla.../1272114463.png

Here is a live sample:
http://ballerplaya.info/menutest.php

My problem is that when I move the mouse while within the rectangle, it draws the rectangle section over and over. I want to draw a single rectangle when within the bounds of the three rectangles:

MenuItem1Rect = new Rectangle(220, 130, 200, 30);
		MenuItem2Rect = new Rectangle(220, 161, 200, 30);
		MenuItem3Rect = new Rectangle(220, 192, 200, 30);


So basically I need help only drawing a single rectangle while I am within the bounds of each rectangle. I have tried a multitude of things which ultimately resulted in failure.


Thank you in advanced,
Dave.

Is This A Good Question/Topic? 0
  • +

Replies To: Rectangle Drawing Flicker

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Rectangle Drawing Flicker

Posted 24 April 2010 - 09:31 AM

pbl's tutorial on getting rid of repaint() flickering
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1