In my java applet I have an image following the cursor around the content pane. I also set a background image. When I move the cursor I am able to see the cursor image but the background flickers rather badly however when I stop moving the cursor the background images stops flickering and the cursor image dissapears behind it. How can I always have the cursor image on top of the background without the background image flickering.
Here is my code:
public class ShootingGallery extends JApplet implements MouseListener, MouseMotionListener {
//VARIABLES
int mouseXPos;
int mouseYPos;
Image myImage;
Image gameBackground;
#Override
public void init() {
//SET UP PAGE
setSize(800, 600);
gameBackground = Toolkit.getDefaultToolkit().getImage("bikiniBottom.JPG");
//ADD ACTION LISTENERS
addMouseMotionListener(this);
addMouseListener(this);
this.add(new PaintContainer());
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(gameBackground, 0, 0, this);
}
#Override
public void update(Graphics g) {
paint(g);
}
#Override
public void mouseMoved(MouseEvent e) {
moveMouse(e);
}
public void moveMouse(MouseEvent e) {
Graphics g = getGraphics();
//SET UP CURSOR IMAGE
myImage = getImage(getDocumentBase(), "spongebob.gif");
mouseXPos = e.getX() - (myImage.getWidth(null) / 2);
mouseYPos = e.getY() - (myImage.getHeight(null) / 2);
System.out.println(mouseXPos + " ," + mouseYPos);
repaint();
}
public class PaintContainer extends JPanel {
#Override
protected void paintComponent(Graphics g) {
g.drawImage(myImage, mouseXPos, mouseYPos, this);
}
}
}
Thanks in advance
You should use Double Buffering to avoid the flickering.
class DoubleBufferedCanvas extends Canvas {
public void update(Graphics g) {
Graphics offgc;
Image offscreen = null;
Dimension d = size();
// create the offscreen buffer and associated Graphics
offscreen = createImage(d.width, d.height);
offgc = offscreen.getGraphics();
// clear the exposed area
offgc.setColor(getBackground());
offgc.fillRect(0, 0, d.width, d.height);
offgc.setColor(getForeground());
// do normal redraw
paint(offgc);
// transfer offscreen to window
g.drawImage(offscreen, 0, 0, this);
}
}
Tried using paintComponent instead of paint? (replace paint with paintComponent). Also, read about double buffering and why you need it: http://docs.oracle.com/javase/tutorial/extra/fullscreen/doublebuf.html
Don't override paint() and don't override update(). That is the old way to do custom painting when using AWT. This is Swing and that is NOT the way to do this.
Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent). Then you add the panel to the applet. Read the Swing tutorial o Custom Painting for an example and explanation.
Here is a paint application where u can draw image through color brushes
It is on white background
/* Arpana */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A simple applet where the user can sketch curves in a variety of
* colors. A color palette is shown along the right edge of the applet.
* The user can select a drawing color by clicking on a color in the
* palette. Under the colors is a "Clear button" that the user
* can press to clear the sketch. The user draws by clicking and
* dragging in a large white area that occupies most of the applet.
* The user's drawing is not persistent. It is lost whenever
* the applet is repainted for any reason.
* <p>The drawing that is done in this example violates the rule
* that all drawing should be done in the paintComponent() method.
* Although it works, it is NOT good style.
* <p>This class also contains a main program, and it can be run as
* a stand-alone application that has exactly the same functionality
* as the applet.
*/
public class SimplePaint extends JApplet {
/**
* The main routine opens a window that displays a drawing area
* and color palette. This main routine allows this class to
* be run as a stand-alone application as well as as an applet.
* The main routine has nothing to do with the function of this
* class as an applet.
*/
public static void main(String[] args) {
JFrame window = new JFrame("Simple Paint");
SimplePaintPanel content = new SimplePaintPanel();
window.setContentPane(content);
window.setSize(600,480);
window.setLocation(100,100);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
/**
* The init method of the applet simply creates a SimplePaintPanel and
* uses it as the content pane of the applet. All the work is done
* by the SimplePaintPanel.
*/
public void init() {
setContentPane( new SimplePaintPanel() );
}
/**
* A simple paint panel contains a large white drawing surface where
* the user can draw curves and a color palette that the user can click
* to select the color to be used for drawing. When this class is used
* as an applet, the content pane of the applet is a SimplePaintPanel.
* When this class is run as a standalone application, the content pane
* is a SimplePaintPanel. All the real work is done in the
* SimplePaintPanel class.
*/
public static class SimplePaintPanel extends JPanel
implements MouseListener, MouseMotionListener {
/**
* Some constants to represent the color selected by the user.
*/
private final static int BLACK = 0,
RED = 1,
GREEN = 2,
BLUE = 3,
CYAN = 4,
MAGENTA = 5,
YELLOW = 6;
private int currentColor = BLACK; // The currently selected drawing color,
// coded as one of the above constants.
/* The following variables are used when the user is sketching a
curve while dragging a mouse. */
private int prevX, prevY; // The previous location of the mouse.
private boolean dragging; // This is set to true while the user is drawing.
private Graphics graphicsForDrawing; // A graphics context for the panel
// that is used to draw the user's curve.
/**
* Constructor for SimplePaintPanel class sets the background color to be
* white and sets it to listen for mouse events on itself.
*/
SimplePaintPanel() {
setBackground(Color.WHITE);
addMouseListener(this);
addMouseMotionListener(this);
}
/**
* Draw the contents of the panel. Since no information is
* saved about what the user has drawn, the user's drawing
* is erased whenever this routine is called.
*/
public void paintComponent(Graphics g) {
super.paintComponent(g); // Fill with background color (white).
int width = getWidth(); // Width of the panel.
int height = getHeight(); // Height of the panel.
int colorSpacing = (height - 56) / 7;
// Distance between the top of one colored rectangle in the palette
// and the top of the rectangle below it. The height of the
// rectangle will be colorSpacing - 3. There are 7 colored rectangles,
// so the available space is divided by 7. The available space allows
// for the gray border and the 50-by-50 CLEAR button.
/* Draw a 3-pixel border around the applet in gray. This has to be
done by drawing three rectangles of different sizes. */
g.setColor(Color.GRAY);
g.drawRect(0, 0, width-1, height-1);
g.drawRect(1, 1, width-3, height-3);
g.drawRect(2, 2, width-5, height-5);
/* Draw a 56-pixel wide gray rectangle along the right edge of the applet.
The color palette and Clear button will be drawn on top of this.
(This covers some of the same area as the border I just drew. */
g.fillRect(width - 56, 0, 56, height);
/* Draw the "Clear button" as a 50-by-50 white rectangle in the lower right
corner of the applet, allowing for a 3-pixel border. */
g.setColor(Color.WHITE);
g.fillRect(width-53, height-53, 50, 50);
g.setColor(Color.BLACK);
g.drawRect(width-53, height-53, 49, 49);
g.drawString("CLEAR", width-48, height-23);
/* Draw the seven color rectangles. */
g.setColor(Color.BLACK);
g.fillRect(width-53, 3 + 0*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.RED);
g.fillRect(width-53, 3 + 1*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.GREEN);
g.fillRect(width-53, 3 + 2*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.BLUE);
g.fillRect(width-53, 3 + 3*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.CYAN);
g.fillRect(width-53, 3 + 4*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.MAGENTA);
g.fillRect(width-53, 3 + 5*colorSpacing, 50, colorSpacing-3);
g.setColor(Color.YELLOW);
g.fillRect(width-53, 3 + 6*colorSpacing, 50, colorSpacing-3);
/* Draw a 2-pixel white border around the color rectangle
of the current drawing color. */
g.setColor(Color.WHITE);
g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing);
g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2);
} // end paintComponent()
/**
* Change the drawing color after the user has clicked the
* mouse on the color palette at a point with y-coordinate y.
* (Note that I can't just call repaint and redraw the whole
* panel, since that would erase the user's drawing!)
*/
private void changeColor(int y) {
int width = getWidth(); // Width of applet.
int height = getHeight(); // Height of applet.
int colorSpacing = (height - 56) / 7; // Space for one color rectangle.
int newColor = y / colorSpacing; // Which color number was clicked?
if (newColor < 0 || newColor > 6) // Make sure the color number is valid.
return;
/* Remove the hilite from the current color, by drawing over it in gray.
Then change the current drawing color and draw a hilite around the
new drawing color. */
Graphics g = getGraphics();
g.setColor(Color.GRAY);
g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing);
g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2);
currentColor = newColor;
g.setColor(Color.WHITE);
g.drawRect(width-55, 1 + currentColor*colorSpacing, 53, colorSpacing);
g.drawRect(width-54, 2 + currentColor*colorSpacing, 51, colorSpacing-2);
g.dispose();
} // end changeColor()
/**
* This routine is called in mousePressed when the user clicks on the drawing area.
* It sets up the graphics context, graphicsForDrawing, to be used to draw the user's
* sketch in the current color.
*/
private void setUpDrawingGraphics() {
graphicsForDrawing = getGraphics();
switch (currentColor) {
case BLACK:
graphicsForDrawing.setColor(Color.BLACK);
break;
case RED:
graphicsForDrawing.setColor(Color.RED);
break;
case GREEN:
graphicsForDrawing.setColor(Color.GREEN);
break;
case BLUE:
graphicsForDrawing.setColor(Color.BLUE);
break;
case CYAN:
graphicsForDrawing.setColor(Color.CYAN);
break;
case MAGENTA:
graphicsForDrawing.setColor(Color.MAGENTA);
break;
case YELLOW:
graphicsForDrawing.setColor(Color.YELLOW);
break;
}
} // end setUpDrawingGraphics()
/**
* This is called when the user presses the mouse anywhere in the applet.
* There are three possible responses, depending on where the user clicked:
* Change the current color, clear the drawing, or start drawing a curve.
* (Or do nothing if user clicks on the border.)
*/
public void mousePressed(MouseEvent evt) {
int x = evt.getX(); // x-coordinate where the user clicked.
int y = evt.getY(); // y-coordinate where the user clicked.
int width = getWidth(); // Width of the panel.
int height = getHeight(); // Height of the panel.
if (dragging == true) // Ignore mouse presses that occur
return; // when user is already drawing a curve.
// (This can happen if the user presses
// two mouse buttons at the same time.)
if (x > width - 53) {
// User clicked to the right of the drawing area.
// This click is either on the clear button or
// on the color palette.
if (y > height - 53)
repaint(); // Clicked on "CLEAR button".
else
changeColor(y); // Clicked on the color palette.
}
else if (x > 3 && x < width - 56 && y > 3 && y < height - 3) {
// The user has clicked on the white drawing area.
// Start drawing a curve from the point (x,y).
prevX = x;
prevY = y;
dragging = true;
setUpDrawingGraphics();
}
} // end mousePressed()
/**
* Called whenever the user releases the mouse button. If the user was drawing
* a curve, the curve is done, so we should set drawing to false and get rid of
* the graphics context that we created to use during the drawing.
*/
public void mouseReleased(MouseEvent evt) {
if (dragging == false)
return; // Nothing to do because the user isn't drawing.
dragging = false;
graphicsForDrawing.dispose();
graphicsForDrawing = null;
}
/**
* Called whenever the user moves the mouse while a mouse button is held down.
* If the user is drawing, draw a line segment from the previous mouse location
* to the current mouse location, and set up prevX and prevY for the next call.
* Note that in case the user drags outside of the drawing area, the values of
* x and y are "clamped" to lie within this area. This avoids drawing on the color
* palette or clear button.
*/
public void mouseDragged(MouseEvent evt) {
if (dragging == false)
return; // Nothing to do because the user isn't drawing.
int x = evt.getX(); // x-coordinate of mouse.
int y = evt.getY(); // y-coordinate of mouse.
if (x < 3) // Adjust the value of x,
x = 3; // to make sure it's in
if (x > getWidth() - 57) // the drawing area.
x = getWidth() - 57;
if (y < 3) // Adjust the value of y,
y = 3; // to make sure it's in
if (y > getHeight() - 4) // the drawing area.
y = getHeight() - 4;
graphicsForDrawing.drawLine(prevX, prevY, x, y); // Draw the line.
prevX = x; // Get ready for the next line segment in the curve.
prevY = y;
} // end mouseDragged()
public void mouseEntered(MouseEvent evt) { } // Some empty routines.
public void mouseExited(MouseEvent evt) { } // (Required by the MouseListener
public void mouseClicked(MouseEvent evt) { } // and MouseMotionListener
public void mouseMoved(MouseEvent evt) { } // interfaces).
} // End class SimplePaintPanel
} // end class SimplePaint
Related
I am trying to teach myself more about graphics in Java. To do this I'm trying to build a chess game. I've hit my first roadblock at making the board. My thought here is that I would have a extension of JComponent called "Square" that would be my container for both the color of the board square and the piece on that square (if any). To start with I haven't attempted to include any representation of the piece yet, just the square colors. Later on I hope to have an abstract "Pieces" class that is extended by multiple subclasses representing all the different types of pieces, and add those to each Square as applicable.
When I execute the following, I only get one black square in the upper left hand corner.
ChessBoardTest.java
public class ChessBoardTest {
public static void main(String[] args) {
ChessBoard Board = new ChessBoard();
Board.Display();
}
}
ChessBoard.java
public class ChessBoard extends JFrame {
public static final int FRAME_WIDTH = 500;
public static final int FRAME_HEIGHT = 500;
// Declare instance variables
private Square[][] square = new Square[rows][cols];
private final static int rows = 8;
private final static int cols = 8;
public ChessBoard() {
}
public void Display() {
JPanel Board_Layout = new JPanel();
Board_Layout.setLayout(new GridLayout(8,8));
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if((i+j) % 2 == 0) {
square[i][j] = new Square(1);
Board_Layout.add(square[i][j]);
} else {
square[i][j] = new Square(0);
Board_Layout.add(square[i][j]);
}
}
}
setTitle("Chess Mod");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(Board_Layout);
setVisible(true);
}
public void messageBox(String pMessage) {
JOptionPane.showMessageDialog(this, pMessage, "Message", JOptionPane.PLAIN_MESSAGE);
}
}
Square.java
public class Square extends JComponent {
private int color;
public Square(int c) {
this.color=c;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.color == 1) {
g.setColor(new Color(0,0,0));
} else {
g.setColor(new Color(255,255,255));
}
g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
}
}
I only get one black square in the upper left hand corner.
That's mainly because of the following call:
g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
getX() returns the horizontal pixel offset/location of the Component which is invoked upon, relative to the Container that contains that Component. getY() accordingly returns the vertical pixel offset/location of the Component which is invoked upon, relative to the Container that contains the Component.
getWidth() and getHeight() return the size of the Component.
So imagine that the Component at row with index 2 and column with index 3, will have its coordinates at about x == 3 * w / 8 and y == 2 * h / 8 where w and h is the size (width and height respectively) of the parent Container (ie the Board_Layout panel). Let's assume that Board_Layout has a size of 300x300 when you show the graphical user interface... This means that the Square at the location I mentioned will only paint the region which starts at x == 112 and y == 75 and expands at one 8th of the width (and height) of Board_Layout (because there are 8 rows and 8 columns in the grid). But the size of the Square itself is also at one 8th of the width (and height) of Board_Layout, ie about 37x37. So the painted region which starts and expands from the location 112,75 will not be shown at all (because it lies completely outside the Square's size).
Only the top left Square will have some paint on it because its bounds in the parent happen to intersect the drawn region.
To fix this, the location given at the Graphics object should be relative to each Square and not its parent Board_Layout. For example:
g.fillRect(0, 0, getWidth(), getHeight());
I'm a noob programmer, and I'm working on a little project that involves a 2D grid of 1000 x 1000 boolean values that change based on an instruction pattern. "After x instructions, how many values in the grid are true?" That kind of thing.
I want to put a little spin on it and render the values as a grid of pixels which are black if their corresponding values are false and white if they're true and that animates in real time as instructions are processed, but I'm pretty lost -- I've never dabbled with 2D graphics in Java. I've read through Oracle's tutorial, which helped, but the way I'm doing things is sufficiently different from its demo that I still feel lost.
My most immediate problem is that I can't even seem to initialize a grid of 1000 x 1000 black pixels using a BufferedImage. Running my code yields a very tiny window with nothing (grayness) in it. Can anyone tell me what I'm doing wrong and suggest how to proceed? My code follows:
import java.awt.image.BufferedImage;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PixelGrid extends JPanel {
private BufferedImage grid;
// Ctor initializing a grid of binary (black or white) pixels
public PixelGrid(int width, int height) {
grid = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
}
/**
* Fill an area with a given color
* #param color 0 for black; 1 for white
* #param x1 Starting x coordinate
* #param y1 Starting y coordinate
* #param x2 Ending x coordinate
* #param y2 Ending y coordinate
*/
public void toggleBlock(int color, int x1, int y1, int x2, int y2) {
if (color == 0) {
color = Color.BLACK.getRGB();
}
else {
color = Color.WHITE.getRGB();
}
for (int x = x1; x <= x2; x++) {
for (int y = y1; y <= y2; y++) {
grid.setRGB(x, y, color);
}
}
}
// Clear the grid (set all pixels to black)
public void clear() {
toggleBlock(0, 0, 0, grid.getWidth() - 1, grid.getHeight() - 1);
}
public static void main(String[] args) {
int width = 1000;
int height = 1000;
PixelGrid aGrid = new PixelGrid(width, height);
JFrame window = new JFrame("A Wild Pixel Grid Appears!");
window.add(aGrid); // Incorporates the pixel grid into the window
window.pack(); // Resizes the window to fit its content
window.setVisible(true); // Makes the window visible
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Note that it doesn't yet deal at all with an actual 2D array of booleans or instruction processing; I'm pretty sure I can handle that on my own, but, for now, I'm just trying to understand how to set up the graphical component.
Your code creates a BufferedImage, but then doesn't do anything with it (graphically). A few options:
Option 1: Override paintComponent of the PixelGrid class and draw the image to the JPanel
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(grid, 0, 0, this);
}
Option 2: Use a JLabel and ImageIcon
JLabel label = new JLabel(new ImageIcon(grid));
add(label);
In either case, you will have to call repaint on the Component every time the BufferedImage is changed
//some code
grid.setRGB(x, y, color);
//some more code
repaint();//or label.repaint() if Option 2
Hey Guys I have succesfully made a GUI in java that will scale polygons and circles using a slider. Everything works but I was wondering if there is a way to change the Origin point(Where it scales from). Right now it scales from the corner and I would like it to scale from the middle so I can start it in the middle and it scales out evenly. Also, If anyone could tell me an easy way to replace the Rectangle I have with an Image of some kind so you can scale the Picture up and down would be great! Thank you! Here is my code:
import javax.swing.*;
public class Fred
{
public static void main(String[] args)
{
TheWindow w = new TheWindow();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //X wont close the window with out this line
w.setSize(375,375);
w.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class TheWindow extends JFrame
{
private JSlider slider; //declare slider
private drawRect myPanel; //declare/ create panel
public TheWindow()
{
super("Slider Example"); //make title
myPanel = new drawRect();
myPanel.setBackground(Color.green); //change background color
slider = new JSlider(SwingConstants.VERTICAL, 0, 315, 10);// restrains the slider from scaling square to 0-300 pixels
slider.setMajorTickSpacing(20); //will set tick marks every 10 pixels
slider.setPaintTicks(true); //this actually paints the ticks on the screen
slider.addChangeListener
(
new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
myPanel.setD(slider.getValue()); //Wherever you set the slider, it will pass that value and that will paint on the screen
}
}
);
add(slider, BorderLayout.WEST); //similar to init method, adds slider and panel to GUI
add(myPanel, BorderLayout.CENTER);
}
}
import java.awt.*;
import javax.swing.*;
public class drawRect extends JPanel
{
private int d = 25; //this determines the beginning length of the rect.
public void paintComponent(Graphics g)//paints circle on the screen
{
super.paintComponent(g); //prepares graphic object for drawing
g.fillRect(15,15, d, d); //paints rectangle on screen
//x , y, width, height
}
public void setD(int newD)
{
d = (newD >= 0 ? newD : 10); //if number is less than zero it will use 10 for diameter(compressed if statement)
repaint();
}
public Dimension getPrefferedSize()
{
return new Dimension(200, 200);
}
public Dimension getMinimumSize()
{
return getPrefferedSize();
}
}
Changing the "origin point" so it becomes the center of the "zoom" is basically just the process of subtract half of d from the center point.
So, assuming the the center point is 28 ((25 / 2) + 15), you would simply then subtract d / 2 (25 / 2) from this point, 28 - (25 / 2) = 15 or near enough...
I modified the paintComponent method for testing, so the rectangle is always at the center of the panel, but you can supply arbitrary values in place of the originX and originY
#Override
public void paintComponent(Graphics g)//paints circle on the screen
{
super.paintComponent(g); //prepares graphic object for drawing
int originX = getWidth() / 2;
int originY = getHeight() / 2;
int x = originX - (d / 2);
int y = originY - (d / 2);
System.out.println(x + "x" + y);
g.fillRect(x, y, d, d); //paints rectangle on screen
//x , y, width, height
}
As for scaling an image, you should look at Graphics#drawImage(Image img, int x, int y, int width, int height, ImageObserver observer), beware though, this will scaling the image to the absolute size, it won't keep the image ratio.
A better solution might be to use a double value of between 0 and 1 and multiple the various elements by this value to get the absolute values you want
im trying to put restrictions on movement if my chicken reaches the corner here is my code for my move method
public void move1(){
if(((x<=350) && (y>=350)) || ((x>=450) && (y>=350))){
if(dy<0){
dy=0;
}
}
if(((x>=350) && (y<=350)) || ((x<=450) && (y<=350))){
if(dx<0 || dx>0){
dx=0;
}
}
if(((y<=250) && (x<=350)) || ((y<=250) && (x>=450))){
if(dy>0){
dy=0;
}
}
if (x >= 750){
x = 750;
}
if (y >= 575){
y = 575;
}
x += dx;y += dy;
}
i used a key adapter for the movement of my chicken but its from a different class
public void keyPressed(KeyEvent e) {
chick.keyPressed(e);
}
the animation is perfectly fine but my chicken wont respond the keyevent
I would strongly recommend you to have a look at KeyBindings, they are more accustomed to such situations, which are more concerned about focus related issues. This post regarding Motion Using the Keyboard, might will surely interest you, on the topic concern :-)
EDIT :
Here a small program from help :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyBindingExample
{
private DrawingBoard contentPane;
/*
* There variables will simply
* decide how much the square
* will move with click key press,
* in this case I have set this to
* 1 (inside the constructor).
* brakes will simply tell whether
* the square will move or not in
* a given direction.
*/
private int speed;
private int brakes;
public KeyBindingExample() {
speed = 5;
brakes = 0;
}
private void displayGUI()
{
JFrame frame = new JFrame("Swing Worker Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new DrawingBoard(10, 10, Color.BLUE.darker());
addBindingsToBoard();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addBindingsToBoard() {
/*
* Since, when UP Arrow is pressed, that means, the square
* can move vertically upwards, hence, the square will move
* along Y-Axis that too in the negative direction of the
* same, though along X-Axis the square will move nowhere,
* hence, we passing 0 and -1, since we want to add the
* current location (say square is at present at 50, 50),
* now after UP Arrow key event, square will be at (50, 49);
*/
putBindingsFor(contentPane, KeyStroke.getKeyStroke("UP"),
"UP Arrow Key", brakes, -speed);
/*
* When RIGHT Arrow is pressed, the square is suppose to
* move horizontally, along the X-Axis, in the positive
* direction towards the RIGHT. Hence +1 change along X-Axis
* and no change along Y-Axis, i.e. from (50, 49), the square
* will now move to (51, 49), that's why we passing (+1, 0)
*/
putBindingsFor(contentPane, KeyStroke.getKeyStroke("RIGHT"),
"RIGHT Arrow Key", speed, brakes);
/*
* When DOWN Arrow is pressed, the square is suppose to
* move vertically, along the Y-Axis, in the positive
* direction towards the BOTTOM. Hence no change along X-Axis
* and +1 change along Y-Axis, i.e. from (51, 49), the square
* will now move to (51, 50), that's why we passing (0, +1)
*/
putBindingsFor(contentPane, KeyStroke.getKeyStroke("DOWN"),
"DOWN Arrow Key", brakes, +speed);
/*
* When LEFT Arrow is pressed, the square is suppose to
* move horizontally, along the X-Axis, in the negative
* direction towards the LEFT side. Hence -1 change along X-Axis
* and no change along Y-Axis, i.e. from (51, 50), the square
* will now move to (50, 50), that's why we passing (-1, 0).
* The square will atlast come to it's initial position.
*/
putBindingsFor(contentPane, KeyStroke.getKeyStroke("LEFT"),
"LEFT Arrow Key", -speed, brakes);
}
private void putBindingsFor(JComponent comp,
KeyStroke keyStroke, String value, final int moveX, final int moveY) {
comp.getInputMap().put(keyStroke, value);
comp.getActionMap().put(value, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
contentPane.setValues(moveX, moveY);
}
});
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
#Override
public void run()
{
new KeyBindingExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class DrawingBoard extends JPanel {
private int x;
private int y;
private int width;
private int height;
private Color rectColor;
public DrawingBoard(int x, int y, Color rColor) {
setOpaque(true);
this.x = x;
this.y = y;
rectColor = rColor;
width = height = 10;
}
public void setValues(int deltaX, int deltaY) {
System.out.format("Firstly X : %d\tY : %d%n", x, y);
repaint(x, y, width, height);
/*
* Whatever values are passed from above, i.e.
* say on Left ARROW, we passing (-1, 0),
* therefore deltaX will be -1 and deltaY will
* be 0. Now whatever the current value of X is
* we are simply adding deltaX to that value
* and the same goes for deltaY as well.
* Now since the value for x and y is updated
* after these two statements below, we checking
* that whether these two updated values lies
* within the bounds of our board or not.
* If they are, then we simply calling repaint,
* to draw the square at this new location, else
* we simply bring back the previous values of
* x and y to their previous state.
*/
x += deltaX;
y += deltaY;
if ((x + width) <= getWidth() && (y + height) <= getHeight()
&& x >= 0 && y >= 0) {
System.out.format("Later X : %d\tY : %d%n", x, y);
repaint(x, y, width, height);
}
else {
x -= deltaX;
y -= deltaY;
System.out.format("Later X : %d\tY : %d%n", x, y);
repaint(x, y, width, height);
}
}
/*
* Make this a customary habbit of overridding
* this method whenever you have to override
* any JComponent, instead of calling
* setPreferredSize(...).
*/
#Override
public Dimension getPreferredSize() {
return (new Dimension(100, 100));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(rectColor);
g.fillRect(x, y, width, height);
}
}
Hello all I have this problem that I can't seem to fix. I've been given some code and have to make a "tic tac toe" game. Fairly primitive. At the moment what it want's me to do is take user input (it just asks for what row / column you want to place the marker) and it is meant to draw an oval on the appropriate square of the board. My current code is as following the work has specified that I make a new class to deal with user input.
I am currently just mucking around with trying to get it to add new items to the JFrame but am having little success. I have a dummy call for input, it doesn't check to see what I've typed it just calls an oval that SHOULD sit in the first square in the top left corner. I can get the object to draw onto the JFrame (albeit it takes up the whole frame) but it is always BEHIND the actual board (ie: if I stretch the frame I can see the circle). I've tried adding JPanels and so forth so that they sit on top of the board but so far I am having little luck.
Here is the code for creating the Oval which I was given for the task. All I am doing is instantiating a new oval with position (0,0,10,10). When it draws however it takes up the WHOLE JFrame but it is also BEHIND the actual board...... any ideas?
package lab4;
import javax.swing.*;
import java.awt.*;
/** Oval Supplier Class
* Author: David D. Riley
* Date: April, 2004
*/
public class Oval extends JComponent {
/** post: getX() == x and getY() == y
* and getWidth() == w and getHeight() == h
* and getBackground() == Color.black
*/
public Oval(int x, int y, int w, int h) {
super();
setBounds(x, y, w, h);
setBackground(Color.black);
}
/** post: this method draws a filled Oval
* and the upper left corner of the bounding rectangle is (getX(), getY())
* and the oval's dimensions are getWidth() and getHeight()
* and the oval's color is getBackground()
*/
public void paint(Graphics g) {
g.setColor( getBackground() );
g.fillOval(0, 0, getWidth()-1, getHeight()-1);
paintChildren(g);
}
}
EDIT: THIS IS NOW THE CODE WE ARE LOOKING AT--
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab4;
/**
*
* #author Scott
*/
import java.awt.*;
import java.util.Scanner;
import javax.swing.*;
public class GameBoard {
private JFrame win;
private int count = 1;
//Create new GUI layout
GridLayout layout = new GridLayout(3, 3);
JPanel panel = new JPanel(layout);
//Create a new Array of Rectangles
Rectangle[][] rect = new Rectangle[3][3];
public GameBoard() {
//Create new JFrame + Set Up Default Behaviour
win = new JFrame("Tic Tac Toe");
win.setBounds(0, 0, 195, 215);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setResizable(true);
//Loop goes through each line of the array. It creates a new rectangle
//determines it's colour based on modulus division
//Add's the rectangle to the JPanel.
for (int i = 0; i < rect.length; i++) {
for (int j = 0; j < rect[i].length; j++) {
rect[i][j] = new Rectangle(0, 0, 1, 1);
if (count % 2 != 0) {
rect[i][j].setBackground(Color.black);
} else {
rect[i][j].setBackground(Color.red);
}
panel.add(rect[i][j]);
count++;
}
}
//Sets the game to be visible.
win.add(panel);
win.setVisible(true);
//userInput();
}
private void userInput() {
Scanner scan = new Scanner(System.in);
}
}
Let's start with your oval class...
This is a VERY bad idea...
public void paint(Graphics g) {
// No super.paint(g) call, hello to a world of pain...
// Paint some stuff
g.setColor( getBackground() );
g.fillOval(0, 0, getWidth()-1, getHeight()-1);
// Then draw the children over it...???
paintChildren(g);
}
This is a better approach.
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor( getBackground() ); // Might want to pick a better color...
g.fillOval(0, 0, getWidth()-1, getHeight()-1);
}
At a guess, I suggest that your window is using a layout manager that is overriding your setBounds call
Answer ended up being that I need to stick with the flow layout, manually size the rectangles and use a JLayeredPane instead. Was not aware this existed, talked to my lecturer who said that my thought process was right and that was the way he intended for me to do it......what a pain but all done thanks to those that helped.