How would i make an object and rotate it?

ok, i'm trying to get java to make a graphics object, and then create an array of said objects (basically just a bunch of rectangles organized into rows/columns) with the end goal being that i be able to put this collection of rectangles into a JPanel and then move it around and rotate it without changing its dimensions (so each rectangle in the collection retains the same height and width but with a different placement/orientation angle )
My java textbook (yes, college student) doesn't cover the Graphics2D stuff which SEEMS to be what i would use... but beyond that I'm finding the resources on the topic to be uniformly opaque. i think i just need some starting point to extrapolate from. could anyone reply me a crash course in the above...?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Blocks extends JPanel {
    Block[][] blocks;
    int dx = 2;
    int dy = 2;
    public Blocks() {
        registerKeys();
        setFocusable(true);
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        if(blocks == null) initBlocks();
        g2.setPaint(Color.red);
        for(int j = 0; j < blocks.length; j++)
            for(int k = 0; k < blocks[j].length; k++)
                blocks[j][k].draw(g2);
    private void initBlocks() {
        int rows = 6;
        int cols = 3;
        Dimension d = new Dimension(200, 200);
        BlockMaker blockMaker = new BlockMaker(rows, cols, d);
        blocks = blockMaker.getBlocks();
    private void registerKeys() {
        getInputMap().put(KeyStroke.getKeyStroke("UP"), "UP");
        getActionMap().put("UP", up);
        getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "LEFT");
        getActionMap().put("LEFT", left);
        getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "DOWN");
        getActionMap().put("DOWN", down);
        getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "RIGHT");
        getActionMap().put("RIGHT", right);
    private void step(int x, int y) {
        for(int j = 0; j < blocks.length; j++)
            for(int k = 0; k < blocks[j].length; k++)
                blocks[j][k].move(x, y);
        repaint();
    private Action up = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            step(0, -dy);
    private Action left = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            step(-dx, 0);
    private Action down = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            step(0, dy);
    private Action right = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            step(dx, 0);
    public static void main(String[] args) {
        Blocks blocks = new Blocks();
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(blocks);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
class BlockMaker {
    Block[][] blocks;
    int PAD = 20;
    public BlockMaker(int rows, int cols, Dimension size) {
        int w = (size.width  - (cols+1)*PAD)/cols;
        int h = (size.height - (rows+1)*PAD)/rows;
        blocks = new Block[rows][cols];
        for(int j = 0; j < rows; j++) {
            int y = PAD + j*(h + PAD);
            for(int k = 0; k < cols; k++) {
                int x = PAD + k*(w + PAD);
                blocks[j][k] = new Block(x, y, w, h);
    public Block[][] getBlocks() {
        return blocks;
class Block {
    int x;
    int y;
    int w;
    int h;
    public Block(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    protected void draw(Graphics2D g2) {
        g2.drawRect(x, y, w, h);
    protected void move(int dx, int dy) {
        x += dx;
        y += dy;
}

Similar Messages

  • How do I get an object to rotate as it moves up and down or left and right from one time key to the next ?

    how do I get an object to rotate as it moves up and down or left and right from one time key to the next ?

    I'm partial to this series:
    After Effects basics tutorials by Andrew Devis
    The subject matter is very clearly-defined by topic.
    And AE shouldn't up & quit on you... but you haven't breathed a word about your system or your AE version.  Please share.

  • How do I make a 3-d rotating text in Flash cs4?

    Hi, Thunder BoomCat PowWow here.
    I'm a student in this Virtual Enterprise class and am in charge of learning flash and making a kick-butt commercial for our firm: Uncommon Ground.
    I was wanted to make some text rotate AROUND a sphere in flash (the sphere is going to be a picture of the earth).
    I want the text to go in one direction, wrapping around the sphere, and appearing on the other side like it went all the way around.
    Can anyone tell me how to do that using Flash?
    Thanks in advanced,
    TBCPW
    Sole employee of the media department

    flash can't make 3d objects.  cs5 can make 2d objects that move in apparent 3d space but that's the limit of flash'es capabilities.
    however, there are other programs that can be used to make 3d objects and then animate those objects in flash.

  • How would I make it so that when "deleting" an image I can make it so that the delete key does it or a button I put on the application?

    How would I make it so that when "deleting" an image I can make it so that the delete key does it or a button I put on the application?
    I want it so that I can use either a button on the designer window or the delete key. I know how to do the or part but the trouble I'm having is coding the button in.
    if (LastImageClicked != null && (e.Key == Key.Delete || Button))
    This is the code that I have in the format I'm looking for I just don't know how to do it. Thanks for your help!

    There are a number of things which are unclear about your question.
    I'll tell you one way to approach this though.
    Handle Window.PreviewKeyDown.
    <Window
    Window.PreviewKeyDown="Mainwindow_PreviewKeyDown"
    Code behind
    private void Mainwindow_PreviewKeyDown(object sender, KeyEventArgs e)
    if (e.Key == Key.Delete)
    File.Delete("p001.jpg");
    I don't know enough about what you mean by LastImageClicked  but you need some way of knowing which path you are going to delete.
    Then you might well have a problem if it's showing in an image control.
    You will need to copy the picture off disk into a new bitmapimage object.
    If you just do
    <Image Source="p001.jpg"
    Then that image will grab the file and you won't be able to delete it - you'll  get an error.
    In my experimental code p001.jpg is set as content copy always so it ends up next to the exe in the bin when the solution compiles.
    You would probably want a full path to a file there.
    Hope that helps.
    Recent Technet articles:
    Property List Editing;  
    Dynamic XAML

  • When I'm bouncing a project, it freezes or doesnt stop bouncing and keeps going past its cut off. how do i make it stop and get back to normal?

    When I'm bouncing a project, it freezes or doesnt stop bouncing and keeps going past its cut off. how do i make it stop and get back to normal?

    Hi
    chorleyman wrote:
    you do have to actually define the area to be bounced in the dialogue window that comes up - there are some discrepencies in the way that the program behaves like that,
    Apart from the "Include Audio Tail" function, which can leave Logic bouncing for a very, very long time, there are generally, no discrepancies regarding the Bounce Locators:
    If Cycle is enabled, the bounce locators will be set to the cycle locators.
    If Cycle is off, and regions are selected, the bounce locators will be set to the selection.
    If cycle is off, and no regions are selected, the bounce locators will be set from the Project Start to the project end.
    It is smart to actually check that the bounce locators are set where you would like them to be.
    wildfire31 wrote:
    When I'm bouncing a project, it freezes or doesnt stop bouncing and keeps going past its cut off. how do i make it stop
    If you get caught with a bounce that does not stop, Command . (period) should do the trick.
    CCT

  • Plz tell me how to create authority check objects and how to usein prg

    dear sir,
    plz tell me how to create authority check objects and how to usein prg

    http://help.sap.com/saphelp_46c/helpdata/en/5c/deaa74d3d411d3970a0000e82de14a/content.htm
    http://help.sap.com/saphelp_nw70/helpdata/en/52/6716a6439b11d1896f0000e8322d00/content.ht
    Create custom authorization – Customer specific object
    If you have requirements that cannot be met using the P_ORGIN and P_ORGXX authorization objects (for example, because you want to build your authorization checks on additional fields of the Organizational Assignment infotype (0001) that are customer-specific), you can include an authorization object in the authorization checks yourself.
    Create the authorization object using transaction SU21. Make sure you keep to the customer name range (Z/Y). To be able to use the new authorization object you have created in the master data authorization check, the object must contain the INFTY, SUBTY, and AUTHC fields. You can use any of the fields of the Organizational Assignment infotype (0001) for the other fields. You can also use customer-specific additional fields provided they are CHAR or NUMC type fields.
    After you have created the object, you must start the RPUACG00 report. This report overwrites the MPPAUTZZ standard include with the code that is needed to evaluate the authorization object you created. Note: Technically speaking, this involves a modification. However, SAP fully supports this procedure. And you should not have more maintenance work as a result of this modification.
              Note: that if you use customer-specific authorization objects, you must maintain these objects in transaction SU24 (Maintain Assignment of Authorization Objects to Transactions) in the same way as you maintain the authorization objects P_ORGIN, P_ORGXX, and P_PERNR
    AUTHORITY CHECK OBJECT Object_name
                ID fieldname1 FIELD fieldvalue1
                ID fieldname2 FIELD fieldvalue2
                ID fieldname3 FIELD fieldvalue3.
                 If sy-subrc eq 0.   "Authorization exists
                 Endif.
    http://articles.techrepublic.com.com/5100-6329_11-5110893.html
    Edited by: JackandJay on Jan 16, 2008 10:21 AM

  • Eraser tool in Photoshop CS4 acts as if it is transparent when on brush or pencil mode. Block acts like its not transparent at all but there are no options for opacity, flow or brush size. Whats wrong and how to I make the brush and pencil eraser erase co

    Eraser tool in Photoshop CS4 acts as if it is transparent when on brush or pencil mode. Block acts like its not transparent at all but there are no options for opacity, flow or brush size. Whats wrong and how to I make the brush and pencil eraser erase completely without having to go over it multiple times?
    It started randomly about two weeks ago and I thought it was a glitch that would just go away. But now I am getting sick of using only the block eraser on one size. Help?

    The easiest thing to start with is to reset the Eraser Tool.
    Right click on the Eraser icon in the tool options bar and then click on Reset Tool.

  • How can we make one object as synchronizable?

    Hi!
    I am new to java.How can we make one object as synchronizable?
    Thanx

    Synchronize is used to make an object and/or a functional sequence thread safe. By itself it does not make something thread safe.
    So the point is to make something thread safe not just to synchronize it.
    You can either sychronize a method like so....
          public synchronized void doit()
    Or you synchronize on an object (where 'Object' below is solely as an example, any object can be used)...
          Object lockObject = new Object();
          public void doit()
             sychronized (lockObject)

  • How can i make my object appear as a mesh?

    How can I make an object appear as a mesh? I dont want anything as complicated as a coloured gradient I just want a flat object to appear like a net. Is there an easy way to do this?
    Thanks
    J

    Jules,
    Depending on the shape, and upon the appearance (varying width and/or knotting or whatever), it may be anything from a Rectangular/Polar Grid (bundled with the Line Segment Tool) to something intricate and difficult.

  • How can 1 make an object of user defined class immutable?

    Hi All,
    How can one make an object of user defined class immutable?
    Whats the implementation logic with strings as immutable?
    Regards,

    Hi All,
    How can one make an object of user defined class
    immutable?The simple answer is you can't. That is, you can't make the object itself immutable, but what you can do is make a wrapper so that the client never sees the object to begin with.
    A classic example of a mutable class:
    class MutableX {
        private String name = "None";
        public String getName() {
            return name;
        public void setName(String name) {
            this.name = name;
    }I don't think it's possible to make this immutable, but you can create a wrapper that is:
    class ImmutableX {
        private final MutableX wrappedInstance;
        public ImmutableX (String name) {
            wrappedInstance = new MutableX();
            wrappedInstance.setName(name);
        public String getName() {
            return wrappedInstance.getName();
        // Don't give them a way to set the name and never expose wrappedInstance.
    }Of course, if you're asking how you can make your own class immutable then the simple answer is to not make any public or protected methods that can mutate it and don't expose any mutable members.
    Whats the implementation logic with strings as
    immutable?
    Regards,I don't understand the question.

  • How would you make an effect like this?

    Hi! I'm new to the forums, but hopefully it's ok if I start this new discussion. I'm very new to Photoshop as well (newbie).
    Here is the image: http://sffbookreview.files.wordpress.com/2012/10/life-of-pi-banner.png
    How would you make the image on the right - with the watercolor painting crop effect of the tiger? Also, how would you make the font on the left gradually turn to orange?
    Thanks! Your help is much appreciated.

    Hi there,
    Here's a really quick tutorial on how to add the mask and gradient text over the image of a tiger.
    Masking the Image
    1) Open your image of the tiger. There should be two layers in your layers panel: the image and a white background.
    2) With the tiger layer selected, click on the icon marked below in the layers panel to create a layer mask.
    3) Being sure you have the mask you just created selected, paint with black using the brush tool to mask out the image. In your layers panel, if you look at the mask icon, you will see the areas where you were painting (on the canvas) change to black. Use a variety of brushes to recreate the effect in your example image.
    Creating Text with a Gradient Effect
    1) Use the Type Tool to create your basic text and reposition it on the page with the Move Tool.
    2) In the layers panel, control click on the type layer and select Blending Options.
    3) In the dialog box that opens, check the Gradient Overlay box and click on the "gradient overlay" text to move to the gradient options. Adjust the angle of the gradient, then click on the colored bar to select the colors you want. Click OK on each dialog to close them.
    Final result:
    I hope this helps, and please let me know if you need any more assistance.

  • I have a used mac computer. I want to sync and update my iphone. Currently itunes tells me that my iphone is connected to a different itunes account. How do I make itunes sync and update my phone without deleting all the current information on my phone?

    I have a used mac computer. I want to sync and update my iphone. Currently itunes tells me that my iphone is connected to a different itunes account and will sync, but delete everything from my phone. How do I make itunes sync and update my phone without deleting all the current information on my phone?

    Iphone will sync with one and only one cpomptuer at a time.  Syncing to another will erase the current content and replace with content from the new computer.  This is the design of the  iphone.
    It will erase the content when you sync.
    Why not update from the comptuer with which you normally sync?

  • Can anyone offer some advice i am looking to upgrade the OS system on one of my macbook pro's, currently running os10.4.11, I would like to upgrade to OS10.5? how would I go about this, and is there a cost, for what is an old operating system now?

    Can anyone offer some advice i am looking to upgrade the OS system on one of my macbook pro's, currently running os10.4.11, I would like to upgrade to OS10.5? how would I go about this, and is there a cost, for what is an old operating system now?

    Since your Mac probably came with 10.4, there is no longer a way to get 10.5 Leopard install media. IF it has the requirements, you may be able to upgrade to 10.6 Snow Leopard by buying the boxed install media at the Apple Store for $30.
    System requirements are found here: http://support.apple.com/kb/SP575
    General support can be found here: http://www.apple.com/support/snowleopard/

  • How can I make the rolumn and row headers appear on the printed sheet?

    I am working in Numbers'09. I want to print out my chart with the column letters and row numbers showing.
    How can I make the rolumn and row headers appear on the printed sheet?

    g,
    Those annotations are called "Labels", and they are only visible during editing when a cell selection is made within the table. If you need to have the labels on your final output, you can create labels in Text Boxes and position them adjacent to the table.
    Jerry

  • When I print a webpage from FF how can I make the text and images bigger?

    I am trying to print an eticket for a flight. The text on the schedule comes out too small and the UPC symbol's black lines look ganged together. I know I can view it zoomed up, but how do I make the text and images stay zoomed so they will print out that way?

    I upgraded but the problem remains. At least with Firefox I do have the option to print a selection even if it mangles the top and bottom lines. With Safari or Chrome I have no option to print a selection. At the moment the only way to get a complete copy of the selection is to copy to openoffice etc and print from there.

Maybe you are looking for

  • Error when saving image

    When I try and save my image a pop-up comes up saying "Could not save as "/Users/.../Jewett9copy.jpg" because the disk is not available."  Does that mean my computer disk is too full or what?  This happened last night but then it ended up saving and

  • In IE I keep getting "adobe flash player 10 required" even though I have uninstalled and reinstalled

    in IE I keep getting "adobe flash player 10 required" even though I have uninstalled and reinstalled the latest version. Weirdly, the sound still works

  • Bean confusion - questions - beginner

    jsp A <jsp:useBean id="searchHeaders" scope="session" class="database.Tool"/> <p><c:out value="${searchHeaders.searchHeaderMap}"/> // This shows my bean info <p><a href="/<c:out value="${param.newID}"/>/searchTooling.jsp?newID=<c:out value="${param.n

  • Transporting active version of datasource from DEV to PRD

    Hi, When I transport a 3.x datasource from DEV to PRD, the version I am getting in PRD is 'modified'. In DEV we have both modified and active versions and there's no difference between them. When I try to activate the modified version, it says that I

  • Problems with sending IDOC via RFC from Unicode to NonUnic

    Hello I have following problem, sending an IDOC via RFC from Unicode to Non Unicode System. IDoc is not sent with error: codepage of receiver system cant determined. receiver destination was: Message no. B1999 I have tried different options in sm59,